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.
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.
Things must line up across rows and columns: a page shell, a card gallery. Use Grid.
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.
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.
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.
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.
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.
1fr in grid-template-rows: auto 1fr auto?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.
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.
Recipe 6: Sticky footer
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.
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.
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; }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.
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:
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()).
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 each layout to the tool that usually builds it.
You are building a dashboard page. Put the layout decisions in a sensible outside-in order.
- Tap the steps below in the right order.
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.
- 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.