I have a container with two children, a header
and a content
. I want both children to take up all available space in the container, even if the content in content
overflows and causes a scroll. Additionally, I want the header
and content
backgrounds to extend over all available space.
Here’s the HTML and CSS code that I’ve tried:
<div class="container">
<div class="header">
<span>header1</span>
<span>header2</span>
</div>
<div class="content">
contentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontentcontent
</div>
</div>
<style>
.container {
display: flex;
flex-direction: column;
overflow: auto;
width: 500px;
border: 1px solid black;
}
.header {
background: red;
display: flex;
justify-content: space-between;
}
.content {
background: green;
}
</style>
However, the header doesn’t take up all available space in width and header2 is visible even though it should be hidden by the overflow scroll. Additionally, the header and content backgrounds don’t extend over all available space.
How can I make both children take up all available space, even with overflow, and make the backgrounds extend over all available space?
Thank you in advance for your help!
2
Answers
You could do it with an extra wrapper and a few other tweaks:
You can set
.container { display: grid; }
: