Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 37 ยท Layout with Flexbox and Grid

Page Layout Patterns

Put flexbox and grid together: centered containers, sticky footers, sidebar and dashboard layouts, equal-height cards, media objects and split screens in Tailwind.

You now know the individual tools: display, position, flexbox and grid. Real pages are made from a small set of recurring patterns. This lesson collects the ones you will use again and again, each built from just a few classes. Learn them as recipes you can adapt, and layout stops being intimidating.

Pattern 1: the centered page container

Almost every page has content that is centered and does not stretch too wide on large monitors:

HTML
<div class="mx-auto max-w-5xl px-4 sm:px-6 lg:px-8"> ... </div>

mx-auto centers, max-w-5xl caps the width at 64rem, and the horizontal padding gives breathing room on narrow screens. Use this wrapper inside each full-width section (header, hero, footer), so the backgrounds can extend edge to edge while the content stays aligned.

When a page has little content, the footer should still sit at the bottom of the window. Make the page a flex column that is at least as tall as the viewport, and let the main area absorb the extra space:

HTML
<body class="flex min-h-dvh flex-col">
  <header>...</header>
  <main class="flex-1">...</main>
  <footer>...</footer>
</body>

flex-1 gives main all the free space. This is the most reliable way, with no absolute positioning.

Pattern 3: sidebar and dashboard layout

A dashboard has a fixed-width sidebar, a top bar, and a main area. CSS grid describes it directly. The next TryIt is a complete one. Make the preview narrower to see how it behaves: the sidebar hides below the md breakpoint (768px), which uses a responsive prefix explained in chapter 5.

Dashboard layout with grid

The outer grid has two rows (auto for the header, 1fr for the rest) and, on md and wider, two columns. The header spans both columns with md:col-span-2. The main area has overflow-y-auto so only it scrolls, not the whole page.

Pattern 4: equal-height cards with bottom-aligned buttons

Cards in a row stretch to the same height automatically (grid and flex children stretch). The catch is that their inner contents differ in length, so buttons end at different heights. Make each card a flex column and push the button down with mt-auto:

Equal cards, aligned buttons

All three "Choose" links sit on the same baseline at the bottom, although the middle card has more text.

Pattern 5: the media object

An image or avatar on one side with text beside it is everywhere (comments, search results, notifications):

HTML
<div class="flex gap-4">
  <img class="size-12 shrink-0 rounded-full" src="..." alt="">
  <div class="min-w-0">
    <p class="font-semibold">Name</p>
    <p class="text-slate-600">The text wraps beside the picture.</p>
  </div>
</div>

shrink-0 keeps the avatar from being squeezed and min-w-0 lets long text wrap. For a vertical layout on phones and horizontal on larger screens, use flex-col sm:flex-row (chapter 5).

Pattern 6: overlay and cover

To place something over a parent (loading spinners, image captions, modal backdrops), use a positioned overlay: relative parent plus absolute inset-0 child. For a modal that covers the viewport, use fixed inset-0 grid place-items-center bg-black/50.

Pattern 7: split screen

A login page with a form on one half and a picture on the other:

HTML
<div class="grid min-h-dvh lg:grid-cols-2">
  <div class="grid place-items-center p-8">  <!-- form -->  </div>
  <div class="hidden bg-indigo-600 lg:block"> <!-- brand image --> </div>
</div>

Choosing a method, fast

Layout Reach for
Row of things, items of different widths flex
Equal columns or cards grid grid-cols-*
Page frame (header, sidebar, main) grid with templates
Center a single thing grid place-items-center or flex items-center justify-center
Sticky footer flex min-h-dvh flex-col + flex-1
Anything over something else relative + absolute inset-0

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How do you make a footer stay at the bottom of a short page?

    A min-height flex column lets main (flex-1) absorb the extra space, pushing the footer down while still growing with content.
  2. 2. Which utility aligns the button at the bottom of cards with different amounts of text?

    In a flex column, an auto top margin consumes the free space and pushes the button to the card's bottom.
  3. 3. In the dashboard grid, what makes only the main area scroll?

    The grid has a fixed height with a 1fr row, and main has overflow-y-auto, so scrolling stays inside it.
  4. 4. Why use px-4 inside max-w-5xl mx-auto?

    When the screen is narrower than the max width, the container fills it entirely, and horizontal padding keeps text off the edges.

Summary

  • Wrap content in mx-auto max-w-* px-*; extend backgrounds on the outer element.
  • Sticky footer: flex min-h-dvh flex-col with flex-1 on main.
  • Dashboards and page frames are grids with a sidebar column, col-span-* for the header and overflow-y-auto on the scrolling area.
  • Use mt-auto for bottom-aligned card actions, shrink-0 plus min-w-0 for media objects, and relative plus absolute inset-0 for overlays.

Next: Fonts, Text and Typography Utilities