Sub topic 9
Hover, focus, and other state variants
This chapter assumes you know basic HTML and core CSS ideas such as selectors, specificity, and pseudo-classes. It also assumes you have read the earlier chapters on utilities, colors, and responsive prefixes. You will learn how Tailwind applies styles only in certain states, how to stack variants, and which mistakes cause variants to silently fail.
What a variant is
A utility such as bg-indigo-600 always applies. A variant is a prefix that limits a utility to a condition. You write the variant, a colon, then the utility.
<button class="bg-indigo-600 hover:bg-indigo-700">Save</button>The button is indigo-600 by default. While the pointer is over it, hover:bg-indigo-700 wins. Tailwind generates CSS roughly like this:
.hover\:bg-indigo-700:hover {
background-color: var(--color-indigo-700);
}In Tailwind v4, the generated rule for hover is also wrapped so that it applies only on devices that support hover. On touch screens, a tap does not leave the button stuck in a hover style. You get this behavior without writing anything extra.
You already used one kind of variant: the breakpoint prefixes sm: and lg:. State variants work the same way. Both are prefixes, and both can be combined.
Pseudo-class variants you will use most
Most state variants map to a CSS pseudo-class. The table lists the common ones.
| Variant | CSS condition | Typical use |
|---|---|---|
| `hover:` | `:hover` | Pointer is over the element |
| `focus:` | `:focus` | Element has focus, by any method |
| `focus-visible:` | `:focus-visible` | Focus that the browser decides should show a ring, usually keyboard focus |
| `focus-within:` | `:focus-within` | The element or any descendant has focus |
| `active:` | `:active` | Element is being pressed |
| `disabled:` | `:disabled` | Disabled form control |
| `checked:` | `:checked` | Checked checkbox or radio |
| `first:` / `last:` | `:first-child` / `:last-child` | First or last item in a list |
| `odd:` / `even:` | `:nth-child(odd)` / `:nth-child(even)` | Striped rows |
| `placeholder:` | `::placeholder` | Placeholder text in an input |
A complete button
The next example styles a button for several states at once.
<button
class="rounded-lg bg-indigo-600 px-4 py-2 font-medium text-white
hover:bg-indigo-700
focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600
active:bg-indigo-800
disabled:cursor-not-allowed disabled:opacity-50"
>
Save changes
</button>Read it as a list of rules. Each state adds a few changes on top of the base style. The base style is the part without a prefix.
Focus versus focus-visible
focus: applies whenever an element has focus. That includes a mouse click on a button, which often shows an outline that users find distracting. focus-visible: applies only when the browser decides the user needs to see where focus is, which is usually keyboard navigation. For buttons and links, focus-visible: is usually the better choice. For text inputs, browsers show focus for mouse clicks too, so focus: is fine.
Styling a form field
<input
type="email"
placeholder="[email protected]"
class="w-full rounded-md border border-slate-300 px-3 py-2
placeholder:text-slate-400
focus:border-indigo-500 focus:ring-2 focus:ring-indigo-500/40 focus:outline-none
disabled:bg-slate-100"
/>Here focus:outline-none is acceptable because focus:ring-2 and the border color change supply a visible replacement. The /40 after the color is an opacity modifier, so the ring is 40 percent opaque.
Stacking variants
You can chain variants. Tailwind applies the utility only when all conditions are true.
<a class="text-slate-600 md:hover:text-indigo-600 dark:md:hover:text-indigo-300" href="/docs">Docs</a>The link turns indigo on hover only from the md breakpoint up. The third class adds a dark-mode condition. Order matters in one case. In Tailwind v4, stacked variants apply from left to right, which matches how you read them. In v3 they applied from right to left. If you are working in a v3 project and see a stacked variant that behaves oddly, check which version you have installed.
Styling based on a parent or sibling: group and peer
Pseudo-class variants affect the element that carries them. Often you want a child to change when its parent is hovered. Tailwind handles this with group.
group
Mark the parent with group. Then use group-hover: (or group-focus:, and so on) on any descendant.
<a href="/pricing" class="group block rounded-xl border border-slate-200 p-5 hover:border-indigo-500">
<h3 class="font-semibold text-slate-900 group-hover:text-indigo-600">Pricing</h3>
<p class="mt-1 text-sm text-slate-500">Compare plans.</p>
<span class="mt-3 inline-block translate-x-0 transition-transform group-hover:translate-x-1">→</span>
</a>Hovering anywhere on the card changes the title color and nudges the arrow. The card itself uses plain hover:.
peer
Mark an element with peer. A later sibling can then react to its state with peer-checked:, peer-invalid:, peer-focus:, and similar variants.
<label class="flex items-center gap-2">
<input type="checkbox" class="peer size-4" />
<span class="text-slate-500 peer-checked:font-semibold peer-checked:text-slate-900">
Subscribe to updates
</span>
</label>If you nest groups, name them to avoid conflicts. Write group/card on the parent and group-hover/card: on the child. The name after the slash tells Tailwind which ancestor you mean.
Variants beyond pseudo-classes
Pseudo-elements
before: and after: style generated content. Tailwind adds content: var(--tw-content) for you in v3 and v4 only when you use a before: or after: variant with a content utility. In practice, you must set content explicitly, even if it is empty.
<span class="relative before:absolute before:-inset-1 before:rounded before:bg-indigo-100 before:content-['']">
<span class="relative">Highlighted</span>
</span>Without before:content-[''], the pseudo-element does not render and the other before: utilities appear to do nothing. Note the arbitrary value syntax in brackets. The next chapters cover it in more detail.
Attribute and data variants
Tailwind can react to attributes. aria-* variants map to ARIA attributes, and data-* variants map to data attributes.
<button
aria-pressed="true"
data-size="large"
class="rounded px-3 py-1 aria-pressed:bg-indigo-600 aria-pressed:text-white data-[size=large]:px-6"
>
Toggle
</button>This pattern is useful because your JavaScript only needs to flip an attribute. The styling stays in the markup. It also keeps visual state and accessibility state in sync, since aria-pressed is something assistive technology reads.
Form and motion variants
Some variants respond to the user's environment instead of the element's state.
motion-reduce:applies when the user has asked their system to reduce animation.motion-safe:applies only when they have not.print:applies when printing.invalid:applies to fields that fail validation.
<div class="motion-safe:animate-pulse bg-slate-200 h-4 w-32 rounded"></div>Transitions make state changes smooth
A state change is instant unless you add a transition. Use transition-colors (or transition, transition-transform, transition-opacity) on the base element, not on the hover variant.
<button class="bg-indigo-600 transition-colors duration-150 hover:bg-indigo-700">Save</button>If you put transition only in hover:, the transition would not run when the pointer leaves, because the transition property would disappear at that moment. Put it in the base style so it applies in both directions.
How variants are generated
The state variants follow the same detection model as other utilities. Tailwind scans your source files for complete class names. A class such as hover:bg-indigo-700 must appear in full somewhere in your files. The variant prefix and the utility are one unit. For that reason, building hover:${color} from fragments fails in the same way as building text-${color}-600.
The diagram shows how one class turns into one CSS rule.
Common errors
- Variant has no effect. Check the spelling, then check that the base utility is not overriding it. Tailwind sorts variant rules after base utilities, so the usual cause is a typo or a missing parent marker.
group-hoverdoes nothing. The ancestor needs thegroupclass. Without it, the variant has nothing to match.peer-*does nothing. The peer must come earlier in the HTML than the element using the variant.before:content does not appear. Addbefore:content-['']or a real content value.- Hover works on desktop but not on a phone. This is expected. Do not rely on hover to reveal essential information. Make it available through tap or focus too.
- Transition only runs one way. Move the
transition-*utility off the variant and onto the base element.
Exercises
Exercise
A card has the classes rounded-xl border p-5. You want its title to turn indigo when the pointer is anywhere on the card. Which two changes do you make, and why does hover:text-indigo-600 on the title not give the effect?
Show answer
Add group to the card and group-hover:text-indigo-600 to the title. hover: on the title applies only when the pointer is over the title itself, not when it is over the rest of the card. group-hover: matches when the marked ancestor is hovered.
Exercise
A checkbox has class="peer", and a label with peer-checked:font-bold appears before it in the HTML. The label never turns bold. What is the cause and the fix?
Show answer
The peer variant uses the CSS subsequent-sibling selector, so it only affects elements that come after the peer. Move the label after the checkbox in the HTML, or put the checkbox first and use CSS-independent layout utilities such as flex-row-reverse to keep the visual order.
Exercise
Write the classes for a button that is indigo, darkens on hover, shows a visible outline only for keyboard focus, and is faded and shows a not-allowed cursor when disabled.
Show answer
For example: bg-indigo-600 hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:opacity-50 disabled:cursor-not-allowed. Adding transition-colors to the base makes the color change smooth.