Sub topic 8
Targeting ranges with max-* variants
This chapter assumes you know the default breakpoints (sm 640px, md 768px, lg 1024px, xl 1280px, 2xl 1536px) and the mobile-first model. It also assumes you have used prefixes such as md:flex, and that you can read a CSS media query.
Why a range is sometimes needed
A plain breakpoint prefix such as md: is a minimum width. md:hidden hides an element at 768px and everything wider. Most layouts need only this. Sometimes a style must apply to one band of widths and nothing else, such as a tablet-only hint or a fix for a cramped layout just below a breakpoint. The max-* variants handle those cases.
The max-* variants
Each default breakpoint has a matching max-* variant. It applies a utility only when the viewport is narrower than that breakpoint.
| Variant | Applies when the viewport is |
|---|---|
| `max-sm:` | narrower than 640px |
| `max-md:` | narrower than 768px |
| `max-lg:` | narrower than 1024px |
| `max-xl:` | narrower than 1280px |
| `max-2xl:` | narrower than 1536px |
The boundary is exclusive. At exactly 768px, max-md: no longer applies and md: does. The two variants never apply at the same width, so they cannot conflict with each other.
<p class="max-md:text-sm">Smaller text only below 768px</p>max-* variants are not available in Tailwind versions before 3.2. They use the same breakpoint names as the min-width variants, so a custom 3xl breakpoint also gives you max-3xl:.
Stacking a min and a max into a range
A min-width variant and a max-width variant can be stacked. The utility then applies only where both conditions are true.
<div class="md:max-lg:hidden">Hidden only from md up to, but not including, lg</div>This element is hidden from 768px up to 1023px. It is visible below 768px and from 1024px up. The order of the two prefixes does not change the result, but writing the minimum first (md:max-lg:) reads naturally from small to large.
A tablet-only note shows the opposite pattern. It is hidden by default and shown inside the range:
<p class="hidden rounded bg-amber-100 p-3 md:max-lg:block">
Tip: rotate your tablet for the full table.
</p>The unprefixed hidden covers widths below md. The stacked md:max-lg:block turns the element on between 768px and 1023px. From lg upward, hidden applies again because the block rule no longer matches.
Stacking with other variants
Range variants combine with state variants in the same way as min-width ones. This link is underlined on hover only below md:
<a href="/docs" class="max-md:hover:underline">Docs</a>Arbitrary max values
For a one-off boundary that is not in your theme, use square brackets. This avoids adding a breakpoint you will use once.
<div class="min-[900px]:flex max-[600px]:hidden">One-off breakpoints</div>When to use max-* and when not to
Prefer the mobile-first style where it works. If you write max-md:flex-col md:flex-row, you have described the same thing twice. The simpler version is flex-col md:flex-row.
Reach for max-* in these cases:
- A style must apply only to a band of widths, as in the tablet note.
- A style must apply to smaller screens, and the default would be wrong for larger ones. An example is a component that already has a desktop-first base from a third-party stylesheet.
- You want to remove a rule that a larger breakpoint added, without restructuring the whole class list.
Common mistakes
- Using
max-md:when you meantmd:. The first affects narrow screens and the second affects wide ones. - Stacking a range whose minimum is larger than its maximum, such as
lg:max-md:. It never matches. - Forgetting the base state. A range utility like
md:max-lg:blockdoes nothing useful if the element is already visible by default.
Exercise
Write the classes that make a <p> invisible below 640px, visible from 640px up to 1279px, and invisible again from 1280px up.
Show answer
hidden sm:max-xl:block. The unprefixed hidden covers widths below 640px and from 1280px up. sm:max-xl:block turns the element on from 640px up to, but not including, 1280px.