I'm creating a list view with a fixed header and would like the list view's body to extend to the bottom of the page and still allow overflow on the y-axis. In practice runs, I was able to get everything working except I didn't try the idea of extending it to the bottom of the page until I went to put it into a page. Prior to this I was using fixed heights and everything was working as expected. Once I put it into my page, the list view then extended well beyond the bounds of the page and instead of extending to the bottom of the page and having overflow within the body, it just caused the page to have overflow instead.
Now, I've done some research on the topic and I've implemented the flexbox solution a few times successfully elsewhere in my web styles. However, this one is proving to be a bit of a pain and I'm not sure that I fully grasp the concept behind this approach.
I managed to create a minimal example of the issue, and during that process I managed to figure out that the one line in my CSS
that causes issues is flex-direction: column
within the .container
class. When I remove this line, the list view only extends to the bottom of the page and the overflow occurs within the list view. However, adding it back will cause the list view to extend the page instead.
var testTable = document.getElementById("test-table");
for (let i = 0; i < 150; i++)
testTable.innerHTML += '<tr><td>DATA ' + (i + 1) + '</td><td>' + i + '</td></tr>';
html,
body {
margin: 0;
height: 100%;
background-color: #fc3;
background-image: linear-gradient(45deg, #fc3, #f33);
}
.container {
display: flex;
flex-direction: column;
overflow-y: auto;
height: 100%;
}
.test-header {
height: 150px;
background-color: #fff3;
}
.test-body {
display: flex;
flex-direction: column;
background-color: #33f5;
}
.test-body table {
width: 100%;
table-layout: fixed;
}
.test-body-header {
background-color: #fff5;
}
.test-body-body {
flex: 1;
overflow-y: auto;
}
<div class="container">
<div class="test-header">A</div>
<div class="test-body">
<div class="test-body-header">
<table>
<tr>
<th>Data ID</th>
<th>Value</th>
</tr>
</table>
</div>
<div class="test-body-body">
<table id="test-table"></table>
</div>
</div>
</div>
I've added some JavaScript to reduce the amount of markup required to reproduce the issue.
Why does adding flex-direction: column
to my .container
class, cause this undesired effect? Also, since I've explicitly set the height of my .test-header
class to 150px
, why isn't that height being displayed? Are the two effects related?