That is an previous trick, however one I attain for simply typically sufficient to warrant a put up on this weblog — for no purpose apart from my very own reference!
The issue
Suppose you could have a web page of content material, all neatly constrained inside a central wrapper. Most likely utilizing some form of utility class, like so:
.wrapper {
width: 70rem;
max-width: 100%;
margin-inline: auto;
}
(I’m utilizing the margin-inline
logical property as shorthand for margin-left
and margin-right
.)

However oh no! All of the sudden you end up in want of a element that spans the complete width of the web page within the midst of your neatly constrained content material. What to do? Nicely, there are a bunch of choices.

The answer(s)
Answer 1: Wrap the content material both aspect
OK, we may wrap the content material above and beneath our full-width part in our wrapper
class.

Positive, that’ll work nicely sufficient. However that depends on us having ample management over the markup, which isn’t all the time the case if, as an illustration, your content material is coming from a CMS. And it doesn’t really feel like probably the most versatile resolution, if we need to add extra full-width sections sooner or later.
Answer 2: Wrap each element
As a substitute of wrapping teams of parts, what about if we wrap every one individually?

Once more, it really works the place we are able to management the markup, if we’ve constructed our personal customized parts. However in WordPress as an illustration, paragraphs, heading, checklist blocks and so on. are rendered as easy HTML components, with none form of wrapping markup to hook into. There’s most likely a manner you would wrap each element in a <div>
in the event you’re au fait with PHP, WordPress and the block editor, but it surely’s actually past my talents. And, in any case, it’d end in in some points with accessibility and semantics, in addition to simply feeling form of gross. Div soup anybody?
Answer 3: Append a category to each element
Reasonably than wrapping every element, how about including a category? We’ll give every of the parts that must be constrained the wrapper
class. So our markup would find yourself one thing like this:
<most important>
<h1 class="wrapper">Heading</h1>
<p class="wrapper">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim advert minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
</p>
<p class="wrapper">
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
eu fugiat nulla pariatur.
</p>
<!--Full width component-->
<div>...</div>
<p class="wrapper">
Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia
deserunt mollit anim id est laborum.
</p>
</most important>
Or, we may go the opposite manner: Set our wrapper types on all direct youngsters of our most important
aspect, then set a full-width
class on the full-width ones.
most important > * {
width: 70rem;
max-width: 100%;
margin-inline: auto;
}
most important > .full-width {
width: 100%;
}
This doesn’t appear to be a fantastic resolution, because it assumes every direct youngster must be the identical width. You might need some parts that have to be narrower than that central wrapper, however nonetheless align with the remainder of the content material.

Adjusting the margins or widths of these components may have unpredictable outcomes. For instance, setting a narrower width leads to misalignment.

To not point out, we’ll want to extend the specificity of the weather we need to goal for particular person styling.
most important > * {
width: 70rem;
max-width: 100%;
margin-inline: auto;
}
/* This may haven't any impact :( */
p {
width: 65ch;
}
/* We gotta do that as an alternative */
most important > p {
width: 65ch;
}
Answer 4: Viewport width breakout approach
Utilizing viewport models, we are able to drive a component to interrupt out of the wrapper, with out altering our authentic markup! Initially shared by Una Kravets in 2018, this little CSS snippet will just do that:
.full-width {
place: relative;
proper: 50%;
left: 50%;
margin-left: -50vw;
margin-right: -50vw;
max-width: 100vw;
width: 100vw;
}

Useful! However you recognize what? We are able to do it much more concisely with a remodel:
.full-width {
width: 100vw;
margin-left: 50%;
remodel: translate3d(-50%, 0, 0);
}
This comes with a small caveat: It received’t work if we now have overflow: hidden
on the wrapper aspect. Nonetheless, if we are able to keep away from that, and if we don’t have management over the markup (or even when we simply want a fast repair), it is a nice resolution.
Answer 5: Grid
It is a grid sample I attain for time and time once more, ostensibly for exactly this form of downside. We create a grid with two flexible-width outer columns (utilizing the fr
unit) and a number of max-width contrained columns. On this case, a single central column will serve our wants. I like to call my grid strains, to make placement simpler:
.wrapper {
show: grid;
grid-template-columns:
[full-start] 1fr [wrapper-start]
minmax(0, 70rem) [wrapper-end] 1fr [full-end];
/* Non-compulsory hole */
column-gap: var(--pad, 1rem);
}
Then we are able to place all direct youngsters of our wrapper into the central column, besides those that want to interrupt out:
.wrapper > * {
grid-column: wrapper;
}
.wrapper > .full-width {
grid-column: full;
}

I like so as to add a column hole once I’m utilizing this system, as a result of it means our grid will likely be responsive straight off — when the viewport is slim there will likely be an area between the sting of the viewport and the content material within the central column, we don’t want to make use of padding. I desire to not set a row hole, as I typically must have extra management over the area between objects, giving headings more room above them than paragraphs, as an illustration. Grid doesn’t allow us to set completely different hole values, so the areas between each merchandise could be the identical. Choosing margins as an alternative permits for extra management.
This demo exhibits the approach used for a web page format.
See the Pen
Untitled by Michelle Barker (@michellebarker)
on CodePen.
That is my favorite resolution to the breakout, and one I’ve used on loads of websites. My solely phrase of warning is that in the event you’re retro-fitting an previous web site with this system, be sure you do loads of testing to verify any leftover workarounds in your code don’t trigger format bugs.