How can we make a margin element fill a flexbox item that is not itself a flexbox?
An element (even a nested one with margins) can easily fill its container using position: absolute
-- if it's not inside a flexbox item. Why does this not work for an element inside a flexbox item?
<main>
<nav>NAV</nav>
<section>
<div>DIV</div>
</section>
</main>
<style>
html, body {
position:absolute; top:0; left:0; right:0; bottom:0;
margin: 0;
}
main {
position:absolute; top:0; left:0; right:0; bottom:0;
display: flex;
}
nav {
flex-basis: 250px;
background-color: #eee;
}
section {
flex-basis: 100%;
background-color: #ccc;
margin: 10px;
}
div {
/* position:absolute; top:0; left:0; right:0; bottom:0; */
/* why doesn't the above line work? */
background-color: #cfc;
margin: 10px;
}
</style>
There are many similar-looking questions like this one and this one that don't really apply to items inside flexboxes or items with margin. There are loads of special-case solutions like align-self: stretch
, height: 100%
and box-sizing: border-box
that just don't work in this example because of the nested margin
or the fact that the flexboxes themselves aren't nested. The problems with these one-off hacks go on and on...
So what is the general method to fill a flexbox item? What is the issue with position:absolute
here? What is the most general way to make an element fill its container?