Sub topic 5
Building common responsive layouts
This chapter assumes you know flexbox and grid, the mobile-first model, and the default breakpoints: sm at 640px, md at 768px, lg at 1024px, xl at 1280px and 2xl at 1536px. The breakpoint prefixes you already know are the tools here. This chapter shows how to combine them into layouts you will build again and again.
The working model
Every layout below follows the same recipe:
- Write the small-screen layout with unprefixed utilities.
- Add a prefixed utility at the first width where the layout needs to change.
- Add more prefixes only if a wider screen needs another change.
Prefixed utilities apply at that width and above, so each step overrides the one before it. You never write a rule to "undo" a larger layout.
Pattern 1: A column grid that grows
A card list is the most common case. It shows one column on a phone, two on a small tablet and three on a laptop.
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div class="rounded border p-4">One</div>
<div class="rounded border p-4">Two</div>
<div class="rounded border p-4">Three</div>
</div>grid-cols-1 has no prefix, so it applies at every width. At 640px, sm:grid-cols-2 replaces it. At 1024px, lg:grid-cols-3 replaces that. The gap-4 stays the same throughout.
Pattern 2: A navigation that stacks, then lines up
On a narrow screen, links stack in a column. From md upward they sit in a row.
<nav class="flex flex-col gap-2 md:flex-row md:items-center md:gap-6">
<a href="/">Home</a>
<a href="/docs">Docs</a>
<a href="/pricing">Pricing</a>
</nav>Three utilities change at md:
md:flex-rowswitches the direction.md:items-centeraligns the links vertically once they share a row.md:gap-6widens the spacing.
Each prefixed utility is independent. You can change one property at a breakpoint and leave the others alone.
Pattern 3: Type that scales
Text that suits a phone looks timid on a desktop. Raise the size at each step.
<h1 class="text-2xl font-bold md:text-4xl lg:text-5xl">Responsive heading</h1>The base text-2xl serves the smallest screens. font-bold is never overridden, so it holds everywhere.
Pattern 4: Sidebar and content
A sidebar usually sits above the content on mobile and beside it on wider screens. Combine flex direction with fixed and flexible widths.
<div class="flex flex-col gap-6 lg:flex-row">
<aside class="lg:w-64 lg:shrink-0">Sidebar</aside>
<main class="min-w-0 flex-1">Content</main>
</div>Below lg, the column layout lets both children fill the width. At lg, the row layout begins. The sidebar gets a fixed 64-unit width (w-64, which is 16rem) and shrink-0 stops it from being squeezed. The main area uses flex-1 to take the remaining space. min-w-0 lets long content such as code blocks shrink instead of forcing the page to scroll sideways.
Pattern 5: Show and hide
You can also show different elements at different widths. The usual approach is to hide something by default and reveal it at a breakpoint.
<button class="md:hidden">Menu</button>
<ul class="hidden md:flex md:gap-6">
<li>Docs</li>
<li>Pricing</li>
</ul>The menu button shows on small screens and disappears at md. The link list is hidden by default and appears as a flex row at md. Hidden elements stay in the HTML, so screen readers and search engines may still process them in some cases. Use this for presentation, not for content that must never be read.
Choosing where to break
Pick a breakpoint by looking at your content, not at device names. Resize the browser until the layout looks cramped or too sparse, then add a prefix at that point. A three-column grid is a good example. It needs enough room for each card to read well, so lg often fits better than md.
| Layout need | Typical starting point |
|---|---|
| Two-column card grid | `sm` or `md` |
| Three or four columns | `lg` or `xl` |
| Navigation in a row | `md` |
| Sidebar beside content | `lg` |
These are starting points. Your content decides.
Common errors in layouts
- Layout looks the same on a phone. The viewport meta tag is probably missing, so the browser pretends the screen is about 980px wide.
- A grid never changes. Check for a typo such as
sm:grid-col-2, which is not a Tailwind class. The correct name isgrid-cols-2. - Two utilities conflict. If you write
p-4 md:p-8 lg:p-4, the layout goes back to small padding atlg. That is valid but usually unintended. - Content overflows sideways. Add
min-w-0to flex children that hold wide content.
Exercise
Write the classes for a container that shows one column by default, two columns from 768px, and four columns from 1280px, with a gap of 1rem throughout.
Show answer
Use grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4. grid-cols-1 applies at every width, md: raises it to two columns at 768px, and xl: raises it to four at 1280px. gap-4 is 1rem and is never overridden.
Exercise
A header has <ul class="flex md:hidden">. At which widths is the list visible, and why?
Show answer
It is visible below 768px. flex applies at every width, but md:hidden sets display: none from 768px upward and overrides it.