Sub topic 14
Arbitrary variants for custom conditions
This chapter assumes you know the built-in variants from the earlier chapters (hover:, group-hover:, peer-invalid:) and the way variants stack. It also assumes Tailwind v3.1 or later.
When built-in variants are not enough
Built-in variants cover common states. Sometimes you need a condition Tailwind has no name for, such as "the third list item" or "any p inside this card". An arbitrary variant lets you write the condition yourself in square brackets. It works like an arbitrary value such as w-[13px], but it applies to the selector instead of the value.
Selector variants with &
Inside the brackets, write a CSS selector. The & stands for the element that carries the class.
<ul class="space-y-1">
<li class="[&:nth-child(3)]:font-bold [&:nth-child(3)]:text-indigo-600">One</li>
<li class="[&:nth-child(3)]:font-bold [&:nth-child(3)]:text-indigo-600">Two</li>
<li class="[&:nth-child(3)]:font-bold [&:nth-child(3)]:text-indigo-600">Three</li>
</ul>Only the third item turns bold and indigo. Tailwind generates a rule like .class:nth-child(3) { ... } for each utility.
Styling descendants
Put & first and add a descendant after it to style elements inside the one that has the class. A space is not allowed in a class name, so write an underscore. Tailwind converts it to a space.
<article class="[&_p]:mt-4 [&_p]:text-slate-600 [&_a]:underline [&>h2]:text-xl">
<h2>Title</h2>
<p>Every paragraph in here gets a top margin.</p>
</article>[&_p]becomes.class p, which matches any descendant paragraph.[&>h2]becomes.class > h2, which matches only direct children.
This is useful for content you do not control, such as rendered Markdown, where you cannot add classes to each tag.
At-rules
A bracket that starts with @ wraps the utility in a CSS at-rule instead.
<div class="grid gap-4 [@media(min-width:900px)]:grid-cols-3 [@supports(display:grid)]:gap-6">
<div>A</div>
<div>B</div>
<div>C</div>
</div>The first variant is a one-off breakpoint at 900px. Use it only when no built-in breakpoint fits. The second applies only where the browser supports display: grid.
Arbitrary conditions on groups and peers
group-[...] and peer-[...] accept the same kind of selector. You can drive a child from a class on its parent, not only from a pseudo-class.
<div class="group is-published rounded border p-4">
<span class="hidden text-green-700 group-[.is-published]:inline">Published</span>
<span class="text-amber-700 group-[.is-published]:hidden">Draft</span>
</div>Remove is-published from the outer div and the badges swap. This pattern fits state that JavaScript toggles with a class.
Common errors
- A space inside the brackets.
[& p]:mt-4breaks the class. Use[&_p]:mt-4. - A missing
&. Tailwind needs it to know where the element goes.[:nth-child(3)]:boldis invalid. - Class names built from strings. Tailwind scans your source for complete class names. `
[&:nth-child(${n})]:font-bold` is never generated. Write out each full class. - Unreadable class lists. A long chain of arbitrary variants is hard to review. Extract a component or use a built-in variant.
Exercise
Write a single class that makes the text red (text-red-600) on every a element inside the element carrying the class. How do you change it to affect only direct child links?
Show answer
Use [&_a]:text-red-600. The underscore becomes a space, so the selector is .class a. For direct children only, use [&>a]:text-red-600.
Exercise
Why does ` [&:nth-child(${n})]:font-bold ` not work in a template string?
Show answer
Tailwind finds classes by scanning your source text for complete class names. A class built at runtime from a variable never appears in the source, so no CSS is generated for it.