Sub topic 2
How a variant prefix works
This chapter assumes you know basic HTML and CSS, including pseudo-classes such as :hover and :focus. It also assumes you have used Tailwind utilities such as colors, padding, and rounded corners in a project where Tailwind is already set up.
The problem variants solve
In plain CSS, you style a state by writing a new rule with a pseudo-class:
.btn {
background-color: #4f46e5;
}
.btn:hover {
background-color: #4338ca;
}Tailwind has no custom class names like .btn. It gives you single-purpose utilities such as bg-indigo-600. A utility on its own always applies. A variant prefix makes it apply only under a condition.
The prefix syntax
A variant is a name, a colon, and then the utility:
<button class="bg-indigo-600 hover:bg-indigo-700">Save</button>Read it from right to left. bg-indigo-700 is the utility. hover: is the condition. Together they mean "use this background when the pointer is over the element."
bg-indigo-600 has no prefix, so it applies all the time. That is the base style. The prefixed class overrides it while the condition is true.
What Tailwind generates
Tailwind scans your files for class names and writes CSS only for the ones it finds. For hover:bg-indigo-700 it generates roughly this:
.hover\:bg-indigo-700:hover {
background-color: var(--color-indigo-700);
}The colon in the class name is escaped with a backslash because : has a special meaning in CSS selectors. The :hover at the end is the real pseudo-class. In Tailwind v4, the hover rule is also wrapped in a media query so it applies only on devices that support hover. A later chapter covers that behavior.
Variants work with any utility
The prefix is not tied to colors. You can put it in front of nearly any utility:
<a
href="#"
class="text-slate-700 underline-offset-2 hover:text-indigo-600 hover:underline"
>
Docs
</a>Here two utilities share one variant. Each needs its own hover: prefix. Writing hover:text-indigo-600 underline would underline the link all the time.
Variants for different kinds of conditions
Not every variant maps to a pseudo-class. The same colon syntax covers several kinds of conditions:
| Kind | Examples | Condition |
|---|---|---|
| Pseudo-class | `hover:`, `focus:`, `disabled:` | Element state |
| Parent or sibling state | `group-hover:`, `peer-invalid:` | State of a related element |
| Media query | `md:`, `dark:` | Screen size or color scheme |
Once you know the pattern, you can read any of them. This course focuses on the first two rows, then shows how to combine them with the third.
Common errors
- Forgetting the base style.
hover:bg-indigo-700alone leaves the button with no background until the pointer arrives. Set the normal state first. - Dynamic class names. Tailwind finds classes by scanning source text. A string like `
hover:bg-${color}-700` never appears in full, so no CSS is generated. Write complete class names. - Misspelled variants. A typo such as
hovr:produces no CSS and no error. The class silently does nothing. - Spaces after the colon.
hover: bg-indigo-700is two separate classes, and neither works.
Exercise
A button has the classes bg-white hover:bg-slate-100 text-slate-900. Which classes apply when the pointer is not over it, and what two-part structure does hover:bg-slate-100 have?
Show answer
Only bg-white and text-slate-900 apply, because the hover: class needs the hover condition. hover:bg-slate-100 has a variant (hover) and a utility (bg-slate-100), separated by a colon.
Exercise
Why does hover:text-${tone}-600 fail to produce styles when tone is set in JavaScript?
Show answer
Tailwind scans source files for complete class names. The full string hover:text-indigo-600 never appears in the source, so Tailwind generates no CSS for it. Use complete class names, such as a lookup object that maps each tone to a full class string.