Sub topic 8
Targeting ranges with max-* variants
This chapter covers the max-* variants and how to combine them with min breakpoints to style a limited width range. It assumes you know the mobile-first model, the default breakpoints and basic breakpoint prefixes from earlier chapters. It also assumes you are on Tailwind v3.2 or later, because max-* variants do not exist before that.
Why you need an upper bound
A prefix such as md: is a minimum width. md:flex means "from 768px upward". The style never switches off at a larger width. That is what you want most of the time, and it is why Tailwind is mobile-first.
Some rules apply only to part of the width scale:
- A compact menu button that should appear on phones and tablets but vanish on desktops.
- A tablet-only layout tweak that a desktop layout overrides completely.
- A hidden element that should return at one width and leave again at another.
You can often write these with min-width prefixes alone by turning the style on at one width and resetting it at the next. A max-* variant states the limit directly, so the intent is visible in the class list.
How max-* variants work
Each default breakpoint has a matching max-* variant. It applies when the viewport is narrower than that breakpoint.
| Variant | Applies when width is | Pixels (default) |
|---|---|---|
| `max-sm:` | below `sm` | under 640px |
| `max-md:` | below `md` | under 768px |
| `max-lg:` | below `lg` | under 1024px |
| `max-xl:` | below `xl` | under 1280px |
| `max-2xl:` | below `2xl` | under 1536px |
The bound is exclusive. max-md: stops applying at exactly 768px, which is the same point where md: starts. A viewport is therefore always matched by exactly one of max-md: and md:, never both and never neither. Tailwind v4 emits these as range media queries such as @media (width < 48rem). Earlier versions emit an equivalent query.
There is no max- version of the unprefixed utility. The base style already applies everywhere, so a max-* variant is only useful when you want to apply something below a width, not in addition to everything.
<button class="max-lg:block hidden lg:hidden">Menu</button>This is the first form you will see in many codebases, and it is more convoluted than it needs to be. The next section shows a cleaner way to write the same intent.
The same result, two ways
Consider a navigation menu button that should show below lg and hide from lg upward. Mobile-first style:
<button class="block lg:hidden">Menu</button>The max-* equivalent for the hidden half of the rule looks like this:
<button class="max-lg:block lg:hidden">Menu</button>Both render the same, but they differ in what the base state is. In the first, the button is visible by default and a larger screen turns it off. In the second, the block only applies below lg, and nothing sets display otherwise, so the element keeps its natural display value. For a button that is inline-block, so on narrow screens it becomes block and on wide screens it is hidden by lg:hidden.
Prefer the first form when the small-screen style is the default. Use max-* when the style you are writing applies only below a width and has no meaningful base equivalent.
<aside class="max-md:hidden">Sidebar</aside>This hides the sidebar below md and leaves it alone elsewhere. The mobile-first equivalent is hidden md:block. These two differ in one way: max-md:hidden does not force a display value above md, so a flex or grid class on the same element still works. hidden md:block would override it. This is the strongest argument for max-* when hiding things.
<aside class="flex flex-col gap-2 max-md:hidden">
<a href="/">Home</a>
<a href="/docs">Docs</a>
</aside>Here flex stays in place at every width where the sidebar is visible, and max-md:hidden removes it on phones only. The mobile-first approach would need hidden md:flex and would lose the flex for the base case.
Targeting a range by stacking
Stack a minimum and a maximum to get a window. Variants chain left to right and all must match:
<div class="md:max-lg:hidden">Hidden from md up to, but not including, lg</div>This element is hidden when the width is at least 768px and below 1024px. In v4 the generated CSS is a nested pair of conditions, roughly @media (width >= 48rem) { @media (width < 64rem) { ... } }.
Notice that the band from 768px to 1023px is the only one where md:max-lg: applies. The same result in plain mobile-first style would need two classes with a reset, such as md:block lg:hidden, which sets and then undoes the style. The stacked form says it in one variant.
A tablet-only banner
<p class="hidden md:max-lg:block rounded bg-amber-100 p-3">
Tablet tip: rotate your device for the full editor.
</p>The base hidden removes the banner everywhere. md:max-lg:block shows it only in the 768px to 1023px window. Both classes are about display, so the more specific variant takes precedence only inside its window.
Range with a state variant
You can add state variants to the chain. The order of the chain does not change which conditions must match, but keep one order for readability, for example breakpoint, then state:
<a class="text-slate-700 md:max-lg:hover:text-blue-700" href="/docs">Docs</a>The hover color applies only on hover, and only between md and lg.
Using max-* with arbitrary and custom breakpoints
max-* works with any breakpoint you have defined, and with arbitrary values in square brackets.
<div class="min-[900px]:flex max-[600px]:hidden">One-off breakpoints</div>Here min-[900px]:flex starts at 900px and max-[600px]:hidden applies below 600px. These windows do not overlap, so they do not conflict.
In Tailwind v4, adding a theme breakpoint creates both the min and max variants for it:
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
}You can now write 3xl: for 120rem and wider, and max-3xl: for anything narrower. In v3, the equivalent is an entry under theme.extend.screens, which also generates the max-3xl: variant from v3.2 on:
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
extend: {
screens: {
'3xl': '1920px',
},
},
},
};Use extend so the default breakpoints stay. Replacing screens directly removes the defaults, and then md: and lg: stop working.
Container queries have max variants too
In Tailwind v4, container queries have a matching @max-* form. A component can then respond to its own container's width instead of the viewport:
<div class="@container">
<div class="flex flex-row gap-4 @max-md:flex-col">
<div>Card</div>
<div>Card</div>
</div>
</div>The cards sit in a row by default and stack when the container is narrower than @md. Use this when the component can sit in a wide main column in one place and a narrow sidebar in another.
Common mistakes
- Using max-* to hide on mobile out of habit.
max-md:hiddenis fine, but if you also need to set a display value abovemd,hidden md:flexmay be clearer. Decide based on what the base state should be. - Expecting the bound to be inclusive.
max-md:does not apply at 768px. If a bug shows up at exactly 768px, check that you did not mixmax-md:with a hard-codedmax-width: 768pxsomewhere else, since a raw CSSmax-width: 768pxincludes 768px. - Overlapping windows that set the same property. If
md:max-lg:p-2andmax-xl:p-6both match at 900px, the winner is decided by the order of rules in the generated stylesheet, not the order of classes in your HTML. Avoid ambiguity by making windows disjoint or by using one property per window. - Mixing a max-* variant with its own opposite.
max-md:block md:blockis redundant. Remove one. - Writing an impossible range.
lg:max-md:flexrequires width of at least 1024px and below 768px. It never matches, and Tailwind does not warn you. The minimum must be smaller than the maximum. - Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">, mobile browsers report a wide layout viewport, andmax-*rules for phones do not fire. - Reaching for max-* when sm: means "small".
sm:is a minimum of 640px. If you want styles only for small screens, the correct tool ismax-sm:or an unprefixed base style with a larger prefix overriding it.
Choosing between the approaches
| Situation | Prefer |
|---|---|
| The small-screen style is the natural default | Unprefixed base, larger prefix overrides |
| A style applies only below a width, and you want to keep other display classes | `max-*` |
| A style applies in a window only | Stacked `min:max-*` |
| One component, one place, odd width | Arbitrary `min-[...]` or `max-[...]` |
| The component sits in containers of different widths | Container query with `@max-*` |
Check your understanding
Exercise
An element has the classes hidden md:max-lg:block. At which viewport widths, using the default breakpoints, is it visible?
Show answer
It is visible from 768px up to, but not including, 1024px. Below 768px and from 1024px upward, only hidden applies, so it is not shown.
Exercise
Write the classes for a div that shows as a flex row at every width, but switches to a column below the md breakpoint, using a max-* variant. Then say how you would write the same rule mobile-first.
Show answer
Use flex flex-row max-md:flex-col. Mobile-first, you would write flex flex-col md:flex-row, so that the column is the base layout and the row starts at 768px.
Exercise
Why does lg:max-md:grid never apply?
Show answer
It needs a width of at least 1024px (lg:) and below 768px (max-md:) at the same time. No viewport satisfies both, so the rule never matches.