Sub topic 2
Applying Breakpoint Prefixes
Assume you know basic HTML and CSS, including flexbox, grid, and media queries. Tailwind uses breakpoint prefixes to apply a utility at a minimum viewport width.
Applying breakpoint prefixes
Tailwind is mobile-first. An unprefixed utility applies at every width. A prefixed utility applies from its breakpoint upward.
| Prefix | Minimum width |
|---|---|
| `sm:` | 640px |
| `md:` | 768px |
| `lg:` | 1024px |
| `xl:` | 1280px |
| `2xl:` | 1536px |
This grid starts with one column. At 640px it uses two columns, and at 1024px it uses three.
<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>The browser applies the last matching utility in the responsive cascade. You can therefore build a progression from a simple narrow layout to a wider one.
Building common responsive layouts
Stack navigation links on narrow screens, then switch to a row on wider screens:
<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>You can also scale typography:
<h1 class="text-2xl font-bold md:text-4xl lg:text-5xl">
Responsive heading
</h1>Common mistakes and how to avoid them
sm: does not mean “small screens.” It means “640px and wider.” For narrow-screen defaults, use an unprefixed utility. Then override it at larger widths.
Also check that the document includes the viewport setting. Without it, mobile browsers may use a wider layout viewport and make breakpoint testing misleading.
<meta name="viewport" content="width=device-width, initial-scale=1">Test at the breakpoint itself and just below it. If a layout changes unexpectedly, inspect which prefixed utilities match and whether a later class overrides the intended style.
Exercise
What does md:grid-cols-2 mean when used with grid-cols-1?
Show answer
The grid has one column by default and changes to two columns at 768px and wider.