Sub topic 5
Responsive design with breakpoint variants
This chapter assumes you can read React and JSX, know how CSS media queries work, and have a Vite project with Tailwind v4 already set up. You should also know the core spacing, typography and color utilities. Here you will learn how Tailwind turns a class prefix into a media query, how to think mobile-first, and where breakpoint variants stop being the right tool.
How a breakpoint variant works
A breakpoint variant is a prefix such as md: placed in front of a utility. The prefix wraps the utility's CSS in a media query. Tailwind v4 ships with five default breakpoints:
| Prefix | Media query | Width |
|---|---|---|
| `sm:` | `@media (width >= 40rem)` | 640px |
| `md:` | `@media (width >= 48rem)` | 768px |
| `lg:` | `@media (width >= 64rem)` | 1024px |
| `xl:` | `@media (width >= 80rem)` | 1280px |
| `2xl:` | `@media (width >= 96rem)` | 1536px |
Every default is a min-width query, so md:flex means "use display: flex at 768px and wider." The variant never turns off at larger sizes. A utility with no prefix applies at every width.
The breakpoints are in rem, so they scale with the user's browser font size. A user who raises their default text size reaches each breakpoint at a wider screen.
Mobile-first means unprefixed is the smallest layout
The most common mistake is reading sm: as "on small screens." In Tailwind, sm: means "from the small breakpoint upward." Unprefixed classes are your mobile layout. Prefixed classes add changes as the screen grows.
export function Hero() {
return (
<section className="px-4 py-8 text-center sm:px-8 md:py-16 lg:px-16 lg:text-left">
<h1 className="text-2xl font-bold sm:text-3xl lg:text-5xl">
Ship interfaces faster
</h1>
<p className="mt-4 text-base text-gray-600 lg:max-w-xl lg:text-lg">
Utility classes keep the styles next to the markup.
</p>
</section>
)
}Read it from the smallest width up. On a phone, the section has px-4, py-8 and centered text. At 640px the horizontal padding becomes px-8. At 768px the vertical padding becomes py-16. At 1024px the horizontal padding grows again and the text aligns left.
If you write sm:text-center expecting a phone-only style, the text will be centered on every screen from 640px up. To style only the smallest screens, put the style on the unprefixed class and override it at the breakpoint where it should stop.
Changing layout direction
Switching between a stacked and a side-by-side layout is the most common responsive change. Combine flex-col with a prefixed flex-row:
export function FeatureRow({
title,
body,
}: {
title: string
body: string
}) {
return (
<div className="flex flex-col gap-4 md:flex-row md:items-center md:gap-8">
<div className="h-40 w-full rounded-lg bg-gray-200 md:w-1/3" />
<div className="md:w-2/3">
<h3 className="text-lg font-semibold text-gray-900">{title}</h3>
<p className="mt-2 text-gray-600">{body}</p>
</div>
</div>
)
}Two things are worth noting. The md:items-center class is only useful once the direction is a row, so it can be prefixed on its own. And w-full on the image block is needed on mobile because a column flex child would otherwise size to its content.
For grids, the same idea applies to column counts. The CardGrid below uses one column by default, two from sm, and three from lg:
export function CardGrid({ children }: { children: React.ReactNode }) {
return (
<div className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
{children}
</div>
)
}The grid-cols-1 is not strictly required, since a grid with no template already has one implicit column. Keeping it makes the intent visible to the next reader.
Showing and hiding elements
You can swap one element for another by pairing a hidden default with a prefixed display class:
export function Navigation({ links }: { links: string[] }) {
return (
<nav className="flex items-center justify-between p-4">
<span className="font-bold">Acme</span>
<ul className="hidden gap-6 md:flex">
{links.map((link) => (
<li key={link}>
<a href={`#${link}`} className="text-gray-700 hover:text-gray-900">
{link}
</a>
</li>
))}
</ul>
<button className="rounded-md border px-3 py-1 md:hidden">Menu</button>
</nav>
)
}The link list uses hidden md:flex: hidden by default, flex from 768px. The menu button uses md:hidden: visible by default, hidden from 768px. Note that hidden is display: none, so the element is removed from the accessibility tree too. This is right for a swap like this one. It is wrong if you only want an element to look different, because screen reader users on a small screen would lose the content.
Targeting a range or only smaller screens
Tailwind also has max-* variants that use max-width queries: max-sm:, max-md:, max-lg:, max-xl: and max-2xl:. Stack a min variant and a max variant to target a band of widths:
export function TabletNote() {
return (
<p className="hidden md:max-lg:block rounded bg-yellow-100 p-3 text-sm">
This note only appears between 768px and 1023px.
</p>
)
}Here md:max-lg:block applies only when the viewport is at least md and below lg. Reach for max-* variants sparingly. If you find yourself writing many of them, your base layout is probably not mobile-first, and the class lists will be harder to follow.
For a one-off width that is not in the theme, use an arbitrary value variant such as min-[900px]:grid-cols-4 or max-[600px]:hidden. Arbitrary values are covered in a later chapter. Prefer a named breakpoint when the width appears more than once.
Adding or replacing breakpoints
Breakpoints are theme variables. Define --breakpoint-* variables in @theme and Tailwind generates the matching variants:
@import "tailwindcss";
@theme {
--breakpoint-xs: 30rem;
--breakpoint-3xl: 120rem;
}This gives you xs: and 3xl: variants, and the matching max-xs: and max-3xl: forms, alongside the defaults. To replace the whole set, reset the namespace first:
@import "tailwindcss";
@theme {
--breakpoint-*: initial;
--breakpoint-tablet: 40rem;
--breakpoint-laptop: 64rem;
--breakpoint-desktop: 80rem;
}After a reset, sm: and md: no longer exist. Any class that still uses them is silently ignored, because Tailwind does not generate CSS for an unknown variant. If you reset breakpoints in an existing project, search the source for the old prefixes.
Keep breakpoint units consistent. If you mix px and rem values, Tailwind may sort the generated CSS in the wrong order, and a larger breakpoint could then be overridden by a smaller one.
Container queries: responding to the parent, not the viewport
Breakpoint variants look at the viewport. A component placed in a narrow sidebar on a wide screen will still see a wide viewport and choose its large layout. Tailwind v4 includes container query variants for this case. Mark the parent with @container, then use @-prefixed variants on the children:
export function StatCard({ label, value }: { label: string; value: string }) {
return (
<div className="@container rounded-lg border border-gray-200 p-4">
<div className="flex flex-col gap-1 @sm:flex-row @sm:items-baseline @sm:justify-between">
<span className="text-sm text-gray-600">{label}</span>
<span className="text-2xl font-semibold text-gray-900">{value}</span>
</div>
</div>
)
}The @sm: variants apply when the nearest @container ancestor is at least 24rem wide, no matter how wide the browser window is. Container sizes are a separate scale from viewport breakpoints. Use viewport variants for page-level structure and container variants for components that get reused in different places.
Common errors
- Styling mobile with
sm:. Put mobile styles on unprefixed classes and addsm:and above for larger screens. - Missing viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1.0" />inindex.html, phones render a zoomed-out desktop page and no breakpoint behaves as expected. Vite's default template includes it. - Overriding in the wrong order. Writing
lg:p-8 md:p-4still works, because Tailwind orders the generated CSS by breakpoint and not by class order in the string. For readability, list classes from small to large anyway. - Forgetting to restate a value. A
md:change persists atlgand above. If you wantlgto return to the original value, you must set it explicitly, for examplep-4 md:p-8 lg:p-4. - Building class names from fragments. `
${size}:flex` will not be detected. Write the full class names, as covered in the chapters on class detection and dynamic classes.
Exercises
Exercise
A card has the classes p-4 sm:p-6 lg:p-10. What padding does it have at a viewport width of 800px, and why?
Show answer
It has p-6 (1.5rem). At 800px the viewport is above the sm breakpoint (640px) and below lg (1024px). The unprefixed p-4 is overridden by sm:p-6, and lg:p-10 is not active yet.
Exercise
Write a class list for a div that shows a single column below 768px, two columns from 768px, and four columns from 1280px, with a gap-6 at all sizes.
Show answer
grid grid-cols-1 gap-6 md:grid-cols-2 xl:grid-cols-4. The single column is the unprefixed default, md: adds two columns, and xl: raises it to four. The lg breakpoint is skipped because two columns carry over to it.
Exercise
You add --breakpoint-3xl: 120rem; to @theme. Which variant names does this create, and what happens to md: and lg:?
Show answer
It creates 3xl: and max-3xl:. The default breakpoints, including md: and lg:, stay available. They are only removed if you reset the namespace with --breakpoint-*: initial;.