Takeshi Tokugawa YD
Takeshi Tokugawa YD

Reputation: 923

Prevent flex PARENT from growing without overflow: hidden

Too much information how to prevent flex child from growing, but too difficult to find out how to prevent flex PARENT from growing (because of the child).

Consider below layout:

enter image description here

Initial solution:

.layout {
  display: flex;
  flex-direction: column;
  width: 640px;
  height: 300px;
  background: rgba(220, 20, 60, 0.2);
}

.header {
  flex: 0 0 auto;
  padding: 4px 8px;
  background: rgba(220, 20, 60, 0.2);
}

.content {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  padding: 4px 8px;
  background: rgba(255, 69, 0, 0.2);
  overflow-y: hidden;
}

.title {
  flex: 0 0 auto;
  background: rgba(255, 255, 0, 0.2);
}

.splitView {
  display: flex;
  flex: 1 1 auto;
  background: rgba(0, 128, 128, 0.2);
  overflow-y: hidden;
}

.splitView-section {
  display: flex;
  flex-direction: column;
  flex: 1 1 50%;
}

.splitView-section:first-child {
  background: rgba(0, 191, 255, 0.2);
}

.splitView-section:last-child {
  background: rgba(0, 0, 255, 0.2);
}

.scrollView {
  flex: 1 1 auto;
  background: rgba(165, 42, 42, 0.2);
  overflow-y: auto;
}

.veryBigElement {
  background: #CD5C5C;
  width: 50px;
  height: 500px;
  box-shadow: rgba(0, 0, 0, 0.25) 0 0 6px
}

.footer {
  flex: 0 0 auto;
  padding: 4px 8px;
  background: rgba(173, 255, 47, 0.2);
}
<div class="layout">
  <div class="header">I am header</div>
  <div class="content">
    <div class="title">Title</div>
    <div class="splitView">
      <div class="splitView-section">
        <div class="sectionTitle">Section title</div>
        <div class="scrollView">
          <div class="veryBigElement"></div>
        </div>
      </div>
      <div class="splitView-section"></div>
    </div>
  </div>
  <div class="footer">I am footer</div>
</div>

It works. But there is one problem: if we add some elements with shadows (e. g. cards to scroll view), because of overflow-y: hidden for .splitView and .content, we will not see the part of it. E. g. if to add box-shadow: rgba(black, 0.25) 0 0 6px for .veryBigElement, we see just:

enter image description here

So, in dependence to design, above solution may not be used. If we remove overflow-y: hidden from .splitView and .content, splitView-section will grow regardless to overflow-y: auto for .scrollView.

Please not that:

JSFiddle: https://jsfiddle.net/teyqkrh8/

Upvotes: 5

Views: 2768

Answers (3)

wol
wol

Reputation: 424

I know it is an old question, but anyway would be useful for others.

Since you don't want to apply some padding to outer container or margin to the shadowed element, you can simply give z-index (greater than any outer elements z-index (if set of course)) property to the shadowed element and it will override cutoffs.

Upvotes: 1

Mak0619
Mak0619

Reputation: 660

If you gave the "veryBigElement" height 500px and you want main content will not be scrolling only inner elements could be scrolled. So you have to add MAX-HEIGHT of veryBigElement parent. If you remove the overflow-y: hidden than splitView-section will not grow.

Upvotes: 0

Salix
Salix

Reputation: 1384

You can remove "overflow-y: hidden" and add "height: 100%" to .splitView and -section.

.splitView{
    display: flex
    flex: 1 1 auto
    background: rgba(#008080, 0.2)
    height: 100%
    &-section{  
        display: flex
        flex-direction: column
        flex: 1 1 50%
        height: 100%
    }
}

Since the height of the parent div is set, the children divs will inherit that height.

https://codepen.io/salixdubois/pen/JQLaRE

Upvotes: 1

Related Questions