skip to content

In Tailwind CSS, what does a responsive variant such as `md:` mean, and how do you limit a utility to a range of widths?

level: juniorimportance: must knowfreq 30%

answer

  1. mobile-first, not device names
  2. unprefixed classes apply everywhere
  3. a min-width media query
  4. max-* caps the upper end
  5. output order, not class order

basics

~20 s

A breakpoint variant such as md: applies its utility from that breakpoint upward through a min-width media query, so unprefixed classes are the phone styles. Stacking md: with a max-* variant, as in md:max-xl:, limits it to a range.

solid answer

~40 s

Tailwind's breakpoints are **mobile-first**: an unprefixed utility applies at every width, and `md:grid-cols-2` wraps the utility in a min-width media query (`@media (width >= 48rem)` with the default theme), so it applies at `md` *and above*. To style a phone you write the unprefixed class and override upward; `sm:` means "from the `sm` breakpoint", not "on small screens". Each breakpoint also has a `max-*` variant (`max-md:` is `width < 48rem`), and stacking the two gives a range: `md:max-xl:flex` applies only from `md` up to, but not including, `xl`. When several min-width breakpoint classes on one element match, the larger breakpoint wins because Tailwind emits breakpoint rules in ascending order; the order of classes in the `class` attribute plays no part.

code

html · 10 lines
html
<!-- one column on phones, two from sm, three from lg -->
<ul class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
  <li class="rounded-lg p-4 lg:p-6">Lemon risotto</li>
</ul>

<!-- a jump-to-steps bar shown only from md up to, not including, xl -->
<nav class="hidden md:max-xl:flex">
  <a href="#ingredients">Ingredients</a>
  <a href="#method">Method</a>
</nav>

go deeper

for a junior

Recall that unprefixed classes apply everywhere and md: means md and up. Be able to say where the phone layout goes and why sm: does not target phones.

for a middle

Explain that each breakpoint variant compiles to a min-width media query, that max-* variants exist, and how stacking md:max-xl: produces a range.

for a senior

Diagnose a responsive conflict from the generated CSS: equal specificity, emission order by breakpoint, and why reordering classes in markup never fixes it.

for a principal

Connect the variant mechanics to team practice: mobile-first authoring as a review rule, and when arbitrary min-[…] widths signal a missing breakpoint rather than a one-off need.

## What a breakpoint variant compiles to A **variant** is a prefix that makes a utility conditional. The five default **breakpoint variants** are `sm:`, `md:`, `lg:`, `xl:` and `2xl:`, and each one wraps the utility's declarations in a **min-width media query** at that breakpoint's width. With the default theme, `md:` starts at `48rem` (768px at a 16px root font size). On a recipe-sharing site's card grid: ```html <ul class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3"> <li>...recipe card...</li> </ul> ``` `sm:grid-cols-2` compiles to roughly this: ```css .sm\:grid-cols-2 { @media (width >= 40rem) { grid-template-columns: repeat(2, minmax(0, 1fr)); } } ``` The escaped colon in the selector is just the class name `sm:grid-cols-2` written as CSS. The breakpoint *values* come from the theme and can be changed there; this question is about what the prefix does. ## Mobile-first: where the phone styles go Because every breakpoint variant is a *minimum* width, the system is **mobile-first**: - an **unprefixed** utility (`grid-cols-1`) applies at every width, including the smallest phone; - a prefixed utility (`sm:grid-cols-2`) applies at that breakpoint **and every wider one**; - a wider breakpoint (`lg:grid-cols-3`) overrides a narrower one where both match. The classic surprise is reaching for `sm:` to style phones. `sm:text-center` does nothing on a 375px-wide phone, because `sm` only starts at 40rem. Read `sm:` as "at the small breakpoint and up". The working pattern is to write the phone layout unprefixed and layer changes on at `sm`, then `md`, and so on. ## Ranges and single breakpoints with `max-*` Tailwind generates a `max-*` variant for each breakpoint: `max-md:` compiles to `@media (width < 48rem)`, so it applies **below** `md`. Stacking a min variant with a max variant combines both conditions: | Class | Applies when | |---|---| | `md:flex` | width is at least `md`, with no upper limit | | `max-md:flex` | width is below `md` | | `md:max-xl:flex` | width is at least `md` and below `xl` | | `md:max-lg:flex` | only the `md` band, up to `lg` | To target exactly one breakpoint, stack it with the `max-*` variant of the **next** breakpoint (`md:max-lg:`). For a one-off width that does not deserve a theme entry, the `min-[…]` and `max-[…]` variants take an arbitrary value, such as `min-[900px]:grid-cols-4`. ## Why the class attribute order does not matter Every utility here is a single class, so the selectors have the same specificity, and the **later rule in the stylesheet wins**. Tailwind decides that order itself: 1. rules with no variant are emitted first; 2. variant rules follow in a fixed variant order; 3. min-width breakpoint rules are sorted from the smallest breakpoint to the largest. So at 1200px, `class="lg:p-4 md:p-8"` gets `p-4`: both media queries match, and the `lg` rule comes later in the CSS. Writing the classes in the other order changes nothing, because the browser never looks at the order of names in the `class` attribute. ## Reading a responsive class list Read a responsive class list from the smallest screen upward, one property at a time: 1. find the unprefixed utility for the property; that is the phone value; 2. walk the breakpoints in ascending order; each prefixed utility replaces the value from its breakpoint upward; 3. look for `max-*` variants; they end a value instead of starting one. For `hidden md:block lg:hidden`, that gives: hidden on phones, shown from `md`, hidden again from `lg`. The same element can be written `hidden md:max-lg:block`, which states the band directly and is easier to review. Both compile to ordinary media queries; which one a codebase prefers is a convention, not a correctness question. ## Common mistakes - Using `sm:` for phone styles instead of the unprefixed class. - Expecting `md:` to stop at `lg`; it never stops unless a `max-*` variant caps it. - "Fixing" a conflict by reordering classes in the markup. - Hand-writing an `@media` block in a stylesheet for a range that `md:max-xl:` already expresses. - Assuming responsive variants only work on layout utilities; they work on every utility, from colour to cursor. How many breakpoints a design system should define, and at what widths, is a separate design question; the variants only express whatever breakpoints the theme defines.

  • Why does `sm:text-center` leave the text left-aligned on a 375px-wide phone?
    `sm:` is a minimum-width condition: with the default theme it starts at 40rem, which is 640px at a 16px root size. Below that, only unprefixed utilities apply. Put `text-center` unprefixed for the phone and, if wider screens need something else, override it upward with `sm:text-left` or a later breakpoint.
  • How do you apply a utility at the `md` breakpoint only, and not at `lg` or above?
    Stack `md:` with the `max-*` variant of the next breakpoint: `md:max-lg:flex`. The rule then sits inside both conditions, at least the `md` width and below the `lg` width, so it applies in that band alone. For a band between arbitrary widths, `min-[…]` and `max-[…]` take literal values.
  • An element has `md:p-8 lg:p-4`. At 1280px, which padding applies, and does the order in the class attribute matter?
    `p-4`. Both media queries match at 1280px, both selectors are one class with equal specificity, and Tailwind emits the `lg` rule after the `md` rule, so the later one wins. The order of names in the `class` attribute plays no part; only the order of rules in the generated stylesheet does.

  • sm: is the prefix for styling phones and other small screens
  • md: applies only between the md and lg breakpoints
  • the class written last in the class attribute wins a conflict
  • breakpoint prefixes only work on layout utilities such as flex and grid
  • limiting a utility to a range of widths needs a hand-written @media block