Skip to content

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.

20 min read0/4 questions

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-1grid grid-cols-1 md:grid-cols-3
text-5xl max-sm:text-3xltext-3xl sm:text-5xl
flex max-md:flex-colflex 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:

ClassApplies when the viewport is
md:flex768px and up
max-md:flexbelow 768px
md:max-lg:flex768px up to (not including) 1024px
min-[22rem]:flex352px and up (a one-off arbitrary breakpoint)
max-[22rem]:flexbelow 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:

HTML + Tailwind
<style type="text/tailwindcss"> @theme { --breakpoint-xs: 30rem; }</style><div class="space-y-3 p-2 font-mono text-sm"> <p> Active: <span class="rounded bg-slate-200 px-2 py-1 xs:hidden">base (under 480px)</span> <span class="hidden rounded bg-emerald-200 px-2 py-1 xs:inline sm:hidden">xs (480px to 639px)</span> <span class="hidden rounded bg-indigo-200 px-2 py-1 sm:inline">sm or wider</span> </p> <p class="rounded border border-slate-300 p-3 max-xs:border-dashed xs:max-sm:bg-emerald-50"> Dashed border below 480px. Green background only in the xs range. </p></div>

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.

Predict the outputHTML

The viewport is 900px wide. What is the element's display value? Type one word.

<div class="hidden md:max-lg:block lg:flex">
Need a nudge? Hints walk you through it.

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:block on 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.

HTML + Tailwind
<div class="@container resize-x overflow-auto border-2 border-dashed border-slate-300 p-3" style="width: 300px"> <div class="grid grid-cols-1 gap-3 @sm:grid-cols-2 @3xl:grid-cols-4"> <article class="rounded-lg bg-amber-100 p-4 @sm:col-span-2 @3xl:col-span-2"> <p class="text-xs font-semibold uppercase text-amber-800">Featured</p> <h3 class="font-bold">Sourdough at home</h3> </article> <article class="rounded-lg bg-slate-100 p-4">Pickles</article> <article class="rounded-lg bg-slate-100 p-4">Ramen</article> <article class="rounded-lg bg-slate-100 p-4">Granola</article> <article class="rounded-lg bg-slate-100 p-4">Dumplings</article> </div></div>

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:

HTML + Tailwind
<div class="@container resize-x overflow-auto border-2 border-dashed border-slate-300 p-4" style="width: 280px"> <h2 class="text-[clamp(1.25rem,0.75rem+6cqi,3rem)] leading-tight font-black tracking-tight"> Fresh bread before your alarm goes off </h2> <p class="mt-2 text-sm text-slate-600 @md:text-base">Delivered warm, every weekday.</p></div>

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.

HTML + Tailwind
<main class="@container/main resize-x overflow-auto border-2 border-dashed border-slate-300 p-3" style="width: 320px"> <p class="mb-2 text-xs text-slate-500">main is <span class="@lg/main:hidden">narrow</span><span class="hidden @lg/main:inline">32rem or wider</span></p> <div class="grid gap-3 @lg/main:grid-cols-[10rem_1fr]"> <aside class="@container/side rounded-lg bg-slate-100 p-3 text-sm"> Sidebar <span class="hidden @3xs/side:inline">(roomy)</span> </aside> <article class="@container/card rounded-lg border border-slate-200 p-3"> <div class="flex flex-col gap-2 @xs/card:flex-row @xs/card:items-center"> <div class="h-16 rounded bg-sky-200 @xs/card:w-24 @xs/card:shrink-0"></div> <p class="text-sm">This card checks its own width with <code>@xs/card:</code>.</p> </div> </article> </div></main>

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.

Quick check
An element has @container/card and sits inside @container/main. A child of the card uses @md:flex. Which container does @md: measure?
Need a nudge? A hint rules out a wrong answer.

Viewport or container: choosing

Who measures what
  1. Elementfluid units, clamp(), max-w-*
  2. Component@container, @sm:, @lg/card:
  3. Pagesm:, md:, lg: on the viewport

The page skeleton (header, sidebar beside main, number of page columns) depends on the screen. Use viewport breakpoints.

1/3

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 the pairs0/5 matched

Match each class to what it measures or does.

Choose an item, then its match on the other side.

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:

  1. 390px: a typical phone. Nothing should scroll sideways, buttons should be thumb-sized (min-h-11 is about 44px), and text should be readable without zooming.
  2. 768px: md: turns on. Watch for awkward in-between layouts, such as two very wide columns.
  3. 1024px: lg: turns on. Sidebars usually appear here. Check nothing is crowded.
  4. 1440px: a common laptop. Lines of text should not stretch across the whole screen; a max-w-* container or max-w-prose should 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 it in order5 steps

Put this responsive workflow in a sensible order.

  1. Tap the steps below in the right order.
Need a nudge? A hint shows where it starts.
Key terms
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, like xs:.
container query
A style that depends on a parent's size. In Tailwind: @container on 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.