Lesson 38 of 52Tailwind in practice
Tailwind Variants and Interactivity
Go past hover: stack variants, style from a parent with group, from a sibling with peer, from children with has-*, and from ARIA and data attributes, with accessible forms.
You can now lay out a responsive page with Tailwind. This unit puts it to work in real components. A button that does nothing when you hover, press or tab to it feels broken. A form that gives no hint when an email address is wrong feels hostile. Interactivity is where an interface starts to feel alive, and in Tailwind almost all of it is done with variants: prefixes like hover: that apply a class only under a condition. Tailwind Layout and States showed the basics. This lesson covers the full toolkit you will use every day in real components: stacking, the focus variants done properly, styling based on a parent, a sibling or a child, ARIA and data attributes, open: for disclosure widgets, structural variants, and form styling that is accessible by default.
Variants are conditions you chain
Every variant is a CSS condition in disguise. hover: becomes :hover, md: becomes a media query, dark: becomes a colour scheme query. When you stack them, all conditions must be true at once:
<a class="text-slate-600 hover:text-slate-900 md:hover:underline dark:md:hover:text-white">Docs</a>Read dark:md:hover:text-white from left to right: "in dark mode, and at 768px or wider, and while hovered, make the text white". In version 4, variants apply in the order you write them, left to right, like nested CSS. For most pairs the order does not change the result, but it does matter for variants that move the target, like *: (direct children) or group-hover:. If a stacked class does nothing, try reading it aloud as a chain.
Two version 4 details worth knowing. hover: only applies on devices that can actually hover, so taps on phones do not leave sticky hover colours. And any CSS selector can become a one-off variant with square brackets: [&.is-active]:bg-sky-100 or [&>svg]:size-4.
Focus, active and disabled, properly
These four variants decide whether a control feels trustworthy.
| Variant | CSS | Use it for |
|---|---|---|
focus: | :focus | Inputs, which should show focus however they were focused |
focus-visible: | :focus-visible | Buttons and links: a ring for keyboard users, not after a mouse click |
focus-within: | :focus-within | A wrapper that lights up while any child has focus |
active: | :active | The moment of pressing |
disabled: / enabled: | :disabled / :enabled | Form controls with or without the disabled attribute |
The difference between focus: and focus-visible: is easiest to feel. Click each button below with the mouse, then click empty space and press Tab to move through them.
Preview
The first button keeps its ring after a mouse click, which many designers dislike, so they remove focus styles and break keyboard navigation. focus-visible: gives keyboard users a ring and mouse users none. On the disabled button, enabled:hover: means the hover colour only applies when the button is usable; hover: alone would still fire on a disabled button. The username box uses focus-within: so the whole field glows while the input inside is focused, and outline-hidden hides the input's own outline while keeping one in Windows high contrast mode (outline-none would remove it completely).
focus-visible: rather than focus: for button focus rings?Styling from a parent: group and named groups
Mark a parent with group, and any descendant can react to the parent's state with group-hover:, group-focus-visible:, group-has-checked: and so on. When groups nest, the inner element cannot tell which group you mean. Give each one a name with a slash: group/item on the parent, group-hover/item: on the child.
Preview
Hovering the row reveals its Call link (group-hover/item:visible). Hovering the link itself darkens its label (group-hover/edit:). Be careful with reveal-on-hover: touch and keyboard users never hover. A real version would also add group-focus-within/item:visible so tabbing onto the link shows it.
Styling from a sibling: peer
peer is the sibling version of group. Mark an element peer, and later siblings can use peer-checked:, peer-focus:, peer-invalid: and friends. "Later" matters: CSS can only select siblings that come after an element, so the peer must come first in the HTML. Names work here too: peer/email and peer-invalid/email:.
has-* goes the other direction: a parent reacts to something inside it, using the CSS :has() selector. has-checked:bg-indigo-50 on a label highlights it when the radio inside is checked. That removes a lot of JavaScript that used to exist only to add a class.
Preview
Type hello in the email box and click elsewhere: the border and message appear. That uses user-invalid:, which only matches after the person has interacted with the field. Plain invalid: matches immediately, so an empty required field would be red before anyone typed, which feels like being told off. The plan cards hide the real radio with sr-only (still focusable and announced by screen readers) and style the label with has-checked:; Tab to them and use the arrow keys to see has-focus-visible:. The checkbox gets the brand colour with accent-indigo-600, and file: styles the button part of a file input.
The checkbox is checked. Is the paragraph visible? Type yes or no.
<p class="hidden peer-checked:block">Thanks!</p> <input type="checkbox" class="peer" checked>
ARIA and data attributes
Interactive widgets often store their state in attributes, and Tailwind can style from those directly. That keeps state in one place: the JavaScript (or React) sets the attribute, and the CSS follows.
aria-expanded:rotate-180matches[aria-expanded="true"]. Everyaria-*variant checks for the valuetrue.- For other values use brackets:
aria-[current=page]:font-bold. Notearia-current:alone would look foraria-current="true", not"page". data-active:matches any element with adata-activeattribute, anddata-[state=open]:blockmatches a specific value. Component libraries such as Radix and Base UI expose their state this way.
Styling from ARIA has a bonus: if the visual state depends on aria-expanded, you cannot forget to set it, so screen readers always get the truth.
Preview
The script only flips two attributes; every visual change lives in the classes. aria-* variants check the element they are on, so the arrow inside uses group-aria-expanded:rotate-180 to read the state from its parent button. Try changing the panel's data-[state=open]:block to data-[state=open]:flex and adding a second line of text, or set aria-expanded="true" in the HTML so it starts open (you would also set data-state="open").
open: and native disclosure
For simple show and hide you often need no script at all. The details element opens and closes itself, and open: styles it while open. Use group on details and group-open: for the parts inside. The same open: variant works for dialog and popovers.
Preview
Click the questions to open and close them. The list shows structural variants: first: and last: match the first and last child, odd: and even: alternate rows, and *: styles every direct child from the parent, handy when you do not control the child markup. Here odd:*:bg-slate-50 reads, left to right, "when the list itself is an odd child of its parent, style all its children": not what you meant, and probably no stripes. Change it to *:odd:bg-slate-50 ("each child, when that child is odd") and the stripes appear. That is stacking order in action.
Match each variant to what it reacts to.
group variants look up to an ancestor, peer variants look back to an earlier sibling, and has-* looks down into descendants.
Accessibility checklist for interactive styles
- Every clickable element gets a visible
focus-visible:style. Never remove outlines without a replacement. - Do not hide information behind
hover:alone. Pair reveals withfocus-within:orgroup-focus-within:. - Use real elements (
button,a,details,input) and style their state; do not fake buttons withdiv. - Text for screen readers only, such as "(opens in a new tab)", goes in a
sr-onlyspan. - Error states use more than colour: a message, an icon or a thicker border, since some people cannot tell red from green.
- Prefer
user-invalid:for error styles so people are not scolded before they type.
aria-current="page". Which class makes it bold?- variant
- A prefix such as
hover:ormd:that applies a utility only when a condition is true. - focus-visible
- A focus state the browser shows when focus should be visible, mainly keyboard navigation.
- named group
- A parent marked
group/nameso nested children can target it withgroup-hover/name:. - peer
- A class on an element so later siblings can react to its state with
peer-*variants. - has-* variant
- Styles an element based on its descendants, using the CSS
:has()selector. - sr-only
- Hides content visually while keeping it available to screen readers.
- user-invalid
- Matches an invalid form field only after the user has interacted with it.