Sub topic 5
Max and range variants
This chapter assumes you know how to mark an element with @container and how to use @md and @lg on its children. It also assumes you know the md: and max-md: viewport variants. Here you will learn to style an element only when its container is narrower than a size, or only when the container falls between two sizes.
Min and max in container queries
The plain @md variant means "the container is at least md wide". It is a minimum-width query, and it is the same as @min-md. Tailwind v4 also provides @max-md, which applies while the container is narrower than md.
This mirrors viewport variants. md: has a counterpart max-md:, and @md has a counterpart @max-md.
| Variant | Applies when the container is |
|---|---|
| `@md` | at least `md` wide |
| `@min-md` | at least `md` wide (same as `@md`) |
| `@max-md` | narrower than `md` |
Both variants need an ancestor marked with @container. Without one, nothing matches.
Going from wide to narrow
Mobile-first styling usually starts with the narrow layout and adds @md: for wider containers. Sometimes it is clearer to write the wide layout as the default and describe the exception for narrow containers:
<div class="@container">
<div class="flex flex-row @max-md:flex-col">
<div>Left</div>
<div>Right</div>
</div>
</div>The children sit side by side by default. When the container is narrower than md, @max-md:flex-col stacks them. Use whichever form matches how you think about the component. Avoid mixing both directions for the same property on one element, because it becomes hard to see which rule wins at a given width.
Targeting a range
You can stack @min-* and @max-* on one utility to match only a band of widths. Variants combine from left to right, so every condition must be true:
<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 the container is at least md wide and also narrower than xl. At xl and above, @max-xl stops matching, so the element is hidden again.
The upper bound is exclusive. A container exactly as wide as xl is not "narrower than xl", so @max-xl does not apply to it.
Default sizes used by the examples
The named sizes come from the default container scale, in rem:
| Name | Width |
|---|---|
| `@sm` | 24rem |
| `@md` | 28rem |
| `@lg` | 32rem |
| `@xl` | 36rem |
| `@2xl` | 42rem |
With a 16px root font size, md is 448px and xl is 576px. So the range example above is visible for container widths from 448px up to, but not including, 576px.
A range in a real layout
Ranges are useful when a middle size needs a special treatment. This grid shows a "Details" column only in a mid-size band, where there is room for it but not enough room for the full four-column layout:
<div class="@container">
<div class="grid grid-cols-1 gap-4 @min-sm:@max-2xl:grid-cols-2 @2xl:grid-cols-4">
<div class="rounded bg-white p-4 shadow">One</div>
<div class="rounded bg-white p-4 shadow">Two</div>
<div class="rounded bg-white p-4 shadow">Three</div>
<div class="rounded bg-white p-4 shadow">Four</div>
</div>
</div>The grid has one column by default. It has two columns from sm up to below 2xl, and four columns from 2xl up. For this particular grid you could get the same result with plain @sm:grid-cols-2 @2xl:grid-cols-4. A range only becomes necessary when a rule must switch off again at the upper end, as with hidden and block above.
Common mistakes
- Putting
@max-md:on the same element that has@container. Container variants respond to an ancestor, so the container itself does not match its own queries. Put the variant on a child. - Writing
@max-md:@min-lg:and expecting a range. No width is both narrower thanmdand at leastlg, so the rule never applies. Theminbound must be smaller than themaxbound. - Forgetting that
@max-*is exclusive. If a rule seems to be missing at exactly the boundary width, this is the reason.
Exercise
A panel should be visible only when its container is at least lg wide but narrower than 3xl. Write the class list that does this, starting from hidden. What happens when the container is exactly 3xl wide?
Show answer
Use hidden @min-lg:@max-3xl:block. The element is hidden below lg, a block from lg up to below 3xl, and hidden again from 3xl upward. At exactly 3xl the element is hidden, because @max-3xl means strictly narrower than 3xl.