Sub topic 1
From CSS pseudo-classes to utility classes
Plain CSS styles interactive states with pseudo-classes such as :hover and :focus. Tailwind CSS has no stylesheet where you write those selectors. Instead, you put the state in the class name. This chapter shows how the two approaches relate, so that later chapters on hover, focus, group, and peer have a clear base.
Prerequisites
This chapter assumes you already know the following:
- Basic HTML and CSS, including pseudo-classes such as
:hoverand:focus. - Common Tailwind utility classes, such as colors, padding, and rounded corners.
- A project where Tailwind CSS is already set up and generating a stylesheet.
What a pseudo-class does in plain CSS
A pseudo-class is a keyword added to a selector. It makes the rule match only while the element is in a certain state. The browser tracks the state, and your CSS reacts to it. You never write JavaScript to detect a hover.
Here is a button styled with plain CSS:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Plain CSS states</title>
<style>
.save {
background-color: #4f46e5;
color: white;
padding: 0.5rem 1rem;
border: 0;
border-radius: 0.25rem;
}
.save:hover {
background-color: #4338ca;
}
.save:focus-visible {
outline: 2px solid #4f46e5;
outline-offset: 2px;
}
.save:disabled {
opacity: 0.5;
cursor: not-allowed;
}
</style>
</head>
<body>
<button class="save">Save</button>
<button class="save" disabled>Save (disabled)</button>
</body>
</html>This approach has three parts:
- A base rule (
.save) sets the default look. - Each state rule (
.save:hover) repeats the same selector with a pseudo-class and overrides only what changes. - The state rules live in a stylesheet, away from the HTML.
The problem utilities have to solve
A Tailwind utility class does one job: it sets one declaration, with no conditions. bg-indigo-600 always sets the background color. It has no way to say "only while hovered".
You could write custom CSS for each state and mix it with utilities. That brings back the stylesheet you were trying to avoid. Tailwind solves the problem by adding a prefix to the class name.
The variant prefix
A variant is a prefix, followed by a colon, placed in front of a utility. hover:bg-indigo-700 means "apply bg-indigo-700, but only while the element is hovered".
The same button in Tailwind looks like this:
<button class="rounded bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:cursor-not-allowed disabled:opacity-50">
Save
</button>Utilities with no prefix, such as rounded and bg-indigo-600, play the role of the base rule. Each prefixed utility plays the role of one state rule.
The following table maps common pseudo-classes to their variants:
| CSS pseudo-class | Tailwind variant | Example utility |
|---|---|---|
| `:hover` | `hover:` | `hover:bg-indigo-700` |
| `:focus` | `focus:` | `focus:ring-2` |
| `:focus-visible` | `focus-visible:` | `focus-visible:outline-2` |
| `:focus-within` | `focus-within:` | `focus-within:border-indigo-500` |
| `:active` | `active:` | `active:bg-indigo-800` |
| `:disabled` | `disabled:` | `disabled:opacity-50` |
| `:invalid` | `invalid:` | `invalid:border-red-500` |
What Tailwind generates
A variant is not a runtime feature. Tailwind reads your source files at build time, finds class names, and writes a CSS rule for each one. For hover:bg-indigo-700, the output is close to this (simplified):
.hover\:bg-indigo-700:hover {
background-color: #4338ca;
}The class name contains a colon, which is not a legal character in a bare CSS class selector. The backslash escapes it. In your HTML you write the class normally. The escaping only exists in the generated stylesheet.
So a variant class is a normal class selector with a pseudo-class added. Your mental model of "what CSS does this produce?" carries over directly from plain CSS.
Which rule wins
The base utility and the variant utility both target the same property on the same element. Two things decide which one applies.
First, specificity. .bg-indigo-600 has one class in its selector. .hover\:bg-indigo-700:hover has one class plus one pseudo-class, so it is more specific. When both match, the variant wins.
Second, order. Tailwind emits variant rules after plain utilities in the stylesheet. This means a variant overrides the base even when specificity ties. You do not need to care about the order of class names in the class attribute. Only the order in the generated stylesheet matters, and Tailwind controls that.
Utilities that only exist in a state
A variant does not need a base utility to go with it. If you write only hover:underline, the element has no underline normally and gets one on hover. The browser's default for text-decoration is the base state.
Be aware of the opposite case. Suppose the base style is the browser's default for a property, and you add a variant that changes it. When the state ends, the property returns to the default. That is usually what you want, but check it when a base value comes from somewhere else, such as a parent rule.
Class names must be written in full
Tailwind finds classes by scanning your files for complete strings. It does not run your code. This causes the most common error with variants. Building a class name from pieces hides it from the scanner:
<script>
// Broken: Tailwind never sees "hover:bg-red-600" as a whole string.
const state = "red";
const cls = `hover:bg-${state}-600`;
</script>The build output will not contain .hover\:bg-red-600:hover, so nothing happens on hover. Write the full names and choose between them:
<script>
// Works: each full class name appears in the source.
const variants = {
red: "hover:bg-red-600",
green: "hover:bg-green-600",
};
const cls = variants["red"];
</script>Version differences worth knowing
Tailwind v4 wraps the hover rule in a media query: @media (hover: hover). The rule applies only when the primary input device can hover, for example a mouse. This avoids the "sticky hover" effect on touch screens, where a tap leaves the hover style stuck. Tailwind v3 does not do this by default. A later chapter covers touch devices in detail. For now, remember that the generated CSS may be slightly more than the one-line rule shown above.
Debugging a variant that does nothing
Work through the following checks in order:
- Is the class spelled as a complete string in a file Tailwind scans?
- Is the base utility and the variant for the same property?
hover:text-whitewill not change a background. - Does another rule override it? Open the browser's DevTools, look at the Styles pane, and find the
.hover\:...:hoverrule. DevTools can also force the:hoverstate on an element. - Is the state real? A
disabled:utility needs thedisabledattribute, not just a gray look.
Summary of the model
Plain CSS: write a selector with a pseudo-class in a stylesheet. Tailwind: write a class with a variant prefix in your HTML, and the build step writes the same kind of selector for you. The state is still tracked by the browser, and the cascade still decides which rule wins. Every variant in the next chapters, including group-hover: and peer-invalid:, follows this one idea.
Exercise
Tailwind generates the selector .hover\:bg-indigo-700:hover for the class hover:bg-indigo-700. Why does the selector contain a backslash, and why does the variant win over a plain bg-indigo-600 on the same element?
Show answer
The backslash escapes the colon, which is not valid in an unescaped CSS class selector. The variant wins because its selector has a class plus a pseudo-class, so it is more specific than the single-class selector of bg-indigo-600, and Tailwind also emits variant rules after plain utilities.
Exercise
A teammate writes const cls = hover:bg-${color}-500; and hover does nothing. What is wrong, and how do you fix it?
Show answer
Tailwind scans source files for complete class names, so it never sees hover:bg-red-500 or any other full string and generates no CSS for it. Fix it by writing each full class name in the source, for example in an object that maps each color to its complete class string.