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. It covers how Tailwind turns a prefix such as md: into a media query, why the approach is mobile-first, and the mistakes that most often break responsive layouts.
How breakpoint prefixes work
Tailwind has no separate "responsive mode". Any utility can be given a breakpoint prefix, and the utility then applies only when the viewport is at least that wide.
<div class="text-sm md:text-lg">Resizes with the viewport</div>This produces font-size: 0.875rem at all widths and replaces it with the larger size once the viewport reaches the md breakpoint. Each prefix compiles to a min-width media query.
The default breakpoints are:
| Prefix | Minimum width | Generated media query |
|---|---|---|
| `sm` | 40rem (640px) | `@media (width >= 40rem)` |
| `md` | 48rem (768px) | `@media (width >= 48rem)` |
| `lg` | 64rem (1024px) | `@media (width >= 64rem)` |
| `xl` | 80rem (1280px) | `@media (width >= 80rem)` |
| `2xl` | 96rem (1536px) | `@media (width >= 96rem)` |
The values are in rem, so the breakpoints scale with the user's browser font size. Check the version you use, because older releases wrote these in pixels.
Mobile-first: unprefixed means "all sizes"
The most common mistake is reading sm: as "on small screens". It means "at the small breakpoint and wider". Unprefixed utilities are the base layer and apply everywhere. Prefixed utilities override them as the screen grows.
Design for the narrowest screen first. Write the base styles without prefixes, then add prefixes only where the layout changes.
<!-- Mistake: this hides the text on phones only if you expect sm: to mean "small" -->
<p class="sm:text-center">Centered from 640px up, left-aligned below</p>
<!-- Intent: centered on phones, left-aligned from 640px up -->
<p class="text-center sm:text-left">Centered on phones, left-aligned from 640px up</p>Both examples are valid HTML. Only the second one matches the stated intent of targeting small screens. To target small screens, put the style in the unprefixed base and override it at a larger breakpoint.
A responsive grid
The grid example below shows the full model. The base is one column. At sm it becomes two columns, and at lg it becomes three.
<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>The prefixes cascade upward. At 900px wide, sm:grid-cols-2 applies and lg:grid-cols-3 does not, so the grid has two columns. At 1100px, both match. Both rules have the same specificity, and Tailwind emits the larger breakpoints later in the stylesheet, so lg: wins.
Changing layout direction
Breakpoints are most useful for switching a layout's structure. A stack on a phone can become a row on a larger screen.
<section class="flex flex-col gap-4 md:flex-row md:items-center md:justify-between">
<h2 class="text-xl font-semibold">Pricing</h2>
<a href="/signup" class="rounded-lg bg-indigo-600 px-4 py-2 text-white">Sign up</a>
</section>On narrow screens the heading and link stack vertically. From md up, flex-row puts them on one line, and items-center and justify-between align and spread them. Notice that md:items-center and md:justify-between are prefixed too. Without the prefix, they would also apply to the column layout, where justify-between acts on the vertical axis.
Showing and hiding elements
Visibility is a frequent use of breakpoints. Start with the element hidden, then reveal it, or the reverse.
<button class="md:hidden">Menu</button>
<ul class="hidden gap-4 md:flex">
<li><a href="/docs">Docs</a></li>
<li><a href="/blog">Blog</a></li>
</ul>The menu button shows below md and disappears from md up. The link list is hidden by default and becomes flex from md up. Note that hidden is display: none, so md:flex must replace it. Writing only md:gap-4 would leave the list hidden.
Stacking prefixes with other variants
Breakpoint prefixes combine with the state variants covered in the next chapter. The order is read left to right, and every part must match for the style to apply.
<a class="text-slate-700 md:hover:text-indigo-600" href="/docs">Docs</a>Here the hover color applies only when the viewport is at least md wide and the pointer is over the link. On a phone, hovering does nothing.
Targeting a range or a maximum
Because every breakpoint is a minimum, the style continues to apply on larger screens. If you need a style to stop at a certain width, you have two options.
- Override it at the next breakpoint, such as
md:hiddenafter a baseblock. - Use a
max-*variant, which generates amax-widthquery.
<div class="max-md:hidden">Visible from md and wider</div>
<div class="md:max-lg:flex hidden">Flex only between md and lg</div>max-md: applies below the md breakpoint. Stacking md:max-lg: creates a range: at least md and below lg. In the first line, max-md:hidden hides the element on narrow screens. This is the opposite of what the line's text claims if you read it carelessly. The element is hidden below md, so it is visible from md upward. The text is correct.
Use max-* sparingly. A design written mobile-first with overrides is usually easier to read than one that mixes minimum and maximum conditions.
Custom breakpoints
You can change or add breakpoints in the theme. In Tailwind v4, breakpoints are theme variables in the --breakpoint-* namespace.
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
--breakpoint-xs: 30rem;
}This creates 3xl: and xs: prefixes. Variants are sorted by their values, so xs is placed before sm in the cascade. The next chapters on theming cover @theme in more detail. If you only need a width once, an arbitrary variant avoids touching the theme.
<div class="min-[900px]:flex max-[600px]:hidden">One-off breakpoints</div>Container queries
Viewport breakpoints respond to the whole window. A component placed in a narrow sidebar on a wide screen still sees a wide viewport, so it may choose the wrong layout. Container queries respond to the size of a parent element instead.
<div class="@container">
<div class="grid grid-cols-1 gap-2 @md:grid-cols-2">
<div class="rounded bg-slate-100 p-4">A</div>
<div class="rounded bg-slate-100 p-4">B</div>
</div>
</div>The parent is marked with @container. The @md: prefix then applies when that container is at least the md container size wide, regardless of the window. These variants are built in from v4. In v3 they need a plugin. Use viewport prefixes for page-level layout and container queries for reusable components.
Common errors
- Reading
sm:as "small screens only". It means "small and up". Put phone styles in the unprefixed base. - Prefixing only half of a layout change. Changing
md:flex-rowwithout the relatedmd:items-centerleaves alignment tuned for the column layout. - Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">, mobile browsers render a desktop-width page and the breakpoints never match as you expect. - Overriding with a rule that does not exist.
hidden md:blockworks.hidden md:visibledoes not hide anything, becausehiddensetsdisplay: none, andvisiblechangesvisibility, which does not undo it. - Building prefixed class names dynamically. `
${size}:grid-cols-2` is invisible to Tailwind's scanner. Write full class names, as covered in the chapter on dynamic classes. - Too many breakpoints. Specifying a value at every breakpoint makes markup long and hard to maintain. Use as few as the design needs, and let larger breakpoints inherit.
Putting it together
This example is a feature section that changes from a single stacked column on phones to a three-column layout on desktops. It uses base styles, two breakpoints, and a prefix-only change in spacing and type size.
<section class="mx-auto max-w-5xl px-4 py-8 md:py-16">
<h2 class="text-2xl font-bold md:text-4xl">Features</h2>
<div class="mt-6 grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-3">
<article class="rounded-xl bg-white p-6 shadow-md">
<h3 class="font-semibold">Fast</h3>
<p class="mt-2 text-slate-600">Only the CSS you use is generated.</p>
</article>
<article class="rounded-xl bg-white p-6 shadow-md">
<h3 class="font-semibold">Consistent</h3>
<p class="mt-2 text-slate-600">Spacing and color come from one scale.</p>
</article>
<article class="rounded-xl bg-white p-6 shadow-md">
<h3 class="font-semibold">Flexible</h3>
<p class="mt-2 text-slate-600">Override anything with a prefix.</p>
</article>
</div>
</section>Read it from the top. The base layer is a single column with py-8 padding and a text-2xl heading. At md, the padding doubles, the heading grows, and the grid gets two columns. At lg, the grid gets three. Nothing is written for phones that is not already unprefixed.
Exercise
A navigation list has the classes hidden md:flex gap-4. A teammate wants the list visible on phones too, stacked vertically, and a row from md up. Which classes should replace hidden md:flex gap-4, and why does sm: not fit here?
Show answer
Use flex flex-col gap-4 md:flex-row. The unprefixed flex flex-col applies at every width, so phones see a vertical stack, and md:flex-row switches to a row at 768px and wider. sm: would change the layout at 640px and wider, which is a different breakpoint and not the one asked for. Also, sm: never means "phones only", because every prefix means "at this width and up".
Exercise
You want a banner that is visible only between the md and xl breakpoints, hidden both below md and from xl up. Write the classes.
Show answer
hidden md:max-xl:block. The banner is hidden by default. The stacked variant md:max-xl: matches only when the viewport is at least md and below xl, and in that range block overrides hidden.