level up some UI chops

Overlapping Grid Items

overlapping css grid items

Now that CSS Grid has arrived, we’re starting to see more and more magazine style layouts on the web. And for good reason: they’re clean, readable, attractive, and familiar. You might even be tasked with building one right now. If not, you almost certainly will soon.

The secret to an awesome magazine-style grid layout is:

  1. organize content into a well-structured grid.
  2. break out of the grid with overlapping grid items.

Now that you know about this little formula you’re going to start noticing it everywhere. On the front and back of your favorite cereal box. In your grandma’s magazines. And increasingly, in your favorite web sites/apps.

Here’s how to build awesome magazine-style grid layouts by overlapping grid items.

How to Overlap CSS Grid Items

In order to overlap grid items, you have to get them positioned into the same grid cells. There are four ways to accomplish that:

  1. using grid line numbers
  2. using named grid lines
  3. using named grid areas
  4. spanning grid items

Let’s take a look at a 3x3 grid containing two grid items, and compare these overlapping techniques. Let’s position Item 1 (purple) into the top row, and Item 2 (red) into the left column, letting the two items overlap in the top left corner (row 1, column 1).

grid line numbers

The browser automatically numbers each grid line, staring with the number 1 and ending with number of tracks + 1.

See the pen by Dave Geddes (@geddski) on CodePen.

For item 1 we used the longhand grid-[row|column]-[start|end] keywords. For item 2 we used the convenient grid-area shorthand that sets all four positioning settings at once.

Since both items are instructed to occupy the top left cell of the grid, they overlap.

named grid lines

Using the automatic line numbers is straightforward enough, but can become hard to work with later if you’re changing your Grid a bunch, like in a responsive design or even just changing your mind while you work on the layout.

So instead of numbers we can name the lines ourselves, and position our items using our custom line names.

See the pen by Dave Geddes (@geddski) on CodePen.

The syntax for naming lines is line, track, line. Here’s a full guide on naming grid lines.

named grid areas

Named grid areas are my go-to when I start building a new grid layout. The grid-template-areas property is essentially just a shortcut for quickly naming your grid lines.

See the pen by Dave Geddes (@geddski) on CodePen.

Notice how we kept the exact same item positioning from the last example, but named our lines in a lot cleaner fashion thanks to grid-template-areas.

An important thing to note with grid areas is that cells can only have one name. That top left cell where we overlap things we can’t name both “header” and “sidebar”. So while we could simplify item 1 to position it into the “header” area, item 2 still needs to use the line names that grid-template-areas created:

See the pen by Dave Geddes (@geddski) on CodePen.

spanning grid items

The last way to make grid items overlap is by spanning into the same cell.

See the pen by Dave Geddes (@geddski) on CodePen.

Here we’ve attached item 1 to the last line on right by using -1, and made it span 3 columns startwards (to the left).

Item 2 we’ve told to start in row 1 column 1, and span 3 rows.

When using the spanning method be sure your items specify the row where you want them, otherwise the grid item placement algorithm will move them down to the next available empty slot instead of overlapping. To visualize exactly how that works see how items flow into a grid.

Control the Stack with Z-Index

Once your grid items are overlapping like you want, you can control which one is on top using z-index.

Let’s make item 1 be on top for a change:

See the pen by Dave Geddes (@geddski) on CodePen.

Let’s Build One!

Now that you know how to overlap grid items let’s check out a real-world example! Here is a page in PC Magazine talking about Divinity 2: Original Sin 2 game (a way fun little game BTW).

pc magazine grid layout

Here’s how we can break this layout up into a grid:

pc magazine grid breakdown

This grid has four columns. The outer columns are a bit wider than the inner ones. We’ve got three rows - the top one is roughly 130px. The middle row is about twice as big as the bottom row.

This should do it:

.magazine {
  display: grid;
  grid-template-rows: 130px 2fr 1fr;
  grid-template-columns: 1.5fr 1fr 1fr 1.5fr;

Let’s drop some divs into our grid and see we what we have:

See the pen by Dave Geddes (@geddski) on CodePen.

Our divs are being positioned using the default grid placement algorithm. This grid is pretty simple so let’s use the grid line numbers approach for positioning.

.title {
  grid-row: 1;
  grid-column: 1 / 3;

.image {
  grid-row: 1 / 3;
  grid-column: 2 / 5;

.content1 {
  grid-row: 2 / 4;
  grid-column: 1 / 2;

.content2 {
  grid-row: 3 / 4;
  grid-column: 2 / 4;

.quote {
  grid-row: 2;
  grid-column: 4;

.score {
  grid-row: 3;

See the pen by Dave Geddes (@geddski) on CodePen.

There we go. This grid contains same items as before, but now they’re positioned and overlapping how we want them.

We had to break the content up into two grid items to achieve this L-shape of content. The CSS Grid spec doesn’t currently have a way to let text flow from one grid item to another, and grid areas have to be rectangular.

At this point the overall layout is pretty much done. If we add a bit of content and refine a couple of styling details it begins to resemble the magazine:

See the pen by Dave Geddes (@geddski) on CodePen.

Not bad eh?! We could spend more time making it pixel-perfect and mobile friendly, but this is good enough for this exercise.

One interesting thing to check out is that quote box that’s overlapping the game image. It’s positioned into the 2nd row, 4th column. But we used position: relative to shift it up and to the left to break out of the grid a bit, just like it does in the magazine. Keep an eye out for this. Oftentimes layouts that don’t appear to be a grid actually are, just with some clever item shifting.

Overlap your Grid Items

Creating magazine-style layouts on the web has never been easier thanks to CSS Grid and the ability to overlap grid items.

There are also a ton of cool effects you can achieve by overlapping grid items. For example combining it with background blend modes. You can even mimic styling grid gaps.

Go try it yourself! Tear a page out of any physical magazine — at the dentist, your grandma’s, wherever you can find one. Build that magazine layout using CSS Grid and overlapping some of the grid items. Don’t forget z-index for the stacking order. Have fun!

Master CSS Grids right from the start by playing this new interactive mastery game. You'll learn the ins and outs of Grids one fun level at a time, while saving an adorable alien life form from certain destruction.