Breaking Elements out of Their Containers in CSS with .full-bleed

About two years ago I wrote a post on How to Break Elements out of Their Containers in CSS so that they are “full bleed”.

Whilst the method which uses CSS Grid still is accurate and works really well (I use it “in production”), the “old” method I used (in case of situations where one is not using CSS Grid) can be simplified.

To recap: My technique required me needed to do some manual calculations that were dependent on the parent element. The result was a tad of CSS that contained some “magic numbers”:

.main {
  width: 70%;
}

.main > .content--fullwidth {
  width: 142.857142857%; /* = (100% / 70%) * 100% */
  transform: translateX(-15%); /* = (100% - 70%) / 2 * -1 */
}

Not very scalable, as it needs adjustment whenever the parent container’s width is altered.

Yesterday I was delighted to see that Andy Bell came up with a much simpler solution, that is not dependent on the parent container’s dimensions:

.full-bleed {
  width: 100vw;
  margin-left: 50%;
  transform: translateX(-50%);
}

The blob of CSS first makes the targeted element fullwidth, then shifts its left edge to the center of the page, and then translates the entire element back to the left edge of the page again. Clever!

Where this technique differs from the CSS Grid technique is that he CSS Grid one will also work fine in scenarios where you want to stretch out an element inside a any parent container (other than the page), whereas the .full-bleed utility always stretches an element out so that it’s full page (not “full container”).

Creating a full bleed CSS utility →

Breaking Elements out of Their Containers in CSS

A common thing to see in a design are visuals that break out of the main content column. You most likely have already seen this kind of design over at Medium for example, where it’s common for the main visual to be full width (or “full bleed”), whilst the content remains centered.


Your typical Medium layout:
content caged in a central column + fullwidth visual breaking out of that column

Last week, whilst attending CSS Day I learnt of a technique I hadn’t seen before to solving this problem.

The old tried and battle tested way of doing it (or at least the way I used to do it)

Let’s start of with some HTML and a tad of CSS:

<div class="main">
    
    <div class="content">
      <p>Some text</p>
    </div>
    
    <div class="content--fullwidth">
      <img src="…" />
    </div>
    
    <div class="content">
      <p>Some more text</p>
    </div>
 
</div>
.main {
  width: 70%;
  margin: 0 auto;
}

img {
  width: 100%;
}

To make the .content--fullwidth element break out of its parent container .main, I use a technique where I stretch it out so that it takes up the outer 100% again, and then recenter it.

One can achieve just this by giving the .content--fullwidth element:

  1. A width which is the inverse of the width of .main to stretch it out.
    Here 1 / 70% = 142.857142857%
  2. A negative translation on the X-axis equalling half of the remaining width to correctly position it.
    Here (100% - 70%) / 2 * -1 = -15%

In CSS that’d become this:

.main > .content--fullwidth {
  width: 142.857142857%;
  transform: translateX(-15%);
}

In case you want to verify your numbers: the product of both percentages should again be 100%. 70% * 142.857142857% indeed equals 100% 🙂

Here’s a pen demoing the technique described above:

See the Pen Breaking elements out of containers, technique #1 (inverse width + translate) by Bramus (@bramus) on CodePen.

Ow yeah 🙂

~

The new and shiny way of doing it

When using CSS Grid, you have much nicer option. Last week at CSS Day I saw Rachel Andrew give a nice demo on how to break a set of images outside of the “content column”.

We start off with the same HTML again:

<div class="main">
    
    <div class="content">
      <p>Some text</p>
    </div>
    
    <div class="content--fullwidth">
      <img src="…" />
    </div>
    
    <div class="content">
      <p>Some more text</p>
    </div>
 
</div>

To limit the width of the contents she uses a CSS Grid Layout containing:

  1. A fluid column, at least 1em in width
  2. A main column, 70% in width
  3. A fluid column, at least 1em in width

The containing content is then placed inside that 2nd column.

.main {
  display: grid;
  grid-template-columns:
    minmax(1em, 1fr)
    minmax(0, 70%)
    minmax(1em, 1fr)
  ;
}

.main > * {
  grid-column: 2;
}

Breaking the .content--fullwidth element out of its parent container .main now becomes rather easy, as only has to make it span all three columns.

In CSS that’d become this:

.content--fullwidth {
  grid-column: 1 / 4;
}

🤔 Huh? 4? Shouldn’t that be 3?
👉 Nopes. Remember, in CSS Grid you’re targeting lines 😉

Here’s a pen demoing the technique described above:

See the Pen Breaking elements out of containers, technique #2 (CSS Grid) by Bramus (@bramus) on CodePen.

~

If you’re using CSS Grid then Rachel’s technique will come in handy. For projects in which you need to support older browsers, you can still rely on the first one.

💁‍♂️ New to CSS Grid Layout? Rachel has got you covered with her excellent Grid By Example.

Did this help you out? Like what you see?
Consider donating.

I don’t run ads on my blog nor do I do this for profit. A donation however would always put a smile on my face though. Thanks!

☕️ Buy me a Coffee ($3)