Sub topic 8
Arbitrary Variants for Custom Selectors and At-Rules
This chapter assumes you know Tailwind utilities and built-in variants such as hover: and md:. You should also know basic CSS selectors and at-rules like @media and @supports. It builds on arbitrary values (square brackets for one-off values) and arbitrary properties (square brackets for a whole declaration).
What an arbitrary variant is
A variant is a prefix that makes a utility apply only under some condition. hover:bg-gray-100 applies on hover. Tailwind ships many variants, but not every condition has one.
An arbitrary variant is a variant you write yourself inside square brackets. The bracket content is either a selector or an at-rule. You attach it to a utility with a colon, the same way as hover:.
<ul class="[&>li:nth-child(odd)]:bg-gray-100 [@supports(display:grid)]:grid">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>This example has two arbitrary variants. The first targets odd list items. The second applies grid only in browsers that support CSS grid.
Selector variants and the `&` character
Inside the brackets, & stands for the element that carries the class. Tailwind replaces & with the generated class selector.
[&>li:nth-child(odd)]:bg-gray-100means "when this element has a direct childlithat is an odd child, give thatlia gray background". More precisely, the generated rule is.class>li:nth-child(odd) { background-color: ... }. The utility styles the matchedli, not theul.[&:nth-child(3)]:underlinestyles the element itself when it is the third child.[.dark_&]:text-whiteputs the element inside a.darkancestor. The underscore becomes a space, so the selector is.dark .class.
The last example shows why underscore handling matters here. A descendant combinator is a space, and class names cannot contain spaces. You write _ and Tailwind converts it.
<div class="[.dark_&]:text-white text-black">
Black text, white text inside a .dark ancestor.
</div>At-rule variants
If the bracket content starts with @, Tailwind treats it as an at-rule and wraps the utility in it. Use this for conditions with no built-in variant.
<div class="[@media(min-width:900px)]:flex [@supports(display:grid)]:grid">
...
</div>The first class generates @media (min-width:900px) { .class { display: flex } }. The second wraps display: grid in @supports.
Use [@media(...)] for a breakpoint that appears only once. For breakpoints used in many places, add a theme breakpoint instead.
Combining with other variants
Arbitrary variants stack with built-in ones. Variants apply from left to right, so order can matter for the generated selector.
<ul class="hover:[&>li:nth-child(odd)]:bg-gray-100 lg:[&>li]:py-2">
<li>One</li>
<li>Two</li>
</ul>Here the odd rows turn gray when the list is hovered, and the lg: class adds padding to list items only at the large breakpoint.
You can also pair an arbitrary variant with an arbitrary property, as in [&>li]:[--row-gap:8px]. This sets a CSS variable on every direct child.
Where this fits
Common errors
- Spaces in the class.
[&>li:nth-child(odd)]works.[& > li]splits into separate classes in HTML. Write[&_>_li]or leave the spaces out where the selector allows it. - Forgetting
&. A selector variant without&is not a valid selector variant. Include&to mark where the element goes. - Building the selector dynamically. Like all Tailwind classes, the whole string must appear in your source.
[&>${tag}]:p-2is never generated. - Overusing them. A long chain of arbitrary variants is hard to read. If you write the same one three or more times, name it.
Practice
Exercise
Write the classes that give every direct child p of a div a mt-2 margin, but only in browsers that support display: grid. Use arbitrary variants, with the selector variant and the at-rule variant combined on one utility.
Show answer
[@supports(display:grid)]:[&>p]:mt-2. The at-rule variant is on the left, so the selector variant sits inside it. The & stands for the div, so &>p selects its direct child paragraphs, and mt-2 applies to those paragraphs inside the @supports block.
Exercise
Why does [.dark_&]:text-white use an underscore, and what CSS selector does it produce?
Show answer
Class names in HTML cannot contain spaces, so Tailwind converts the underscore to a space. The selector becomes .dark .class, a descendant combinator, so the text is white when the element sits inside a .dark ancestor.