Sub topic 8
Responsive design with breakpoint prefixes
This chapter assumes you know the CSS box model, media queries at a basic level, and the flexbox and grid utilities from the previous chapter. You should also have a working Tailwind setup.
The mobile-first model
Tailwind makes a design responsive with breakpoint prefixes. A prefix such as md: in front of a utility means "apply this utility at the md width and above". It compiles to a min-width media query.
A utility with no prefix applies at every width. So you write the small-screen design first, then add prefixed utilities that change it as the screen grows.
The default breakpoints are:
| Prefix | Minimum width | Media query |
|---|---|---|
| `sm` | 640px (40rem) | `@media (width >= 40rem)` |
| `md` | 768px (48rem) | `@media (width >= 48rem)` |
| `lg` | 1024px (64rem) | `@media (width >= 64rem)` |
| `xl` | 1280px (80rem) | `@media (width >= 80rem)` |
| `2xl` | 1536px (96rem) | `@media (width >= 96rem)` |
A responsive grid
This grid shows one column on phones, two columns from sm, and three from lg.
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div class="rounded bg-slate-100 p-4">One</div>
<div class="rounded bg-slate-100 p-4">Two</div>
<div class="rounded bg-slate-100 p-4">Three</div>
</div>Read it from left to right:
grid-cols-1applies everywhere.- At 640px and wider,
sm:grid-cols-2overrides it. - At 1024px and wider,
lg:grid-cols-3overrides that.
The sm: rule is still active at lg. The lg: rule wins because it comes later in the generated CSS and has the same specificity.
Common mistakes
The most common error is treating sm: as "small screens only". It means "640px and wider", so sm:text-center centers text on tablets and desktops too. To style phones, use the unprefixed utility.
The second error is designing for desktop first. A class list like lg:flex-row flex-col is fine. A list that starts with desktop styles and tries to undo them on small screens is hard to read and needs more classes.
The third error is using breakpoints to hide things by guesswork. For example, hidden md:block hides an element below 768px and shows it at 768px and wider. Check that the content is still reachable on small screens some other way.
Targeting a range
You can stack a variant with a max- prefix to limit a style to a range. max-md: applies below 768px. Combining md: and max-xl: limits a style to widths from 768px up to, but not including, 1280px.
<p class="md:max-xl:text-lg">
Larger text only between the md and xl breakpoints.
</p>Custom breakpoints
In Tailwind v4, you add a breakpoint by defining a variable in the --breakpoint-* namespace inside @theme. This creates a matching prefix.
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
}You can now write 3xl:grid-cols-4. Customizing the theme is covered in a later chapter.
Combining with other utilities
Breakpoint prefixes work with nearly every utility, so one element can change several properties at once. This navigation stacks vertically on phones and sits in a row from md.
<nav class="flex flex-col gap-2 p-4 md:flex-row md:items-center md:justify-between md:gap-6">
<a href="/" class="text-lg font-bold">Brand</a>
<a href="/docs">Docs</a>
<a href="/blog">Blog</a>
</nav>Exercise
A paragraph has the classes text-base lg:text-xl md:text-lg. What font size does it use at a width of 800px, and at 1100px?
Show answer
At 800px it uses text-lg, because md: (768px and up) applies and lg: (1024px and up) does not. At 1100px it uses text-xl, because both md: and lg: apply and the lg: rule comes later in the generated CSS. Tailwind orders its generated CSS by breakpoint, so the order in which you write the classes does not matter.