Sub topic 3
Stacking Responsive Variants in Tailwind CSS
Assume you know basic HTML, CSS media queries, and Tailwind utilities such as flex, grid, and text-lg. Tailwind combines responsive prefixes with state and theme variants by placing them before the utility.
Stacking Variants
A class such as md:hover:bg-blue-700 means “apply bg-blue-700 when the viewport is at least the md breakpoint and the element is hovered.”
<button class="bg-blue-600 hover:bg-blue-700 md:hover:bg-blue-800">
Save
</button>The unprefixed hover:bg-blue-700 works at every width. The md:hover: rule changes the hover color at md and wider. Variants can also stack with dark:, focus:, and other variants:
<button class="bg-white text-gray-900 dark:bg-gray-800 dark:text-white md:focus:ring-2">
Continue
</button>Targeting Width Ranges
A max-* variant limits a utility to widths below a breakpoint. Stack it with a minimum breakpoint to target a range:
<div class="hidden md:max-lg:block">
Visible from md up to, but not including, lg
</div>This uses md: as the lower bound and max-lg: as the upper bound. Tailwind's default breakpoints are typically sm 640px, md 768px, lg 1024px, xl 1280px, and 2xl 1536px.
Custom and One-Off Breakpoints
In Tailwind v4, add a named breakpoint in your CSS:
@import "tailwindcss";
@theme {
--breakpoint-3xl: 120rem;
}Tailwind v3 uses theme.extend.screens in tailwind.config.js. For a single exception, use an arbitrary breakpoint:
<div class="min-[900px]:flex max-[600px]:hidden">
Special-width behavior
</div>Container Queries
Viewport breakpoints suit page-level layouts. Container queries suit reusable components whose available width differs depending on where they appear.
<div class="@container">
<div class="grid grid-cols-1 @md:grid-cols-2">
<article>Card</article>
<article>Card</article>
</div>
</div>@container makes the parent a query container. @md: responds to that container's width rather than the viewport.
Testing and Debugging
Resize the browser across each boundary and inspect the element's class list. Check the viewport meta tag on mobile:
<meta name="viewport" content="width=device-width, initial-scale=1">When a rule seems wrong, verify the prefix direction, breakpoint value, generated class name, and whether another utility has higher precedence.
Exercise
What does md:max-lg:hover:bg-green-600 mean?
Show answer
Apply the green hover background only when the viewport is at least md and below lg.