Lesson 37 of 52Tailwind CSS
Tailwind Responsive Design
Mobile-first Tailwind in depth: custom breakpoints, max-* ranges, responsive grids and type, container queries with named containers, and how to test it all.
You already know the headline rule from the layout lesson: an unprefixed class applies everywhere, and md: applies from 768px up. That is enough for a simple page. Real products need more. A marketing site wants an extra breakpoint for small phones, a dashboard widget has to fit in a sidebar and a main column at the same time, and a heading should shrink gracefully instead of jumping between two sizes. This lesson goes deeper into Tailwind's responsive tools: custom and ranged breakpoints, hiding and showing, grids and type that adapt, and container queries with named containers. It finishes with a testing routine you can run on every page you build.
Thinking mobile-first, for real
Mobile-first is easy to say and easy to break. The rule is simple: write the class with no prefix for the smallest screen, then add prefixes only for what changes as the screen grows.
A common beginner pattern is the opposite: designing on a laptop, writing grid-cols-3, then patching phones with lots of overrides. That fights the system. Compare these two class lists for the same card grid:
| Desktop-first (fighting Tailwind) | Mobile-first (working with it) |
|---|---|
grid grid-cols-3 max-md:grid-cols-1 | grid grid-cols-1 md:grid-cols-3 |
text-5xl max-sm:text-3xl | text-3xl sm:text-5xl |
flex max-md:flex-col | flex flex-col md:flex-row |
Both columns work, but the right one reads naturally ("one column, three from tablet up"), and it means the phone gets the plainest styles. A handy mental test: cover up every prefixed class with your thumb. What is left should be a complete, usable phone layout.
Remember that sm: does not mean "small screens". It means "640px and wider". If you want something only on phones, write it unprefixed and undo it at the breakpoint where it should stop, or use a max-* variant, covered next.
Custom breakpoints and ranges
The five default breakpoints are theme variables, which means you can add, change or remove them in @theme:
@import "tailwindcss";
@theme {
--breakpoint-xs: 30rem; /* adds an xs: variant at 480px */
--breakpoint-3xl: 120rem; /* adds 3xl: at 1920px */
--breakpoint-2xl: initial; /* removes 2xl: */
}Use rem here, just like plain CSS media queries, so breakpoints respect a user's larger default font size. Tailwind sorts breakpoints by size, so xs: slots in before sm: automatically.
Every breakpoint also gets a max version. max-md: means "below 768px" (it compiles to @media (width < 48rem)). Stack a min and a max to target a range:
| Class | Applies when the viewport is |
|---|---|
md:flex | 768px and up |
max-md:flex | below 768px |
md:max-lg:flex | 768px up to (not including) 1024px |
min-[22rem]:flex | 352px and up (a one-off arbitrary breakpoint) |
max-[22rem]:flex | below 352px |
Ranges are useful for genuinely tablet-only fixes, like squeezing a toolbar between 768px and 1024px. Use them sparingly: if most of your classes carry max-*, you are writing desktop-first again.
The preview below is usually between 400px and 600px wide, so the default sm: (640px) will not fire here, but a custom xs: at 480px may. The badge tells you which rules are live:
Preview
Change --breakpoint-xs to 20rem and the green range almost certainly activates. Change it to 60rem and it disappears. Breakpoints are just numbers you own.
The viewport is 900px wide. What is the element's display value? Type one word.
<div class="hidden md:max-lg:block lg:flex">
Hiding and showing
hidden sets display: none. Pair it with a breakpoint to show or hide pieces of the page:
hidden md:block: invisible on phones, visible from 768px. Typical for a desktop sidebar.md:hidden: visible on phones, gone from 768px. Typical for a hamburger menu button.hidden md:flex: use the display value the element really needs.md:blockon a flex row would break its layout.
Two warnings. First, hidden content is still downloaded, so do not hide a heavy desktop-only carousel and assume phones are spared the cost. Second, display: none also hides content from screen readers, which is correct for a duplicate menu but wrong for information. If phone users need it, find a way to show it, perhaps in a different layout. When you want text that only screen readers announce, use sr-only (and md:not-sr-only to reveal it visually on larger screens).
Responsive grids
The classic pattern is a ladder of columns: grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4. Each step adds columns as space appears. Often you also want the gaps and the item spans to change:
<section class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4 lg:gap-6">
<article class="sm:col-span-2">Featured</article>
<article>...</article>
</section>A second option skips breakpoints entirely: grid-cols-[repeat(auto-fit,minmax(14rem,1fr))]. The browser fits as many 14rem columns as it can. It is the plain CSS trick from the grid lesson, written as an arbitrary value. Use the breakpoint ladder when the design specifies exact column counts, and auto-fit when you just want "as many as fit".
Since this preview will not reach sm:, the demo below uses the same ladder with container variants on a box you can resize. The idea is identical; only the thing being measured changes. Drag the bottom right corner.
Preview
At 24rem (384px) the grid becomes two columns with the featured card spanning both. At 48rem (768px) it becomes four, if your screen allows the box to grow that far. Try changing @3xl: to @xl: (36rem) to see the four-column layout sooner.
Responsive type
Headings usually need the biggest change between phone and desktop. You have two tools.
Stepped sizes with prefixes: text-3xl sm:text-4xl lg:text-6xl. Simple and predictable, and each size comes with its paired line height. The downside is a visible jump at each breakpoint.
Fluid sizes with clamp() as an arbitrary value: text-[clamp(1.75rem,1rem+4vw,3.5rem)]. The size slides smoothly between a minimum and a maximum. Keep a rem part in the middle value so browser zoom still works. If the same clamp appears on several headings, turn it into a token: --text-hero: clamp(1.75rem, 1rem + 4vw, 3.5rem); in @theme gives you a text-hero class.
Inside a component, measure the container instead of the viewport with cqi units (1cqi is 1% of the container's width). Drag the box:
Preview
The heading grows smoothly, while the paragraph steps up once at 28rem with @md:. Change 6cqi to 2cqi and the heading barely moves; the middle value controls the speed.
Container queries in depth
The layout lesson introduced @container and @md:. Here is the rest of the toolkit.
Container sizes are their own scale, smaller than the screen breakpoints, because components are smaller than screens: @3xs is 16rem, @xs 20rem, @sm 24rem, @md 28rem, @lg 32rem, @xl 36rem, @2xl 42rem, and so on up to @7xl at 80rem. Do not assume @md: equals md:; it is 448px, not 768px.
Max and range variants work too: @max-md: means "container narrower than 28rem", and @sm:@max-lg: targets a range. Arbitrary sizes look like @min-[30rem]:, and custom sizes go in @theme as --container-8xl: 96rem.
Named containers solve nesting. A child queries its nearest container by default. When a card container sits inside a page container, name them with a slash: @container/main on the outer element, then @lg/main: on any descendant to ask about that one specifically.
Preview
Widen the box slowly. First the page layout switches to a sidebar at @lg/main (32rem). The card then lays itself out by its own width, which is smaller than the page, so it switches later. Each query asks a different container a different question.
@container/card and sits inside @container/main. A child of the card uses @md:flex. Which container does @md: measure?Viewport or container: choosing
The page skeleton (header, sidebar beside main, number of page columns) depends on the screen. Use viewport breakpoints.
A good rule: if you are asking "how big is the device?", use md:. If you are asking "how much room do I have?", use @md:. Device-level things like hiding a mobile menu, showing a desktop sidebar or switching to a bottom tab bar belong to the viewport. Card layouts belong to containers. Container queries are supported in all modern browsers since 2023; check Can I Use if you must support older ones.
Match each class to what it measures or does.
Testing at real sizes
The previews on this page are narrow, so test viewport breakpoints in your own browser. Open devtools, turn on device mode (Ctrl+Shift+M, or Cmd+Shift+M on a Mac), and choose Responsive so you can type exact widths. Check these four:
- 390px: a typical phone. Nothing should scroll sideways, buttons should be thumb-sized (
min-h-11is about 44px), and text should be readable without zooming. - 768px:
md:turns on. Watch for awkward in-between layouts, such as two very wide columns. - 1024px:
lg:turns on. Sidebars usually appear here. Check nothing is crowded. - 1440px: a common laptop. Lines of text should not stretch across the whole screen; a
max-w-*container ormax-w-proseshould hold them.
Then drag slowly from narrow to wide. Bugs live between the breakpoints, not on them. Horizontal scroll is the most common one: in the Elements panel, look for the element wider than the screen (often a long word, a fixed width, or an image without max-w-full). Finally, open the page on a real phone before you ship.
Put this responsive workflow in a sensible order.
- Tap the steps below in the right order.
- mobile-first
- Unprefixed classes describe the phone layout; prefixed classes add changes as space grows.
- max-* variant
- A variant like
max-md:that applies below a breakpoint. Combine with a min variant for a range. - custom breakpoint
- A
--breakpoint-*theme variable that creates a new responsive variant, likexs:. - container query
- A style that depends on a parent's size. In Tailwind:
@containeron the parent and@md:on children. - named container
- A container given a name with
@container/name, queried with variants like@lg/name:. - cqi
- A unit equal to 1% of the container's inline size (its width in English text).
- device mode
- A devtools feature that simulates screen sizes so you can test breakpoints.