The following simple snippet results in a single web page that takes up the available screen space with a header at the top, a footer at the bottom, and the main content taking up as much space as possible (with a dotted border to make it easier to see):
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-flow: column;
}
h1, small {
flex: 0 1 auto;
}
div {
flex: 1 1 auto;
border: 1px dotted;
}
<!doctype html>
<html>
<body>
<h1>Some Header</h1>
<div>Some Text</div>
<small>Some Footer</small>
</body>
</html>
If I modify the CSS and HTML to instead use a table
in place of a div
, it doesn't expand the table to consume the available space:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
display: flex;
flex-flow: column;
}
h1, small {
flex: 0 1 auto;
}
table {
flex: 1 1 auto;
border: 1px dotted;
}
<!doctype html>
<html>
<body>
<h1>Some Header</h1>
<table><tr><td>Some Content</td></tr></table>
<small>Some Footer</small>
</body>
</html>
Why does the first version (with div
) work but the second version (with table
) not? And is there a way to fix the second example so the table expands and consumes all available space (without introducing scroll bars)?
Some notes: my table will have a row of headers (all with equal width) and several rows (all with equal width/height). I know it's possible to recreate a table using a bunch of div
s and more CSS, but going down that route feels like throwing out the baby with the bathwater (and besides, I wouldn't get to ask this question and learn if I just hacked around it). Also, JavaScript isn't available to me here (and seems like overkill).
I know enough CSS/HTML to get myself into trouble but not enough to get myself out of it...
Edit: aavrug's suggestion to use display: flex
for the table
makes it so the table properly expands to fill the area, but when I add multiple rows/columns to the table they are no longer equidistributed. I'd like to preserve the table
's cell equidistribution.
display: table;
just override it withdisplay: flex;
and it will be fine. – Tadddisplay:flex
as well, since the<td>
elements are a child of a child of the flex parent. – Fixture<div>
inside and use flex on that one. – Caulk<div><table><tr><td>Some Content</td></tr></table></div>
)? If so, then the div takes up all the space but not the inner table. – Sonnie