Willi
Willi

Reputation: 407

how to correctly make flex-box children to scroll

I'm stuck and can't make content to correctly scroll while facing a few of nested flex-boxes. Here is my css:

html,body,#root{
   width:100%;
   height:100%;
   padding:0;
   margin:0;
}
.flex-fill{
   height:100%;
   width:100%;
   display:flex;
   align-items:stretch;
}
.flex-fill>div:last-child{
   flex-grow:1;
}
.flex-col{
   flex-direction:column;
}
<div id='root'>
  <div class='flex-fill flex-col'><!--actually scrolling div-->
     <div style="flex:0 0 35px"><h1>main title</h1></div>
     <div>
        <div class='flex-fill flex-col'>
           <div style="flex:0 0 30px"><h2>subtitle</h2></div>
           <div class='flex-fill'>
              <div style="flex:0 0 30%">...left side nav list...</div>
              <div class='flex-fill flex-col'>
                 <div style="flex:0 0 25px">...data header...</div>
                 <!--I hope the content of this div is scroll-able-->
                 <div style="overflow-y:scroll">...data list...</div>
              </div>
           </div>
        </div>
     </div>
  </div>
</div>

The data list contained in the inner most div is very long and I want to make it scrollable. But in the fact the above codes make whole content of the root div scrolling. Have I misunderstanding about flex or overflow? How can I fix the other parts and just scroll the data list?

Upvotes: 2

Views: 2148

Answers (2)

JoeP
JoeP

Reputation: 876

Answer updated to keep your HTML structure the same. Inline styles removed and added as classes:

html,body{
   height:100%;
   padding:0;
   margin:0;
}
#root {
  height: 100%;
	display: flex;
}
.flex-fill{
   height:100%;
   display:flex;
   align-items:stretch;
}
.flex-col{
   flex-direction:column;
   display: flex;
   flex: 1 0 0;
   height: 100%;
}
.flex-row{
   flex-direction:row;
   display: flex;
   height: 100%;
}
.scrolling-div {
	height: 100%;
	overflow-y: scroll;
}
<div id='root'>
  <div class='flex-fill flex-col'>
     <div><h1>main title</h1></div>
     <div class="flex-col">
        <div class='flex-col'>
           <div><h2>subtitle</h2></div>
           <div class="flex-row">
              <div>...left side nav list...</div>
              <div class='flex-col'>
                 <div>...data header...</div>
                 <!--Now Scrolls-->
                 <div class="scrolling-div">
				 ...scrolling data list...<br />
				 ...scrolling data list...<br />
				 ...scrolling data list...<br />
				 ...scrolling data list...<br />
				 </div>
              </div>
           </div>
        </div>
     </div>
  </div>
</div>

Upvotes: 2

Girisha C
Girisha C

Reputation: 1950

@Welson Zhong, please have a look at the below working snippet, please look it in Full page view, hope it helps now :)

note: too much flex kills flex ;) that was the issue with your code.

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}
body {
  display: flex;
  flex-direction: column;
  font-family: sans-serif;
  font-size: 18px;
}
.main {
  display: flex;
  flex-direction: row;
  flex: 1 0 auto;
}
.sidebar {
  flex: 1 auto;
}
.content {
  display: flex;
  flex-flow: column nowrap;
  flex: 3 0px;
}
h1,h2,.data-header {
  flex: none;
}

/* below css is for visual purpose only */
.main *:not(.content) {
  box-shadow: inset 0 0 0 1px #ccc;
  padding: 10px;
}
<h1>main title</h1>
<h2>sub title</h2>
<div class="main">
  <div class="sidebar">
    ...left side nav list...
  </div>
  <div class="content">
    <div class="data-header">...data header...</div>
    <div style="overflow-y:scroll; flex: auto">...data list...</div>
  </div>
</div>

Upvotes: -1

Related Questions