css - Force a flexbox element to scroll in cross-axis after matching other element's size -
there few questions similar after going through i'm pretty sure nothing covers exact question. similar this question flex element rather static-size picture.
using flexbox want able have single element match height (cross-axis) of other elements, , present scroll if content in element overflow, rather growing height of container.
i have set simple jsfiddle show mean.
essentially blue section collapse , present scrollbar if go extend further down red, rather extending flexbox vertically.
i have tried few things , closest can is:
styling ".element2" { min-height:0; height:0; } , ".body" { overflow-y:auto; }
which doesn't scroll has first element of correct size.
any appreciated, , i'm not wed using flexbox if there easier solution.
first fiddle code:
.container{ display:inline-flex; } .element1{ background-color:red; } .element2{ min-width:180px; } .title{ background-color:green; } .body{ background-color:blue; } <div class="container"> <div class="element1"> want element control height of other element. want element control height of other element. want element control height of other element. want element control height of other element. want element control height of other element. </div> <div class="element2"> <div class="title"> v needs scrollbar v </div> <div class="body"> scroll element <br/> scroll element <br/> scroll element <br/> scroll element <br/> scroll element <br/> scroll element <br/> scroll element <br/> scroll element <br/> </div> </div> </div>
make right column
position: relativeoverflow-y: autoscroll when neededmake right column's children
position: absolute, position appropriatelytop,left,right.
the children no longer take height, allowing box freely grow , shrink in height.
example 1 - title scrolls
a min-height idea right column.
.container { display: inline-flex; } .element1 { background-color: red; } .element2 { min-width: 180px; overflow-y: auto; position: relative; min-height: 50px; } .element2 > * { position: absolute; top: 0; left: 0; right: 0; } .element2 .body { top: 1em; } .title { background-color: green; } .body { background-color: blue; } <div class="container"> <div class="element1"> want element control height of other element. want element control height of other element. want element control height of other element. want element control height of other element. want element control height of other element. </div> <div class="element2"> <div class="title"> v needs scrollbar v </div> <div class="body"> scroll element <br/>scroll element <br/>scroll element <br/>scroll element <br/>scroll element <br/>scroll element <br/>scroll element <br/>scroll element <br/> </div> </div> </div> example 2 - title fixed
we can fix title in place placing overflow-y: auto , position: absolute on .body element. restrict height bottom: 0 stretches bottom of parent (.element2)
.container { display: inline-flex; } .element1 { background-color: red; } .element2 { min-width: 180px; position: relative; min-height: 50px; } .element2 .body { overflow-y: auto; position: absolute; top: 1em; left: 0; right: 0; bottom: 0; } .title { background-color: green; } .body { background-color: blue; } <div class="container"> <div class="element1"> want element control height of other element. want element control height of other element. want element control height of other element. want element control height of other element. want element control height of other element. </div> <div class="element2"> <div class="title"> v needs scrollbar v </div> <div class="body"> scroll element <br/>scroll element 1 <br/>scroll element 2 <br/>scroll element 3 <br/>scroll element 4 <br/>scroll element 5 <br/>scroll element 6 <br/>scroll element 7 <br/> </div> </div> </div>
Comments
Post a Comment