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, 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

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, you need to 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

So last week at CSS Day I saw Rachel Andrew give a nice demo where she used CSS Grid – what else? – 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.

Original Content , ,

One Response to Breaking Elements out of Their Containers in CSS

  1. Pingback: Aspect Ratios in CSS are a Hack | Bram.us

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.