Sub topic 6
Styling hover, focus and disabled states
This chapter assumes you can write React components with props and JSX, that Tailwind v4 is already set up in your Vite project, and that you know the core utilities for spacing, color and borders. It covers how Tailwind expresses interactive states, how variants combine, and the mistakes that most often produce a button that looks wrong or is hard to use with a keyboard.
How state variants work
A state variant is a prefix followed by a colon. The prefix limits a utility to one condition:
hover:bg-blue-700applies the background only while the pointer is over the element.disabled:opacity-50applies only when the element has thedisabledattribute.
Tailwind generates one CSS rule per variant and utility pair. For hover:bg-blue-700 it emits a rule whose selector includes :hover. Because the base class and the variant class have the same specificity, the order of rules in the generated stylesheet decides which one wins. Tailwind orders its output so that variants come after base utilities, which is why bg-blue-600 hover:bg-blue-700 behaves the way you expect regardless of the order you type them in.
In Tailwind v4, hover: is wrapped in a @media (hover: hover) query. On a touch screen that cannot hover, the style does not apply. This avoids the "sticky hover" problem, where a tapped button stays highlighted until you tap elsewhere. Do not rely on hover to reveal essential information, because touch users will never see it.
The common pseudo-class variants
| Variant | Applies when | Typical use |
|---|---|---|
| `hover:` | Pointer is over the element (on devices that can hover) | Darken a background |
| `focus:` | Element has focus by any means | Text inputs |
| `focus-visible:` | Browser decides focus should be visible, usually keyboard focus | Buttons and links |
| `focus-within:` | The element or a descendant has focus | Highlight a form row |
| `active:` | Element is being pressed | Slight scale or darker shade |
| `disabled:` | Element has the `disabled` attribute | Fade and block interaction |
| `enabled:` | Element does not have `disabled` | Restrict hover styles to usable controls |
The difference between focus: and focus-visible: matters. A mouse click on a button triggers :focus but usually not :focus-visible. If you put an outline on focus:, mouse users see a ring after every click. If you use focus-visible:, keyboard users get the ring and mouse users do not. Use focus: for text inputs, where the focus indicator is always useful, and focus-visible: for buttons and links.
A complete button
This component combines hover, active, keyboard focus and disabled styles. It also adds a transition so the color change is not abrupt.
import type { ButtonHTMLAttributes } from 'react'
export function SaveButton(props: ButtonHTMLAttributes<HTMLButtonElement>) {
return (
<button
type="button"
className="cursor-pointer rounded-md bg-blue-600 px-4 py-2 font-medium text-white transition-colors enabled:hover:bg-blue-700 enabled:active:bg-blue-800 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 disabled:cursor-not-allowed disabled:opacity-50"
{...props}
>
Save
</button>
)
}Several details are worth noting:
enabled:hover:bg-blue-700keeps a disabled button from changing color on hover. Withoutenabled:, a faded button would still darken, which suggests it is clickable.cursor-pointeris explicit because Tailwind v4's base styles no longer set a pointer cursor on buttons. Browsers' default is an arrow, so add the cursor yourself.transition-colorsanimates only color-related properties. It is cheaper and more predictable than a baretransition, which covers a longer list of properties.- The component spreads
propslast, so callers can passdisabled,onClickandtype. If a caller passesclassName, it will replace the one above because of the spread order. The chapter on merging classes covers a safe fix.
Disabled is more than a look
The disabled: variant only changes appearance. The disabled attribute is what stops clicks and removes the button from the tab order. Never fake a disabled state with classes alone, because a keyboard user could still activate the control.
Sometimes you want a control that looks disabled but stays focusable, for example to show a tooltip explaining why it is unavailable. In that case use aria-disabled and style it with the aria-disabled: variant:
export function SubmitButton({ blocked }: { blocked: boolean }) {
return (
<button
type="submit"
aria-disabled={blocked}
onClick={(e) => {
if (blocked) e.preventDefault()
}}
className="rounded-md bg-green-600 px-4 py-2 text-white aria-disabled:cursor-not-allowed aria-disabled:opacity-50"
>
Submit
</button>
)
}You are now responsible for blocking the action in JavaScript, as the onClick handler does here. The variant only handles the look.
Stacking variants
You can chain variants, and each one adds a condition. md:hover:bg-blue-700 means "at the md breakpoint and wider, on hover." dark:md:hover:bg-blue-400 adds a dark-mode condition as well.
In Tailwind v4, stacked variants apply from left to right, as in the order you read them. Tailwind v3 applied them right to left. This matters for variants that wrap content, such as *: (children) or file:. If you are copying snippets from older articles, check which version they were written for.
Long chains are hard to read. If a class list has several stacked variants, consider whether the component is doing too much, or move the variants into a typed lookup table as the component chapter shows.
Styling based on a parent or sibling
Sometimes the element you want to style is not the one the user is interacting with. Tailwind has two mechanisms for this.
Group variants
Mark an ancestor with the group class. Descendants can then react to the ancestor's state with group-hover:, group-focus-visible: and similar variants.
export function LinkCard({ title, href }: { title: string; href: string }) {
return (
<a
href={href}
className="group flex items-center justify-between rounded-lg border border-gray-200 p-4 hover:border-blue-500 focus-visible:outline-2 focus-visible:outline-blue-600"
>
<span className="font-medium text-gray-900 group-hover:text-blue-700">{title}</span>
<span aria-hidden="true" className="transition-transform group-hover:translate-x-1">
→
</span>
</a>
)
}Hovering anywhere on the card colors the title and nudges the arrow. If you nest groups, name them with group/card and group-hover/card: so an inner group does not capture the wrong state.
Peer variants
Mark an element with peer. A later sibling can then react with peer-checked:, peer-invalid: or peer-focus:. The peer must come earlier in the DOM, because CSS sibling selectors only look forward.
export function EmailField() {
return (
<div>
<input
id="email"
type="email"
required
placeholder="[email protected]"
className="peer w-full rounded-md border border-gray-300 px-3 py-2 focus:border-blue-600 focus:outline-none"
/>
<p className="mt-1 hidden text-sm text-red-600 peer-[:not(:placeholder-shown):invalid]:block">
Enter a valid email address.
</p>
</div>
)
}The message appears only when the field has content and fails validation. The bracket syntax lets you write a custom selector condition. Use it sparingly, since it is harder to read than a named variant.
Styling from attributes
React components often express state through attributes, either ARIA attributes or data- attributes. Tailwind has variants for both:
aria-expanded:,aria-checked:andaria-invalid:match the corresponding ARIA attribute set totrue.data-[state=open]:matchesdata-state="open".
import { useState } from 'react'
export function Disclosure({ children }: { children: React.ReactNode }) {
const [open, setOpen] = useState(false)
return (
<div>
<button
type="button"
aria-expanded={open}
onClick={() => setOpen(!open)}
className="rounded-md bg-gray-100 px-3 py-2 hover:bg-gray-200 aria-expanded:bg-gray-200 aria-expanded:font-semibold"
>
Details
</button>
{open && <div className="mt-2 text-gray-700">{children}</div>}
</div>
)
}This approach keeps the visual state tied to the accessible state. You cannot end up with a button that looks open while a screen reader announces it as collapsed, because both read from the same attribute.
Common errors
- Outline removed with no replacement. Writing
focus:outline-noneon a button deletes the only keyboard indicator. In v4,outline-hiddenhides the default outline but keeps a visible one in forced-colors mode. Whichever you use, add afocus-visible:outline or ring beside it. - Hover styles on disabled controls. Use
enabled:hover:or add adisabled:override that restores the base color. - Hover-only information. Tooltips and menus that open only on
hover:are unreachable on touch devices and for keyboard users. Pair them withfocus-visible:orgroup-focus-within:. - Missing transitions. State changes without
transition-colorsortransition-transformfeel abrupt, but avoidtransition-allon large elements because it can animate layout properties unintentionally. - Peer after its target. A
peerelement must precede the elements that react to it. Reorder the markup or restructure withgroupandhas-*:variants. - Wrong version syntax. If stacked variants behave in reverse, the snippet was probably written for v3.
Summary of decisions
- Use
hover:for pointer feedback and never as the only way to reach information. - Use
focus-visible:for buttons and links, andfocus:for text inputs. - Use
enabled:to keep hover and active styles away from disabled controls. - Use
groupandpeerwhen the visual change belongs to a different element than the one that changed state. - Use
aria-*:anddata-[...]:variants when React state is already exposed as an attribute.
Exercise
A teammate writes className="bg-blue-600 hover:bg-blue-700 focus:outline-none disabled:opacity-50" on a submit button. Name two problems for keyboard or touch users, and give the corrected class list.
Show answer
First, focus:outline-none removes the keyboard focus indicator and nothing replaces it. Second, hover:bg-blue-700 still applies to the disabled button, so a faded control darkens on hover as if it were usable. A corrected list is cursor-pointer bg-blue-600 enabled:hover:bg-blue-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 disabled:cursor-not-allowed disabled:opacity-50. Because Tailwind v4's hover: only applies on devices that support hover, touch users will not see the hover color at all, so the focus and disabled styles must carry the important information.
Exercise
You have a list row with a title and a trash icon. The icon should appear only when the row is hovered or when any control inside the row has keyboard focus. Which Tailwind variants do you put on the row and on the icon?
Show answer
Put group on the row. Put opacity-0 group-hover:opacity-100 group-focus-within:opacity-100 on the icon, or on the button that wraps it. The group-focus-within: variant makes the icon visible to keyboard users who tab into the row, which group-hover: alone would miss.