Sub topic 1
From CSS pseudo-classes to utility classes
Prerequisites
This chapter assumes you know the following:
- Basic HTML and CSS, including pseudo-classes such as
:hoverand:focus. - Common Tailwind utilities such as colors, padding, and rounded corners.
- A project where Tailwind CSS is already set up.
The problem with plain utilities
A utility class such as bg-indigo-600 always applies. It has no condition. But interfaces change with state: a button looks different under the pointer, when it has keyboard focus, or when it is disabled.
In plain CSS you handle this with a pseudo-class. A pseudo-class is a keyword added to a selector that matches an element only while it is in a certain state.
.save-button {
background-color: #4f46e5;
}
.save-button:hover {
background-color: #4338ca;
}
.save-button:disabled {
opacity: 0.5;
}This works, but you must name the class, write a rule for each state, and keep the CSS file and the HTML in sync.
The Tailwind equivalent
Tailwind keeps the state in the HTML. You put a prefix in front of a utility, followed by a colon. The prefix is called a variant. The utility then applies only in that state.
<button class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 disabled:opacity-50">
Save
</button>Here bg-indigo-600 applies always. hover:bg-indigo-700 applies only while the pointer is over the button. disabled:opacity-50 applies only when the button has the disabled attribute.
What Tailwind generates
A variant is a recipe for building a selector. When Tailwind finds hover:bg-indigo-700 in your files, it writes a rule like this. The exact color value depends on your Tailwind version and theme.
.hover\:bg-indigo-700:hover {
background-color: #4338ca;
}Two details matter:
- The colon in the class name is escaped with a backslash, because a bare
:in a selector would start a pseudo-class. In the HTML you writehover:bg-indigo-700with no backslash. - The
:hoverpart is added at the end of the selector. The class matches the element, and the pseudo-class limits when the rule applies.
In Tailwind v4, the hover variant is also wrapped in a @media (hover: hover) query, so it applies only on devices that can hover. A later chapter covers touch devices.
Tailwind generates CSS only for classes it finds in your source files. A variant you never write costs nothing in your output.
Specificity and order
A variant rule has more specificity than the plain utility, because the pseudo-class adds to the selector. This is why hover:bg-indigo-700 wins over bg-indigo-600 when both match. You do not need to place the classes in a particular order inside the class attribute. Order in the attribute has no effect. Only the order of rules in the generated CSS matters, and Tailwind controls that.
Common mistakes
- Writing the variant without the base utility.
hover:bg-indigo-700alone leaves the button with no background until it is hovered. Set the default state first. - Using a variant on the wrong element.
disabled:only matches elements that can be disabled, such as buttons and inputs. Adivis never:disabled. - Building class names dynamically. A string like `
hover:bg-${color}-700` is never found by the scanner, so no CSS is generated. Write complete class names in your source.
What comes next
The same prefix pattern covers many states. The next chapters apply it to hover, focus, pressed, and disabled states, then extend it to parents and siblings with group and peer.
Exercise
A button has the classes "bg-slate-700 text-white hover:bg-slate-900". Which CSS selector does Tailwind generate for the hover class, and what background does the button have when the pointer is not over it?
Show answer
Tailwind generates a rule whose selector is .hover\:bg-slate-900:hover. When the pointer is not over the button, the :hover condition does not match, so only bg-slate-700 applies and the button has the slate-700 background.