Sub topic 2
How a Variant Prefix Works
Prerequisites
This chapter assumes you know basic HTML and CSS, including pseudo-classes such as :hover and :focus. It also assumes you know common Tailwind utility classes (colors, padding, rounded corners) and have a project where Tailwind CSS is already set up.
By the end, you will be able to explain how a prefix such as hover: makes a utility apply only in a given state. You will also be able to read a stacked class like dark:hover:bg-slate-700 and predict when it applies.
The problem variants solve
A plain utility class applies all the time. bg-indigo-600 sets a background color on the element, in every state, from the moment it renders.
Interactive interfaces need styles that apply only under a condition: while the pointer is over a button, while an input has focus, while a control is disabled. In plain CSS you write these conditions as selectors with pseudo-classes:
.save-button {
background-color: #4f46e5;
}
.save-button:hover {
background-color: #4338ca;
}Tailwind keeps you in your HTML by letting you attach the condition to the utility itself. The condition is a variant, and you write it as a prefix followed by a colon.
<button class="bg-indigo-600 hover:bg-indigo-700">Save</button>Here bg-indigo-600 is the base utility, and hover:bg-indigo-700 is the same kind of utility wrapped in the hover variant.
Anatomy of a variant class
A variant class has two parts separated by a colon.
| Part | Example | Meaning |
|---|---|---|
| Variant | `hover` | The condition under which the rule applies |
| Utility | `bg-indigo-700` | The declaration to apply when the condition holds |
The full class name is hover:bg-indigo-700. The colon is part of the class name. In generated CSS it appears escaped, because a bare colon is special in CSS selectors.
Tailwind generates something equivalent to this:
.bg-indigo-600 {
background-color: #4f46e5;
}
.hover\:bg-indigo-700:hover {
background-color: #4338ca;
}Two things are worth noticing:
- The class name
hover:bg-indigo-700is a single token. It matches the element only through theclassattribute. - The
:hoverpseudo-class is attached to the generated selector, not to anything you wrote. Tailwind adds it for you.
Newer Tailwind versions may wrap the hover rule in a media query so it applies only on devices that support hover. This matters on touch screens, and a later chapter covers it.
What the variant does not do
A variant does not replace the base utility. It adds a second rule that wins only while the condition holds. When the condition stops holding, the base rule applies again. That is why you usually list a base utility and its variants together:
<a href="#" class="text-slate-600 hover:text-indigo-600">Docs</a>If you leave out text-slate-600, the link uses the inherited color until hovered, then switches to indigo. That can be what you want, but it is a choice. Leaving out the base utility is a common source of surprise.
How Tailwind finds and generates variant classes
Tailwind does not ship every possible class. It scans your source files for strings that look like class names and generates CSS only for those it finds. This has a practical consequence for variants.
Write class names as complete literal strings. Tailwind cannot see a class you build by joining pieces together:
<!-- Works: the full class name appears in the source -->
<button class="hover:bg-indigo-700">Save</button>
<!-- Does not work: Tailwind never sees "hover:bg-indigo-700" -->
<script>
const color = "indigo";
const cls = `hover:bg-${color}-700`;
</script>If you need to choose between classes, write each full name and select among them:
<script>
const hoverClass = isPrimary ? "hover:bg-indigo-700" : "hover:bg-slate-200";
</script>Ordering and the cascade
When two rules set the same property, CSS picks one using specificity and then source order. Tailwind arranges its output so variants beat the base utility. That is why hover:bg-indigo-700 overrides bg-indigo-600 when both could apply, regardless of the order you wrote them in the class attribute.
The order of classes in the class attribute never affects which rule wins. Only the order of rules in the generated stylesheet matters, and Tailwind controls that.
Variants also have an order relative to one another. For example, hover rules are placed before focus rules, which come before active rules, which come before disabled rules. If more than one applies at once, the later one in the stylesheet wins when they set the same property. Consider a button that is both hovered and pressed:
<button class="bg-indigo-600 hover:bg-indigo-700 active:bg-indigo-800">
Save
</button>While the pointer is down on the button, both hover and active match. Because active comes later in the stylesheet, the button shows bg-indigo-800. This matches the usual expectation that "pressed" is the strongest of the two states.
Many variants, one pattern
The same prefix pattern covers a wide range of conditions. You do not need a different mental model for each one.
| Prefix | Condition it represents |
|---|---|
| `hover:` | Pointer is over the element |
| `focus:` | Element has focus |
| `focus-visible:` | Element has focus and the browser decides a focus ring is useful |
| `active:` | Element is being pressed |
| `disabled:` | Element is disabled |
| `invalid:` | Form field value fails validation |
| `dark:` | Dark color scheme is in effect |
| `md:` | Viewport is at least the `md` breakpoint width |
| `group-hover:` | A marked ancestor is hovered |
| `peer-invalid:` | A marked earlier sibling is invalid |
Some are pseudo-classes of the element itself (hover, focus, disabled). Some are media queries (dark, md). Some depend on another element (group-*, peer-*). All of them use the same variant:utility syntax. Later chapters take these up one at a time.
Stacking variants
You can chain variants by writing several prefixes before the utility. Each prefix adds one more condition, and all of them must hold for the utility to apply.
<button class="bg-white text-slate-900 hover:bg-slate-100 dark:bg-slate-800 dark:text-white dark:hover:bg-slate-700 md:hover:shadow-md">
Settings
</button>Read dark:hover:bg-slate-700 as "in dark mode, and while hovered, use this background." Read md:hover:shadow-md as "at the md breakpoint or wider, and while hovered, add a medium shadow."
Stacked variants behave like an AND. Here is how this button resolves in a few situations:
| Situation | Background | Shadow |
|---|---|---|
| Light mode, not hovered, narrow screen | `bg-white` | none |
| Light mode, hovered, narrow screen | `bg-slate-100` | none |
| Dark mode, not hovered | `bg-slate-800` | none |
| Dark mode, hovered | `bg-slate-700` | none on narrow screens |
| Wide screen, hovered | depends on mode | `shadow-md` |
Note that hover:bg-slate-100 also matches in dark mode when hovered, because it does not mention dark mode. Both it and dark:hover:bg-slate-700 apply, and only one background can win. Tailwind generates dark variant rules after the plain hover rules, so the dark rule takes precedence and the button shows bg-slate-700. This is the reason you give the dark version its own stacked class instead of relying on the light one.
Order of stacked prefixes
In current Tailwind versions, stacked variants apply from left to right, with the leftmost prefix as the outermost wrapper. For most combinations the result is the same either way, so dark:hover: and hover:dark: both behave as expected. The convention is to write the broad condition first (breakpoint or color scheme) and the element state last, like md:dark:hover:. This keeps class lists easy to scan. Some variants, such as those that rewrite the selector, are sensitive to order, so keep the habit of reading prefixes in a consistent direction.
Common errors
- Forgetting the base utility.
hover:bg-indigo-700alone leaves the element without an indigo background in its resting state. - Building class names dynamically. Strings such as `
hover:bg-${color}-700` are not detected, so no CSS is produced. - Putting a space after the colon.
hover: bg-indigo-700is two separate tokens, and neither does what you intended. - Using the wrong separator. The separator is a colon.
hover-bg-indigo-700is a different, nonexistent class. - Expecting class order to decide the winner. The order of rules in the generated stylesheet decides, not the order in your
classattribute. - Styling a state the element cannot enter.
disabled:has no effect on a<div>, because:disabledmatches only form controls and similar elements.
Check your reading
Exercise
A button has the classes bg-indigo-600 hover:bg-indigo-700 active:bg-indigo-800. The user hovers over it and holds down the mouse button. Which background color does the button show, and why?
Show answer
It shows bg-indigo-800. Both hover and active match while the pointer is down on the button. Both rules set background-color, and Tailwind places active rules after hover rules in the stylesheet, so active wins. The order of the classes in the class attribute does not matter.
Exercise
Explain what the class dark:hover:bg-slate-700 requires before it applies, and why hover:bg-${color}-700 built in JavaScript would fail to produce any CSS.
Show answer
dark:hover:bg-slate-700 applies only when both conditions hold: the dark color scheme is in effect and the element is hovered. The dynamic string fails because Tailwind scans source files for complete class names. It never sees hover:bg-indigo-700 as a whole string, so it generates no rule for it. Write the full class names and choose between them instead.