I’m trying to follow some advice i’ve seen regarding not using margins for spacing and instead to use a flex container with column direction and the gap property. It’s much nicer, except I can’t work out how to break out of the container when needed for a background color on single flex items.
https://codepen.io/Xe11o/pen/VwVOpPP
.container {
display: flex;
flex-direction: column;
max-width: 1130px;
margin: 20px auto 0 auto;
gap: 50px;
}
header {
display: flex;
justify-content: space-between;
}
nav {
display: flex;
}
section {
font-size: 50px;
}
footer {
font-size: 25px;
background: lightgray;
}
<html>
<body>
<div class='container'>
<header>
<span>title text</span>
<nav>
<button>button link 1</button>
<button>button link 2</button
</nav>
</header>
<section>main content</section>
<footer>
Footer content.
<br />
How do i get this background color to stretch beyond the flex container to the edge of the screen?
<br />
I can remove the max-width+margin from the container and apply them instead to the children (header, section), then maybe do an align-items on the container, but then I can't get the header to look like it does right now.
</footer>
</div>
</body>
</html>
2
Answers
One possible solution for stretching your
<footer>
container to the edge of the screen is to apply awidth: 100vw;
to let the<footer>
fill the whole screen horizontally and analign-self: center;
to position the element accordingly in the center of the screen so you prevent any overflow issues.P.S. I adjusted the
max-width
of.container
so one can see that the layout of the header and main content doesn’t change in the snippet.The footer is restrained by the max-width of the container. It may be possible to break out of this but I think it would seem more sensible to remove this restriction to begin with.
In the example below, I’ve moved the max-width so that it applies to just the header and sections, and then the footer can expand to the edge of the page. (I’ve removed the margins on the body just to demonstrate that it goes right to the edge). You mentioned that doing this did not give you the desired header but I think it looks the same as in your example if you set the header width to 100%.