Skip to content
Tutorials Center
My learning

Topics

Lesson 79 of 139 ยท Grid

Grid Patterns

Put CSS Grid to work with proven patterns: full page layouts, responsive galleries without media queries, overlapping text on images and breakout content.

You have met every part of CSS Grid: tracks, placement, named areas, alignment and subgrid. This lesson puts them together into the layouts you will build most often. Each pattern is small enough to memorize, and each one solves a problem that used to take floats, absolute positioning or a pile of media queries.

As in Flexbox Patterns, the goal is to recognize which few properties a layout needs. Resize the previews as you go; several of these patterns adapt to any width all by themselves.

Pattern 1: the page skeleton

Almost every website has a header, a main area and a footer, and many have a sidebar. Grid describes that shape in a few lines. With named areas, the CSS even looks like the page:

CSS
.page {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 200px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header header"
    "nav    main"
    "footer footer";
}

The rows auto 1fr auto give the header and footer their natural heights and hand all remaining height to the middle row. That's the Grid version of the sticky footer: on a short page the footer sits at the bottom of the window, on a long page it follows the content.

The pattern becomes responsive by starting with a single column and adding the sidebar when there is room. This is the mobile-first approach:

Responsive page skeleton

Below 500px, everything stacks and the menu runs as a wrapping row. From 500px up, the template switches to two columns and the menu becomes a vertical sidebar. Only the template changes; the HTML and the grid-area names stay the same. The width >= 500px syntax is the modern range form of a media query, explained in Media Queries.

This is the line from Grid Tracks that made Grid famous:

CSS
grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));

The browser fits as many columns of at least 160px as it can, then stretches them to fill the row. There is one weakness: when the container is narrower than 160px, a single column still demands 160px and overflows. Wrapping the minimum in min() fixes that:

CSS
grid-template-columns: repeat(auto-fill, minmax(min(160px, 100%), 1fr));

Now the minimum is "160px, or the full width if that is smaller". The calc(), min(), max() and clamp() lesson covers these functions in depth.

Self-adjusting gallery

Resize the preview and watch the console report the column count. No breakpoints anywhere: the number of columns follows the available space. Change --min to 200px for bigger tiles.

The featured item spans two columns, and its image uses a wider 8 / 3 aspect ratio so it stays about as tall as its neighbors. Be careful with spans in auto-filled grids: if the grid ever has only one column, a span 2 item creates an extra implicit column and the layout overflows. Here the min() guard plus a 130px minimum keeps two columns in any reasonably wide preview, but on very narrow screens you would turn the span on only inside a media or container query.

auto-fill or auto-fit? With only a few items, auto-fill keeps empty column slots and items stay their normal width, while auto-fit collapses the empty slots so the items stretch to fill the row. Galleries usually look better with auto-fill; a short row of feature boxes often looks better with auto-fit.

Pattern 3: overlapping items

In Grid, two items can occupy the same cell. They simply stack on top of each other, later items above earlier ones (adjust with z-index if needed). That makes text-on-image banners easy without any position: absolute.

The trick is to place every child in the same area:

Text over an image

grid-area: 1 / 1 puts every child in row 1, column 1. The grid cell is as tall as its tallest child, so the banner grows if the headline wraps onto more lines. That is something absolute positioning can't do, because absolutely positioned text doesn't affect its container's size. Each layer then positions itself with align-self and justify-self, which you learned in Grid Alignment.

The image has an empty alt because the heading already carries the meaning; if the picture added information, you would describe it.

Overlap also works with line numbers across larger grids, for instance a quote that starts in column 2 of a picture spanning columns 1 to 3. Any time two items share some cells, they overlap.

Pattern 4: the breakout layout

Long-form articles look best with a narrow text column (around 60 to 70 characters per line). But some elements, such as wide images, code samples or colored callouts, should be allowed to "break out" wider, or even run edge to edge.

Named lines make this neat. Define a center column with gutters on both sides, and let individual elements choose how wide they go:

Breakout article layout

Each named area like content works because lines named content-start and content-end exist; Grid lets you use the shared prefix as an area name. Children default to the content column, and the wide and full classes widen them. The min(42ch, 100% - 2 * var(--gutter)) track keeps text readable on large screens and lets it shrink on small ones without overflowing. (Inside min(), you can do arithmetic directly, no calc() needed.)

If the sections of your article contain their own nested elements that should also follow these lines, combine this pattern with subgrid.

Pattern 5: the pancake and the sidebar, in one line each

Two small layouts are so common they deserve a mention:

CSS
/* Header, content, footer stacked: the "pancake stack" */
.stack {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 100%;
}

/* A sidebar that never gets narrower than 150px or wider than 25% */
.with-sidebar {
  display: grid;
  grid-template-columns: minmax(150px, 25%) 1fr;
}

The pancake is the same idea as the page skeleton, shrunk to fit any component, such as a modal with a fixed header and footer and a scrolling body (add overflow: auto to the middle row). The sidebar line uses minmax() so the sidebar flexes within sensible limits.

Choosing between patterns

  • Building the whole page frame? Use named areas and rearrange them at breakpoints.
  • A collection of similar items? Use auto-fill with minmax(min(โ€ฆ, 100%), 1fr) and skip the media queries.
  • Layers on top of each other? Put them in the same cell.
  • Article content with occasional wide elements? Use named lines for a breakout layout.
  • Items in a single row whose sizes depend on content? That is still Flexbox's job.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does grid-template-rows: auto 1fr auto achieve on a full-height page grid?

    The auto rows size to their content and the 1fr row absorbs the rest, so a short page still has its footer at the bottom. It is not fixed while scrolling.
  2. 2. Why wrap the minimum in min(160px, 100%) inside minmax()?

    Without it, each column demands at least 160px even when the container is narrower. min() lowers the minimum to the container width in that case.
  3. 3. How do you stack a caption on top of an image using Grid?

    Items placed in the same cell overlap, with later items painted on top. The cell grows to fit the taller item, unlike absolute positioning.
  4. 4. In a breakout layout, why can a child use grid-column: content?

    Lines named <name>-start and <name>-end let you refer to <name> as an area, even if grid-template-areas is not used.

Summary

  • Page skeleton: named areas with grid-template-rows: auto 1fr auto, starting with one column and adding a sidebar at a breakpoint.
  • Gallery: repeat(auto-fill, minmax(min(160px, 100%), 1fr)) adapts the column count to any width without media queries.
  • Overlap: put several children in the same cell (grid-area: 1 / 1) and position each layer with align-self and justify-self.
  • Breakout: named lines (full, wide, content) let elements choose how wide to run while text keeps a readable measure.
  • Pancake and sidebar: auto 1fr auto rows and minmax() columns cover many components in a single line.

Next: Responsive Design Principles, where you learn to make all of these layouts work on every screen.