Skip to content

Lesson 20 of 52Layout

Common Layouts

Plain-CSS recipes you will reuse on every project, from centring and sticky headers to sidebars, the holy grail, card grids, sticky footers and full-bleed sections.

22 min read0/4 questions

You now know Flexbox, Grid, positioning and media queries. The good news is that most real pages are built from a short list of the same patterns, put together in different ways. A blog post is a centred column with a few full-width images. A dashboard is a sidebar layout with a card grid inside. A product page is a split screen above a card grid. This lesson is a recipe book: each section shows one layout, the few lines of CSS that make it, and which tool (Flexbox or Grid) does the work and why. Every recipe has a live editor, so copy them freely.

Picking the tool

Before the recipes, a quick rule of thumb from the last two lessons. Ask yourself one question: do things need to line up in two directions?

  • If yes (rows and columns, a page shell, a gallery), use Grid. You define the tracks on the parent and the children fall into them.
  • If no (one row or one column, and the content should decide the sizes), use Flexbox.
  • If an element must ignore the flow entirely (a header that stays put while you scroll), reach for positioning, usually position: sticky.

Most of the recipes below are only three to six declarations long. That is the point: once you know the pattern, you never have to reinvent it.

Which tool for which layout
Two directions?: rows and columnsTwo directions?rows and columnsGrid: shells, galleriesGridshells, galleriesFlexbox: rows, toolbars, mediaFlexboxrows, toolbars, mediaSticky: stays while scrollingStickystays while scrollingyesnoheader, sidebarnav bar

Things must line up across rows and columns: a page shell, a card gallery. Use Grid.

1/3

Recipe 1: Centre anything

There are two modern one-liners. Both work on the parent.

.center-grid { display: grid; place-items: center; }
.center-flex { display: flex; justify-content: center; align-items: center; }

place-items is a shorthand for align-items and justify-items together, so place-items: center centres in both directions. The grid version is shorter, which is why many developers default to it for a single child. The flex version is handy when you have several children that should sit in a centred row.

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; } .stage { display: grid; place-items: center; height: 220px; background: #f1f5f9; border: 2px dashed #94a3b8; } .badge { padding: 12px 20px; background: #1e293b; color: white; border-radius: 999px; }</style><div class="stage"> <div class="badge">Exactly in the middle</div></div>

Preview

Try this: swap the two lines for display: flex; justify-content: center; align-items: center; and the result is identical. Then add a second badge and notice the difference: Grid stacks them in the middle, Flexbox puts them side by side.

For horizontal centring of a block of text content (an article column), you do not need either tool. max-width plus margin-inline: auto does it, and you will see it again in the full-bleed recipe.

Recipe 2: Sticky header, scrolling main

You want the site header visible at all times while the content scrolls underneath. position: sticky with top: 0 keeps the element in normal flow until it reaches the top edge, then pins it there. Unlike position: fixed, the content below does not slide under it at the start, so you do not need a magic padding-top to compensate.

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; } .site-header { position: sticky; top: 0; z-index: 10; display: flex; justify-content: space-between; align-items: center; padding: 12px 16px; background: white; border-bottom: 1px solid #e2e8f0; } main { padding: 16px; } main p { margin: 0 0 16px; line-height: 1.6; }</style><header class="site-header"> <strong>Crumb</strong> <nav><a href="#">Recipes</a> <a href="#">About</a></nav></header><main> <p>Scroll this preview. The header stays at the top while the paragraphs move under it.</p> <p>Paragraph two. Sticky headers need a solid background, or the text shows through.</p> <p>Paragraph three. The z-index keeps the header above cards that create their own layers.</p> <p>Paragraph four.</p> <p>Paragraph five.</p> <p>Paragraph six.</p> <p>Paragraph seven.</p> <p>Paragraph eight.</p></main>

Preview

Try this: delete background: white; and scroll to see why the background matters. Then change sticky to fixed and notice the first paragraph now hides behind the header.

The header itself is a small Flexbox row (logo left, nav right). Sticky only controls where the box sits while scrolling.

Recipe 3: Sidebar layout

A fixed-width sidebar and a main area that takes the rest is the classic dashboard and documentation shape. Grid makes it two values:

.with-sidebar { display: grid; grid-template-columns: 14rem 1fr; }

On a phone, a 14rem sidebar would squash the content, so start with one column and add the sidebar when there is room. That is mobile-first, from the responsive design lesson.

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; } .with-sidebar { display: grid; gap: 12px; padding: 12px; } @media (min-width: 30rem) { .with-sidebar { grid-template-columns: 9rem 1fr; } } aside { background: #ecfdf5; border: 2px solid #059669; padding: 12px; } aside ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; } .content { background: #f8fafc; border: 2px solid #cbd5e1; padding: 12px; }</style><div class="with-sidebar"> <aside> <ul><li>Overview</li><li>Orders</li><li>Customers</li><li>Settings</li></ul> </aside> <main class="content"> <h2 style="margin-top:0">Orders</h2> <p>The main area takes every pixel the sidebar does not use.</p> </main></div>

Preview

The preview is roughly 400 to 600 pixels wide, so you are probably seeing two columns. Change 30rem to 60rem to see the phone layout, where the sidebar stacks on top. Then swap the column order to 1fr 9rem for a right-hand sidebar.

Recipe 4: The holy grail

The "holy grail" is an old web design name for a layout that was famously hard before Grid: header across the top, footer across the bottom, and three columns in between (navigation, main content, an extra side panel). With grid-template-areas you draw it as text.

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; } .grail { display: grid; min-height: 300px; gap: 6px; grid-template-columns: 1fr; grid-template-rows: auto auto 1fr auto auto; grid-template-areas: "header" "nav" "main" "ads" "footer"; } @media (min-width: 30rem) { .grail { grid-template-columns: 6rem 1fr 6rem; grid-template-rows: auto 1fr auto; grid-template-areas: "header header header" "nav main ads" "footer footer footer"; } } .grail > * { padding: 8px; border: 2px solid #334155; } header { grid-area: header; background: #fef3c7; } nav { grid-area: nav; background: #dcfce7; } main { grid-area: main; } aside { grid-area: ads; background: #e0e7ff; } footer { grid-area: footer; background: #e5e7eb; }</style><div class="grail"> <header>Header</header> <nav>Nav</nav> <main>Main content</main> <aside>Related</aside> <footer>Footer</footer></div>

Preview

The phone layout is a single column in source order. From 30rem the areas rearrange into three columns. The HTML never changes, which keeps the reading order logical for screen readers and keyboard users. Try moving ads to the left of main by editing only the middle string.

Quick check
In the holy grail layout, why does the middle row use 1fr in grid-template-rows: auto 1fr auto?
Need a nudge? A hint rules out a wrong answer.

Recipe 5: The auto-fit card grid

This one line is probably the most copied rule in modern CSS:

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr)); gap: 1rem; }

"Make as many columns as fit, each at least 16rem, and share the leftover space." No media queries. Drag the resize handle in the bottom right corner of the dashed box to see the column count change.

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; padding: 8px; } .frame { resize: horizontal; overflow: auto; min-width: 160px; max-width: 100%; border: 2px dashed #94a3b8; padding: 8px; } .cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(9rem, 100%), 1fr)); gap: 8px; } .card { display: flex; flex-direction: column; border: 2px solid #1e293b; padding: 10px; } .card h3 { margin: 0 0 4px; font-size: 1rem; } .card p { margin: 0 0 10px; color: #475569; } .card a { margin-top: auto; }</style><div class="frame"> <div class="cards"> <article class="card"><h3>Pancakes</h3><p>Fluffy, 20 minutes.</p><a href="#">Cook</a></article> <article class="card"><h3>Tomato soup</h3><p>A long description that makes this card taller than the others.</p><a href="#">Cook</a></article> <article class="card"><h3>Lemon tart</h3><p>Sharp and sweet.</p><a href="#">Cook</a></article> <article class="card"><h3>Bean chilli</h3><p>One pot.</p><a href="#">Cook</a></article> </div></div>

Preview

Two details are worth stealing. First, min(9rem, 100%) stops the grid overflowing when the container is narrower than the minimum. Second, each card is itself a Flexbox column with margin-top: auto on the link, so every "Cook" link sits at the bottom even when the descriptions differ in length. Grid outside, Flexbox inside.

On a page with little content, the footer should still sit at the bottom of the window, not float halfway up. The Flexbox lesson showed one version. The Grid version is just as short:

body { min-height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; margin: 0; }

Header, main, footer: the middle row takes the spare height. 100dvh is the dynamic viewport height, which tracks the visible area on phones as the browser toolbar slides in and out (plain 100vh can be taller than what is actually visible). It works in all modern browsers.

HTML & CSS
<style> body { margin: 0; min-height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; font-family: system-ui, sans-serif; } header, footer { padding: 12px 16px; background: #0f172a; color: white; } main { padding: 16px; }</style><header>Header</header><main>Only one short line of content here.</main><footer>Footer stays at the bottom</footer>

Preview

Try this: remove grid-template-rows and watch the three rows share the height equally, which pushes the footer up. Then put it back and add many paragraphs to main: the footer moves down naturally once the content is taller than the window.

Predict the outputHTML & CSS

The window is 800px tall. The header is 60px and the footer is 40px. With this rule, how tall (in px) is the main row when its content is only 100px tall?

body { min-height: 100dvh; display: grid; grid-template-rows: auto 1fr auto; }
Need a nudge? Hints walk you through it.

Recipe 7: Media object

The media object is an image (or avatar, or icon) on one side and text on the other: comments, notifications, chat messages, search results. It is a one-dimensional job, so Flexbox fits.

HTML & CSS
<style> body { font-family: system-ui, sans-serif; } .media { display: flex; gap: 12px; align-items: flex-start; max-width: 28rem; } .avatar { flex-shrink: 0; width: 48px; height: 48px; border-radius: 50%; background: #c7d2fe; } .media-body { min-width: 0; } .media-body p { margin: 4px 0 0; color: #475569; overflow-wrap: anywhere; }</style><div class="media"> <div class="avatar" aria-hidden="true"></div> <div class="media-body"> <strong>Sam replied</strong> <p>Tried the tomato soup with smoked paprika. Here is the link: https://example.com/a-very-long-url-that-would-otherwise-break-the-layout</p> </div></div>

Preview

The two protective lines matter. flex-shrink: 0 keeps the avatar a perfect circle when the text is long. min-width: 0 on the body lets the text shrink below its longest word, and overflow-wrap: anywhere breaks that long URL. Delete them one at a time to see what breaks. Add flex-direction: row-reverse to flip the avatar to the right, as chat apps do for your own messages.

Recipe 8: Split screen

Two halves side by side on large screens (a sign-up form next to an image, or a headline next to a product shot), stacked on phones. Grid with two equal columns:

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; } .split { display: grid; min-height: 280px; } @media (min-width: 30rem) { .split { grid-template-columns: 1fr 1fr; } } .split > * { display: grid; place-items: center; padding: 24px; } .left { background: #0f172a; color: white; } .right { background: #fde68a; }</style><div class="split"> <section class="left"><h2>Cook better tonight</h2></section> <section class="right"><p>Sign-up form goes here</p></section></div>

Preview

Each half is a centring grid from Recipe 1, so recipes nest. Change 1fr 1fr to 2fr 1fr for an uneven split.

Recipe 9: Full-bleed sections inside a centred container

Articles usually keep text in a narrow column for readability, but sometimes a photo or a coloured band should stretch edge to edge. The cleanest trick is a three-column grid where the middle column is the content width and the outer columns soak up the rest:

.article {
  display: grid;
  grid-template-columns: 1fr min(40rem, 100% - 2rem) 1fr;
}
.article > * { grid-column: 2; }
.article > .full-bleed { grid-column: 1 / -1; }

Every child goes in the middle column by default. A child with .full-bleed spans from the first line to the last. The 100% - 2rem keeps a 1rem gutter on each side on phones (math inside min() does not need calc()).

HTML & CSS
<style> body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.6; } .article { display: grid; grid-template-columns: 1fr min(22rem, 100% - 2rem) 1fr; row-gap: 12px; } .article > * { grid-column: 2; margin: 0; } .article > .full-bleed { grid-column: 1 / -1; } .band { background: #1e3a8a; color: white; padding: 24px; text-align: center; }</style><article class="article"> <h2>A week of soups</h2> <p>Body text stays in a comfortable column, centred on the page.</p> <div class="full-bleed band">This band runs edge to edge</div> <p>Then the text returns to the readable column without any extra wrappers.</p></article>

Preview

Remove the full-bleed class from the band and it snaps back into the column. Change 22rem to 14rem to see the column narrow while the band stays full width.

Match the pairs0/5 matched

Match each layout to the tool that usually builds it.

Choose an item, then its match on the other side.
Put it in order5 steps

You are building a dashboard page. Put the layout decisions in a sensible outside-in order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.

Putting recipes together

Real pages stack these recipes like building blocks. A dashboard might be: a sticky-footer grid on body, a sticky header (Flexbox inside), a sidebar layout in main, an auto-fit grid of stat cards, and media objects in an activity feed. No single rule is clever. Each element only arranges its own children, which is what keeps layout CSS easy to change later.

When a layout misbehaves, open devtools. Chrome, Firefox and Safari all show a small grid or flex badge next to containers in the Elements panel. Click it to draw the tracks, gaps and line numbers right on the page.

Key terms
place-items
Shorthand for align-items and justify-items; place-items: center centres grid children both ways.
holy grail layout
Header, footer and three columns (nav, main, side panel) between them; easy with grid-template-areas.
media object
An image or avatar beside a block of text, built with Flexbox and a non-shrinking image.
sticky footer
A footer that sits at the bottom of the window on short pages, using min-height: 100dvh and an auto 1fr auto grid.
dvh
Dynamic viewport height unit; 100dvh matches the visible height on phones as browser toolbars move.
full-bleed
An element that stretches edge to edge while the surrounding content stays in a centred column.