Sub topic 5
Responsive design with breakpoint variants
This chapter assumes you know React components and JSX, CSS media queries, and the flexbox and grid utilities from the previous chapter. It uses Tailwind CSS v4.
Mobile first
Tailwind responsive variants are prefixes such as sm: and lg:. A prefixed utility applies at that screen width and above. A utility with no prefix applies at every width.
This means you write the small-screen style first, then add overrides as the screen grows. The default breakpoints are:
| Prefix | Minimum width | CSS generated |
|---|---|---|
| `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)` |
A responsive grid
This grid shows 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>
)
}At 700px wide, grid-cols-1 and sm:grid-cols-2 both match. The sm: rule wins because Tailwind emits responsive rules later in the stylesheet, in breakpoint order. You never write the 1 case inside a media query.
Changing direction and visibility
Breakpoints work on any utility, not only grids. This header stacks on phones and sits in a row from md. The tagline is hidden until lg.
export function SiteHeader() {
return (
<header className="flex flex-col gap-2 p-4 md:flex-row md:items-center md:justify-between">
<h1 className="text-xl font-bold md:text-2xl">Acme</h1>
<p className="hidden text-sm text-gray-600 lg:block">
Tools for small teams
</p>
<nav className="flex gap-4">
<a href="/docs">Docs</a>
<a href="/pricing">Pricing</a>
</nav>
</header>
)
}hidden lg:block is the usual pattern for showing something only on large screens. To hide something on large screens, use block lg:hidden.
Common mistake: treating `sm:` as "phones"
sm: does not mean small screens only. It means 640px and up, so it also applies on desktops. If you write sm:text-center expecting a phone-only style, the text will also be centered on a 1400px monitor.
Style the base case without a prefix. Add prefixes only for larger screens.
Targeting a range
Tailwind v4 includes max-* variants, which apply below a breakpoint. You can stack them with a min-width variant to target a range:
export function TabletNotice() {
return (
<p className="hidden md:max-lg:block text-sm text-amber-700">
Tablet layout is in preview.
</p>
)
}This paragraph shows only from 768px up to, but not including, 1024px. Use ranges sparingly. Most layouts need only min-width variants.
Custom breakpoints
Breakpoints are theme variables. Define one with @theme, and Tailwind generates a matching variant:
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
}You can now write 3xl:grid-cols-4. Chapter 12 covers the theme in more detail.
Container queries
Viewport breakpoints are wrong for a component that can appear in a narrow sidebar on a wide screen. In v4, container queries are built in. Mark a parent with @container, then use @md: and similar variants on its children. They respond to the parent's width.
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:gap-3">
<span className="text-sm text-gray-600">{label}</span>
<span className="text-2xl font-semibold">{value}</span>
</div>
</div>
)
}Testing
Use your browser's responsive mode and drag the width across each breakpoint. Check the widths just below and just above each boundary, since that is where layouts break.
Exercise
A paragraph has the classes text-center md:text-left. Which alignment does it have at 600px, 800px and 1400px wide?
Show answer
At 600px it is centered, because md: has not started yet (768px). At 800px and 1400px it is left-aligned, because md:text-left applies at 768px and above and overrides the unprefixed class.
Exercise
You want a sidebar to be hidden on phones and visible from 1024px up. Write the classes.
Show answer
hidden lg:block. The unprefixed hidden applies by default, and lg:block overrides it at 1024px and above.