Sub topic 5
Building Common Responsive Layouts
This chapter assumes you know HTML, flexbox and grid, basic Tailwind utilities such as flex, p-4 and text-lg, and what a CSS media query is. You should also know that Tailwind is mobile-first: an unprefixed utility applies at every width, and a prefixed utility such as md: applies at that width and above.
The goal here is to turn that model into repeatable layout patterns. Each pattern starts with a layout that works on a phone, then adds prefixed utilities to change it as the screen grows.
The working method
Build every layout in the same order:
- Write the narrowest layout with no prefixes.
- Widen the browser until the layout looks stretched, cramped or wasteful.
- Add one prefixed utility at that width to fix it.
- Repeat until you reach the widest screen you support.
The default breakpoints are sm (640px), md (768px), lg (1024px), xl (1280px) and 2xl (1536px). You rarely need all of them. Most layouts need one or two changes, and adding more only makes the markup harder to read.
Pattern 1: A column grid that grows
The most common responsive pattern is a card grid. It shows one column on phones, two on tablets and three on desktops.
<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>Read this from left to right:
grid-cols-1has no prefix, so it applies at every width.sm:grid-cols-2takes over from 640px.lg:grid-cols-3takes over from 1024px.
At 700px both grid-cols-1 and sm:grid-cols-2 match. The sm: rule wins because Tailwind generates responsive variants after the plain utilities in the stylesheet. At 1100px all three match, and lg: wins for the same reason.
Writing grid-cols-1 is optional, since one column is the grid default. Many teams keep it anyway because it states the intent. It also gives you a visible starting point when you later add a middle breakpoint.
Edge case: uneven item counts
A three-column grid with four items leaves a lone card on the second row. That is normal grid behavior, not a bug. If it looks wrong, pick a column count that suits your content, or use auto-fit with a minimum width so the browser chooses the count itself:
<div class="grid grid-cols-[repeat(auto-fit,minmax(16rem,1fr))] gap-4">
<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 class="rounded border p-4">Four</div>
</div>This version needs no breakpoints at all. Use it when you want fluid columns. Use breakpoint prefixes when you want exact column counts at known widths.
Pattern 2: Stacked on mobile, row on desktop
Navigation bars and toolbars usually stack vertically on a phone and sit in a row on wider screens. The switch is a change of flex-direction.
<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>Here flex-col is the base. From md, flex-row replaces it. Two other changes ride along:
md:items-centeraligns the links on the cross axis. In a column,items-*controls horizontal alignment, so you rarely want to center there. In a row it controls vertical alignment, which is what you want.md:gap-6widens the spacing, because horizontal links need more room between them than stacked ones.
Notice that every prefixed utility on this element is tied to the same breakpoint. Keeping related changes at one breakpoint makes the layout switch in a single step, so you never see a half-converted state at some in-between width.
Showing and hiding at a breakpoint
A related pattern shows a compact control on mobile and a full one on desktop. Pair hidden with a display utility:
<button class="md:hidden">Menu</button>
<ul class="hidden md:flex md:gap-6">
<li><a href="/docs">Docs</a></li>
<li><a href="/pricing">Pricing</a></li>
</ul>The button is visible by default and hidden from md up. The list is hidden by default and becomes a flex container from md up. Two points to remember:
hiddensetsdisplay: none, so you must restore a display value withmd:flex,md:blockor similar. Writing onlymd:gap-6would leave the list hidden.- Hidden elements are removed from the accessibility tree too. If the mobile menu button toggles something, wire up the toggle with JavaScript and
aria-expanded. Tailwind only handles the styling.
Pattern 3: Sidebar and content
A page with a sidebar often puts it above the content on mobile and beside the content on desktop. A grid handles this well:
<div class="grid grid-cols-1 gap-6 lg:grid-cols-[16rem_1fr]">
<aside class="rounded border p-4">Sidebar</aside>
<main class="min-w-0">Main content</main>
</div>On a phone, the single column stacks the sidebar over the content. From lg, the arbitrary value grid-cols-[16rem_1fr] creates a fixed 16rem sidebar and a flexible content column. Underscores in arbitrary values stand for spaces.
Note the min-w-0 on the content column. A grid item's minimum width defaults to its content size, so a wide table or a long code line can push the layout past the screen. Setting min-w-0 lets the column shrink and lets inner overflow-x-auto work as expected.
If the sidebar should come second on mobile but first on desktop, use order:
<div class="flex flex-col lg:flex-row">
<main class="lg:order-2 lg:flex-1">Main content</main>
<aside class="lg:order-1 lg:w-64">Sidebar</aside>
</div>Be careful with order. It changes visual order only. Keyboard focus and screen readers still follow the HTML source order, so a large mismatch between the two confuses users. Prefer arranging the HTML in the right reading order and avoid order when you can.
Pattern 4: Responsive typography and spacing
Text and spacing also need to scale. A heading that fits on a laptop can fill an entire phone screen.
<h1 class="text-2xl font-bold md:text-4xl lg:text-5xl">Responsive heading</h1>The base size serves the smallest screens, and each breakpoint raises it. Apply the same idea to padding and gaps:
<section class="px-4 py-8 md:px-8 md:py-16 lg:px-12">
<p class="max-w-prose">Body text stays readable at any width.</p>
</section>The max-w-prose utility caps line length at about 65 characters. Long lines are hard to read, so this one unprefixed class does more for wide screens than extra breakpoints would.
Pattern 5: Switching a component's internal layout
A media object, such as an avatar next to text, often stacks on phones and sits side by side on tablets:
<article class="flex flex-col items-start gap-4 sm:flex-row sm:items-center">
<img class="size-16 rounded-full sm:size-20" src="/avatar.png" alt="Profile photo">
<div>
<h2 class="text-lg font-semibold">Ada Lovelace</h2>
<p class="text-sm text-gray-600">Mathematician</p>
</div>
</article>Unlike the earlier patterns, this one changes at sm, because the component is small and needs little room before it fits in a row. Choose the breakpoint from the component's own needs, not from a fixed idea of what "tablet" means.
Choosing a breakpoint
Beginners often pick a breakpoint by device name. A better rule is to pick it by content. Resize the window and note the width at which the layout starts to break, then use the nearest breakpoint at or below that width.
| Symptom as you widen the window | Typical fix |
|---|---|
| Single-column cards look stretched | Add `sm:grid-cols-2` |
| Links wrap onto a second line | Switch to `md:flex-row` earlier, or collapse into a menu |
| Text lines exceed about 75 characters | Add `max-w-prose` or `max-w-3xl` |
| A sidebar squeezes the content | Move the side-by-side switch to a larger breakpoint |
Pitfalls specific to layouts
- Using
sm:for phones.sm:means 640px and wider, not small screens. Put the phone layout in the unprefixed utilities. - Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">in the document head, mobile browsers report a desktop-sized width, and none of these patterns will switch as expected. - Prefixing everything. If a utility has the same value at every width, leave it unprefixed. Repeating
md:p-4next top-4adds noise and no behavior. - Undoing a base utility. To remove a base style at a breakpoint you need an explicit counter value, such as
flex-col md:flex-roworhidden md:block. There is no "remove" prefix. - Dynamic class names. Tailwind finds classes by scanning your source for complete strings. A class built as `
md:grid-cols-${n}` is never generated. Write out the full class names, or map values to complete strings.
Putting it together
This page layout combines several patterns: a nav that switches direction, a sidebar grid, and a card grid inside the main column.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive layout</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="text-gray-900">
<header class="border-b px-4 py-4 md:px-8">
<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>
</header>
<div class="grid grid-cols-1 gap-6 px-4 py-6 md:px-8 lg:grid-cols-[16rem_1fr]">
<aside class="rounded border p-4">Sidebar</aside>
<main class="min-w-0">
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 xl: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>
</main>
</div>
</body>
</html>Save it as an HTML file and open it in a browser. Drag the window edge and watch the layout change at 640px, 768px, 1024px and 1280px. The card grid is nested in a column that is narrower than the viewport at lg because the sidebar takes 16rem. That is why it waits until xl for three columns. When a component's available width depends on its parent rather than the screen, viewport breakpoints become awkward. Container queries, covered later in this tutorial, solve that case.
Exercise
A card grid uses grid grid-cols-3 md:grid-cols-1. The author wants three columns on desktop and one on phones, but the layout shows three columns on phones. What is wrong, and how do you fix it?
Show answer
The classes are written desktop-first. The unprefixed grid-cols-3 applies at every width, and md:grid-cols-1 forces one column from 768px upward, which is the reverse of the intent. Write the phone layout as the base and add the wider layout with a prefix: grid grid-cols-1 md:grid-cols-3.
Exercise
You want a menu list hidden on phones and shown as a horizontal row with spacing from md up. Write the classes for the <ul>.
Show answer
hidden md:flex md:gap-6. The base hidden removes the list on small screens, md:flex restores display as a flex row at 768px and wider, and md:gap-6 adds the spacing. Using md:gap-6 alone would leave the list hidden.
Exercise
A two-column layout with a long code block inside the main column pushes the page wider than the screen on a phone. Which utility on the grid item is the usual fix, and why?
Show answer
Add min-w-0 to the grid item. Grid items default to a minimum width equal to their content, so the long line prevents the column from shrinking. With min-w-0 the column can shrink, and an overflow-x-auto wrapper around the code block can then scroll inside it.