Sub topic 7
Using Arbitrary Values with Hover, Breakpoints and Other Variants
This chapter assumes you know basic HTML and CSS, common Tailwind utilities such as p-4 and bg-blue-500, and variant prefixes such as hover: and md:. It also assumes you have seen the square-bracket syntax for arbitrary values, like w-[317px].
Variants wrap any utility
A variant is a prefix that makes a utility apply only under a condition. The condition can be a state (hover:), a screen width (lg:), a color scheme (dark:) or something else. Tailwind does not care whether the utility behind the prefix comes from the theme or from square brackets. Both kinds work the same way.
<button class="bg-[#bada55] hover:bg-[#9bb800] md:w-[317px]">
Save
</button>Here the button has a one-off green background. On hover the background changes to a darker green. From the md breakpoint upward, the width is fixed at 317px.
The order is always the variant first, then the utility, then the bracket value. Nothing goes between the variant's colon and the utility name.
Arbitrary properties with variants
Arbitrary properties, which write a CSS declaration directly in square brackets, accept variants too. The variant goes before the opening bracket.
<div class="[mask-type:luminance] hover:[mask-type:alpha]"></div>The element uses mask-type: luminance by default. While the pointer is over it, the rule switches to mask-type: alpha.
Changing a CSS variable at a breakpoint
A common pattern combines the two ideas. An arbitrary property defines a CSS variable, a variant changes it, and an arbitrary value reads it.
<section class="[--scroll-offset:56px] lg:[--scroll-offset:44px] scroll-mt-[var(--scroll-offset)]">
...
</section>Follow the flow of values:
- Without a variant,
--scroll-offsetis56px. - At the
lgbreakpoint and above,lg:[--scroll-offset:44px]overrides it with44px. scroll-mt-[var(--scroll-offset)]is written once. It reads whichever value is active.
This keeps the responsive logic in one place. The alternative is to repeat the whole utility, as in scroll-mt-[56px] lg:scroll-mt-[44px]. That is fine for one property, but the variable pays off when several utilities share the same value.
How variants stack
You can chain several variants on one utility. Tailwind applies them from left to right in the generated selector, and the utility applies only when all the conditions are true.
<a class="text-[15px] md:hover:text-[17px] dark:md:hover:text-[length:var(--link-size)]">
Read more
</a>The second class applies on hover at md and wider. The third applies only when dark mode, md and hover are all active. Notice the length: type hint on the third class. Because var(--link-size) could be a color or a size, the hint tells Tailwind to generate font-size. Type hints are the same inside a variant as outside it.
Common errors
Most problems come from three mistakes.
- A space in the value. A class attribute is split on whitespace, so
grid-cols-[1fr 500px]is read as two classes. Use underscores instead:md:grid-cols-[1fr_500px]. - A variant in the wrong place.
[mask-type:alpha]:hoverdoes not work. Writehover:[mask-type:alpha]. - A built class name. A class such as
md:w-[${width}px]is never generated, because Tailwind reads your source as plain text and does not run your code. Write each full class name in the source, even when you choose between them with JavaScript.
const columns = {
two: "md:grid-cols-[1fr_1fr]",
three: "md:grid-cols-[1fr_500px_2fr]",
};
const classes = `grid gap-[10px] ${columns[layout]}`;Both complete class names appear in the source, so Tailwind finds and generates them.
Exercises
Exercise
Write a class list for a <div> that is 200px wide by default, 320px wide from the lg breakpoint, and has a #bada55 background only while hovered.
Show answer
w-[200px] lg:w-[320px] hover:bg-[#bada55]. Each variant comes first, then the utility with its bracket value.
Exercise
This class does not produce the expected result in a React component: ` md:w-[${size}px] `. Why, and how do you fix it?
Show answer
Tailwind scans source files as plain text and never sees the complete class name, so no CSS is generated. Fix it by writing full class names in the source, for example a map such as { small: "md:w-[240px]", large: "md:w-[480px]" }, and look the class up with the size key.