Sub topic 6
Styling hover, focus and disabled states
This chapter assumes you can write React components with props and JSX, know basic CSS pseudo-classes such as :hover, and have Tailwind running in a Vite project as set up earlier in this tutorial.
How state variants work
A variant is a prefix that makes a utility apply only under a condition. Write the variant, a colon, then the utility. hover:bg-blue-700 sets the background color only while the pointer is over the element.
Each variant becomes a CSS selector or at-rule in the generated stylesheet. hover:bg-blue-700 produces a rule that applies inside a :hover selector. Because the condition lives in the class name, you can read an element's states in one place instead of jumping to a separate stylesheet.
export function SaveButton({ disabled }: { disabled?: boolean }) {
return (
<button
disabled={disabled}
className="rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600 disabled:opacity-50"
>
Save
</button>
)
}This button has four parts:
- The base classes (
bg-blue-600,text-white) always apply. hover:bg-blue-700darkens the background under the pointer.- The
focus-visible:classes draw an outline around the button. disabled:opacity-50fades the button when thedisabledattribute is set.
Common state variants
| Variant | Applies when | Typical use |
|---|---|---|
| `hover:` | The pointer is over the element | Color shifts on buttons and links |
| `focus:` | The element has focus by any means | Text inputs |
| `focus-visible:` | The browser decides focus should be visible, usually keyboard focus | Buttons and links |
| `active:` | The element is being pressed | Press feedback |
| `disabled:` | The element has the `disabled` attribute | Faded or blocked controls |
Prefer focus-visible: for buttons and links. With focus:, a mouse click also shows the outline, which many designs avoid. For text inputs, focus: is usually right, because users need to see where they are typing.
Stacking variants
You can chain variants. Each one narrows the condition, and all must be true for the style to apply.
export function PrimaryAction() {
return (
<button className="bg-blue-600 text-white hover:bg-blue-700 disabled:hover:bg-blue-600">
Continue
</button>
)
}Here disabled:hover:bg-blue-600 keeps a disabled button from darkening on hover. Without it, the plain hover: style would still apply to a disabled button.
Styling based on a parent or sibling
State does not have to belong to the element you style. Mark an ancestor with the group class and use group-hover: on its children. Mark a sibling with peer and use peer-focus: on later siblings.
export function LinkRow({ label }: { label: string }) {
return (
<a href="#" className="group flex items-center gap-2 rounded p-2 hover:bg-gray-100">
<span className="text-gray-700 group-hover:text-gray-900">{label}</span>
<span className="opacity-0 transition-opacity group-hover:opacity-100">→</span>
</a>
)
}The arrow stays invisible until the pointer enters the whole row, not only the arrow itself.
Common errors
- Using
focus:on buttons. Mouse clicks leave an outline that designers often did not intend. Usefocus-visible:. - Removing the outline without a replacement.
outline-noneon its own makes keyboard navigation invisible. Always pair it with afocus-visible:style. - Forgetting disabled and hover interact. Disabled buttons still match
:hoverunless you handle it. Usedisabled:hover:or adddisabled:pointer-events-none. - Expecting
disabled:to work on adiv. The variant matches thedisabledattribute, which only form controls such asbuttonandinputsupport.
Exercise
A link should turn dark gray when hovered, show a visible outline only for keyboard users, and have an icon inside it that becomes visible only when the link is hovered. Which variants do you use for each of the three behaviors?
Show answer
Use hover:text-gray-900 for the color change, focus-visible:outline-2 (with an outline color) for the keyboard outline, and add group to the link with group-hover:opacity-100 on the icon (starting from opacity-0).