Sub topic 5
Max and range variants
This chapter assumes you know the basics from the earlier chapters. You should be comfortable marking an element with @container and styling children with @sm, @md and @lg. You should also know the default container size scale. Here you learn how to apply styles below a container size, and how to combine bounds to target a band of widths.
Min, max and range in one picture
Container variants such as @md: are minimum-width variants. @md:flex-row means "when the container is at least md wide, use flex-row." This is the same mobile-first idea as the viewport variants md: and lg:. The unprefixed class is the base style, and each larger variant overrides it as the container grows.
Mobile-first covers most layouts. Sometimes you need a rule that only applies to narrow containers, or only to a middle band. Tailwind v4 provides two more families for this:
@min-*is the explicit form of the minimum variant.@min-md:and@md:produce the same CSS.@max-*applies when the container is narrower than the named size.@max-md:means "belowmd."
Under the hood, Tailwind v4 compiles these to container queries with range syntax:
| Variant | Generated condition | Active when container width is |
|---|---|---|
| `@md:` | `@container (width >= 28rem)` | 28rem or wider |
| `@min-md:` | `@container (width >= 28rem)` | 28rem or wider |
| `@max-md:` | `@container (width < 28rem)` | narrower than 28rem |
The two bounds differ at the edge. A container exactly 28rem wide matches @md and @min-md. It does not match @max-md. The min variant is inclusive and the max variant is exclusive. Because of this, @max-md and @md never match at the same time, and together they cover every width.
The max variant
Use @max-* when a rule belongs to the small case and you want to keep the base classes for the large case. This is the opposite of the usual order, so it fits "desktop-first" thinking.
<div class="@container">
<div class="flex flex-row gap-4 @max-md:flex-col">
<div class="flex-1">Left</div>
<div class="flex-1">Right</div>
</div>
</div>The base style is a row. When the container is narrower than md (28rem), the layout switches to a column. The same result is possible mobile-first with flex flex-col gap-4 @md:flex-row. Both are valid.
Prefer one direction per component. If half the utilities are written mobile-first and half desktop-first, readers have to work out the base style for each property separately. Pick whichever direction matches how you think about the component.
When max is the better choice
@max-* is the cleaner option in a few cases:
- A rule exists only for the narrow case. Hiding a label or shrinking padding when the container is tight is simpler as
@max-sm:hiddenthan as@sm:blockplus ahiddenbase. - You are adapting a desktop-first component. You can add
@max-*overrides without rewriting the base classes. - You need a rule that undoes a larger rule. Resetting a property for small containers is clearer when the intent is written in the class name.
<div class="@container">
<article class="flex items-center gap-3 p-6 @max-sm:p-3">
<img class="size-16 rounded-full @max-xs:size-10" src="/avatar.jpg" alt="Avatar" />
<div>
<h3 class="font-semibold">Dana Reyes</h3>
<p class="text-sm text-gray-600 @max-sm:hidden">Staff engineer, platform team</p>
</div>
</article>
</div>Here the card shrinks its padding below sm (24rem), shrinks the avatar below xs (20rem), and drops the job title below sm. Each rule applies on its own scale.
Range variants by stacking
You can stack @min-* and @max-* on one utility to target a band. Variants stack by writing them one after another, separated by colons, just like hover: and md:.
<div class="@container">
<div class="hidden @min-md:@max-xl:block">
Visible only between @md and @xl
</div>
</div>The element is hidden by default. It becomes a block when both conditions hold: the container is at least md (28rem) and narrower than xl (36rem). That is the half-open range [28rem, 36rem).
Remember the edge rule: the lower bound is inclusive and the upper bound is exclusive. If you want a band that ends at xl and includes it, use the next size up as the max bound.
A range example with real layout
Suppose a stats panel should show a compact two-column grid only in the middle range. Below that it is a single column, and above it a four-column grid.
<div class="@container">
<ul class="grid grid-cols-1 gap-3 @min-sm:@max-xl:grid-cols-2 @xl:grid-cols-4">
<li class="rounded bg-white p-4 shadow">Visits</li>
<li class="rounded bg-white p-4 shadow">Signups</li>
<li class="rounded bg-white p-4 shadow">Revenue</li>
<li class="rounded bg-white p-4 shadow">Churn</li>
</ul>
</div>The widths map to columns as follows:
| Container width | Columns | Class that wins |
|---|---|---|
| Under 24rem | 1 | `grid-cols-1` |
| 24rem to under 36rem | 2 | `@min-sm:@max-xl:grid-cols-2` |
| 36rem and wider | 4 | `@xl:grid-cols-4` |
The range class is written as a band, so it cannot leak into the wide case. In this example the stacked range is optional. Plain mobile-first classes, @sm:grid-cols-2 @xl:grid-cols-4, give the same result, because the @xl rule overrides the @sm rule. Ranges matter when a property should apply to a band and then disappear, rather than be overridden by a larger value.
A good example is a property with no sensible override. Suppose a hint should appear only in medium containers:
<div class="@container">
<p class="hidden text-sm text-gray-500 @min-md:@max-xl:block">
Tip: drag the corner to resize this panel.
</p>
</div>With only @md:block, the hint would also show in large containers. You would need @xl:hidden to turn it off again. The range expresses the intent in one class.
Mixing sizes from different sources
Max and min variants take any size from the container scale, so you can use the whole scale in either direction. Recall the default scale from the previous chapter:
| Name | Size |
|---|---|
| `@3xs` | 16rem |
| `@2xs` | 18rem |
| `@xs` | 20rem |
| `@sm` | 24rem |
| `@md` | 28rem |
| `@lg` | 32rem |
| `@xl` | 36rem |
| `@2xl` | 42rem |
| `@3xl` | 48rem |
| `@4xl` | 56rem |
| `@5xl` | 64rem |
| `@6xl` | 72rem |
| `@7xl` | 80rem |
The small sizes, @3xs to @xs, are useful for max variants. Components such as sidebars and cards are often narrower than 24rem, and @max-xs lets you adapt them there.
If a named size does not fit, use an arbitrary value in square brackets. @max-[600px]:flex-col applies below 600px, and @min-[475px]:@max-[800px]:grid-cols-2 targets a custom band. Arbitrary bounds work with both @min- and @max-. A custom bound is a good fit when a design token such as "a card needs 475px for its image" does not match a named step. If you need the same value in many places, add a theme variable instead, as covered in a later chapter.
Naming a specific container with max and range variants
Max and range variants accept a container name after a slash, the same way @md/main does. Each stacked variant carries its own name, so one utility can depend on two different ancestors.
<div class="@container/main">
<aside class="@container/sidebar">
<div class="flex flex-row @max-md/main:flex-col @max-sm/sidebar:gap-1">
<div>A</div>
<div>B</div>
</div>
</aside>
</div>The flex-col change is tied to the width of main. The gap-1 change is tied to the width of sidebar. Without a name, a variant targets the nearest ancestor marked @container. Name containers once the nearest one is no longer the one you mean. The @container/name form is shown in more detail in the chapter on nested and named containers.
Ordering, overlap and specificity
Range variants make conflicts easier to create. A few rules keep them manageable.
- Overlapping bands are allowed but confusing. If
@max-lg:p-2and@min-md:p-6both match at 30rem, both setpadding. The result depends on the order Tailwind emits the CSS, not the order of classes in your HTML. Avoid two rules that set the same property in overlapping widths. - Make bands meet at one boundary.
@max-md:and@min-md:share a boundary and never overlap. Using the same size for the max of one rule and the min of the next gives clean handoffs. - Prefer one direction per property. For a given property, use either a min chain or a max chain, not both. Ranges are for the "appears in the middle and goes away" case.
- Check the base style. The base class applies when no container variant matches. With a max chain, the base is the large case. With a min chain, the base is the small case.
Common errors with max and range variants
- No
@containerancestor.@max-md:flex-coldoes nothing if no ancestor has the@containerclass. The element is not wrong, and Tailwind still generates the CSS, but the browser has no container to measure. - Styling the container itself. A container query measures the nearest ancestor, never the element it is on. If
@containerand@max-md:flex-colare on the same element, the variant does not respond to that element's width. Put the variants on a child. - Stacking in the wrong shape.
@min-md:@max-xl:blockis correct.@md-xl:blockdoes not exist, and@min-md-@max-xlis not valid either. Write each bound as its own variant, joined by a colon. - Reversed bounds.
@min-xl:@max-md:blockcan never match, because no width is at least 36rem and also under 28rem. The element stays in its base state. - Mixing up viewport and container variants.
max-md:(no@) reacts to the viewport.@max-md:reacts to the container. They have similar names and a similar effect, so check for the@.
Debugging a range variant
When a range does not apply, check in this order:
- Confirm the
@containerclass is on an ancestor of the element, not on the element. - Open the browser's dev tools and read the container's computed width. Compare it to the size in rem. Remember 1rem is usually 16px, so
@mdis 448px. - Confirm the container has a width that depends on its content or layout. A container sized as
display: inlineor collapsed to zero width will match only the smallest@max-*variants. - Check that the bounds are in the right order, with the smaller size on
@min-*and the larger size on@max-*. - Look for another rule that sets the same property in an overlapping band.
Exercise
A container is exactly 28rem wide. Which of these variants are active: @md, @min-md, @max-md, @max-lg? Explain the boundary behavior.
Show answer
@md, @min-md and @max-lg are active. @max-md is not. Min variants use width >= 28rem, so 28rem matches. Max variants use width < size, so 28rem is not below md (28rem) but is below lg (32rem).
Exercise
Write the classes for a <nav> inside an @container ancestor. It should be hidden by default and display as flex only when the container is at least sm but narrower than 2xl. Then explain why it would be wrong to put @container on the same <nav>.
Show answer
Use class="hidden @min-sm:@max-2xl:flex" on the <nav>, with the @container class on a parent element. A container query measures the nearest ancestor container, not the element it is on, so the variants would not respond to that element's own width. The @container class belongs on the parent.