Skip to content

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.

21 min read0/4 questions

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.

VariantCSSUse it for
focus::focusInputs, which should show focus however they were focused
focus-visible::focus-visibleButtons and links: a ring for keyboard users, not after a mouse click
focus-within::focus-withinA wrapper that lights up while any child has focus
active::activeThe moment of pressing
disabled: / enabled::disabled / :enabledForm 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.

HTML + Tailwind
<div class="flex flex-wrap gap-3 p-2"> <button class="rounded-lg bg-slate-900 px-4 py-2 text-white focus:outline-4 focus:outline-amber-400"> focus: </button> <button class="rounded-lg bg-slate-900 px-4 py-2 text-white focus-visible:outline-4 focus-visible:outline-offset-2 focus-visible:outline-amber-400 active:scale-95"> focus-visible: </button> <button disabled class="rounded-lg bg-slate-900 px-4 py-2 text-white enabled:hover:bg-slate-700 disabled:cursor-not-allowed disabled:opacity-40"> Disabled </button></div><label class="mt-4 flex max-w-xs items-center gap-2 rounded-lg border border-slate-300 px-3 py-2 focus-within:border-indigo-500 focus-within:ring-2 focus-within:ring-indigo-200"> <span class="text-slate-400">@</span> <input class="w-full outline-hidden" placeholder="username" /></label>

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).

Quick check
Why do many teams use focus-visible: rather than focus: for button focus rings?
Need a nudge? A hint rules out a wrong answer.

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.

HTML + Tailwind
<ul class="max-w-sm divide-y divide-slate-200 rounded-xl border border-slate-200"> <li class="group/item flex items-center gap-3 p-3 hover:bg-slate-50"> <div class="size-9 rounded-full bg-rose-200"></div> <div class="flex-1"> <p class="font-medium">Priya Shah</p> <p class="text-sm text-slate-500">Product designer</p> </div> <a href="#" class="group/edit invisible flex items-center gap-1 rounded-md px-2 py-1 text-sm text-slate-500 group-hover/item:visible hover:bg-slate-200"> <span class="group-hover/edit:text-slate-900">Call</span> <span class="transition group-hover/edit:translate-x-0.5">&gt;</span> </a> </li> <li class="group/item flex items-center gap-3 p-3 hover:bg-slate-50"> <div class="size-9 rounded-full bg-sky-200"></div> <div class="flex-1"> <p class="font-medium">Tom Okafor</p> <p class="text-sm text-slate-500">Engineer</p> </div> <a href="#" class="group/edit invisible flex items-center gap-1 rounded-md px-2 py-1 text-sm text-slate-500 group-hover/item:visible hover:bg-slate-200"> <span class="group-hover/edit:text-slate-900">Call</span> <span class="transition group-hover/edit:translate-x-0.5">&gt;</span> </a> </li></ul>

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.

HTML + Tailwind
<form class="max-w-sm space-y-5 p-1"> <div> <label for="email" class="block text-sm font-medium">Email</label> <input id="email" type="email" required placeholder="you@example.com" class="peer/email mt-1 w-full rounded-lg border border-slate-300 px-3 py-2 placeholder:text-slate-400 focus:border-indigo-500 focus:ring-2 focus:ring-indigo-200 focus:outline-hidden user-invalid:border-red-500" /> <p class="mt-1 hidden text-sm text-red-600 peer-user-invalid/email:block">Please enter a valid email address.</p> </div> <fieldset> <legend class="text-sm font-medium">Plan</legend> <div class="mt-2 grid grid-cols-2 gap-2"> <label class="rounded-lg border border-slate-300 p-3 has-checked:border-indigo-500 has-checked:bg-indigo-50 has-focus-visible:ring-2 has-focus-visible:ring-indigo-300"> <input type="radio" name="plan" class="sr-only" checked /> <span class="block font-semibold">Monthly</span> <span class="text-sm text-slate-500">9 a month</span> </label> <label class="rounded-lg border border-slate-300 p-3 has-checked:border-indigo-500 has-checked:bg-indigo-50 has-focus-visible:ring-2 has-focus-visible:ring-indigo-300"> <input type="radio" name="plan" class="sr-only" /> <span class="block font-semibold">Yearly</span> <span class="text-sm text-slate-500">90 a year</span> </label> </div> </fieldset> <label class="flex items-center gap-2 text-sm"> <input type="checkbox" class="size-4 accent-indigo-600" /> Send me recipes </label> <input type="file" class="block text-sm text-slate-500 file:mr-3 file:rounded-md file:border-0 file:bg-slate-900 file:px-3 file:py-2 file:text-white hover:file:bg-slate-700" /></form>

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.

Predict the outputHTML

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>
Need a nudge? Hints walk you through it.

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-180 matches [aria-expanded="true"]. Every aria-* variant checks for the value true.
  • For other values use brackets: aria-[current=page]:font-bold. Note aria-current: alone would look for aria-current="true", not "page".
  • data-active: matches any element with a data-active attribute, and data-[state=open]:block matches 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.

HTML + Tailwind
<div class="max-w-sm"> <button id="toggle" aria-expanded="false" aria-controls="panel" class="group flex w-full items-center justify-between rounded-lg border border-slate-300 px-4 py-3 font-medium aria-expanded:border-indigo-500 aria-expanded:bg-indigo-50"> Delivery details <span class="transition group-aria-expanded:rotate-180" aria-hidden="true">v</span> </button> <div id="panel" data-state="closed" class="mt-2 hidden rounded-lg bg-slate-50 p-4 text-sm data-[state=open]:block"> We deliver Monday to Friday between 7am and 9am. </div></div><script> const btn = document.getElementById("toggle"); const panel = document.getElementById("panel"); btn.addEventListener("click", () => { const open = btn.getAttribute("aria-expanded") === "true"; btn.setAttribute("aria-expanded", String(!open)); panel.dataset.state = open ? "closed" : "open"; });</script>

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.

HTML + Tailwind
<div class="max-w-sm divide-y divide-slate-200 rounded-xl border border-slate-200"> <details class="group p-4 open:bg-amber-50" open> <summary class="flex cursor-pointer list-none items-center justify-between font-medium"> Can I skip a week? <span class="transition group-open:rotate-45" aria-hidden="true">+</span> </summary> <p class="mt-2 text-sm text-slate-600">Yes. Skip any week up to Thursday at noon.</p> </details> <details class="group p-4 open:bg-amber-50"> <summary class="flex cursor-pointer list-none items-center justify-between font-medium"> Is the packaging recyclable? <span class="transition group-open:rotate-45" aria-hidden="true">+</span> </summary> <p class="mt-2 text-sm text-slate-600">All of it, including the ice packs.</p> </details></div><ol class="mt-4 max-w-sm text-sm *:px-3 *:py-2 odd:*:bg-slate-50"> <li class="first:font-semibold">Gold: Ana</li> <li>Silver: Ben</li> <li class="last:text-slate-400">Bronze: Cy</li></ol>

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 the pairs0/5 matched

Match each variant to what it reacts to.

Choose an item, then its match on the other side.
Which way each variant looks
form.groupgroup-*: parentinput.peerpeer: the triggerspanpeer-checked: looks backlabelhas-checked: looks downspangroup-open: looks upsummarydetails.groupopen: on itselfroot

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 with focus-within: or group-focus-within:.
  • Use real elements (button, a, details, input) and style their state; do not fake buttons with div.
  • Text for screen readers only, such as "(opens in a new tab)", goes in a sr-only span.
  • 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.
Quick check
A nav link has aria-current="page". Which class makes it bold?
Need a nudge? A hint rules out a wrong answer.
Key terms
variant
A prefix such as hover: or md: 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/name so nested children can target it with group-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.