TExcelTAD
Home › Tailwind CSS › Hover, Focus & Other States

Hover, Focus & Other States

Prefix any utility with a variant to apply it only in a certain state: hover:bg-sky-600 applies on hover, focus:ring-2 on focus, disabled:opacity-50 when the element is disabled. Variants can be stacked: dark:md:hover:bg-sky-600.

Interaction states

Variant CSS pseudo-class When
hover: :hover pointer over the element (only on devices that can hover)
focus: :focus element has focus
focus-visible: :focus-visible focus from the keyboard
focus-within: :focus-within the element or a child has focus
active: :active while being pressed
visited: :visited visited links
disabled: :disabled disabled form controls
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="flex flex-wrap items-center gap-3 p-6">
  <button class="rounded-lg bg-sky-600 px-4 py-2 text-white hover:bg-sky-700 active:bg-sky-800">Hover / press</button>
  <button class="rounded-lg border border-gray-300 px-4 py-2 focus-visible:ring-4 focus-visible:ring-amber-400 focus-visible:outline-hidden">Tab to me</button>
  <button disabled class="rounded-lg bg-sky-600 px-4 py-2 text-white disabled:cursor-not-allowed disabled:opacity-40">Disabled</button>
  <div class="flex items-center rounded-lg border border-gray-300 px-2 focus-within:border-sky-500 focus-within:ring-2 focus-within:ring-sky-200">
    🔎<input class="px-2 py-2 outline-hidden" placeholder="focus-within">
  </div>
</body>
</html>

Note: In v4 hover: only applies when the device supports hovering (@media (hover: hover)), so taps on phones no longer leave buttons "stuck" in their hover style.

Structural states

Variant Selects
first: / last: the first / last child
odd: / even: odd / even children (zebra stripes)
nth-3: / nth-[3n+1]: :nth-child(3) / any formula
only: the only child
empty: elements with no content
*: every direct child (put it on the parent)
html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-4">
  <ul class="w-64 overflow-hidden rounded-lg border border-slate-300 *:px-3 *:py-2">
    <li class="font-bold odd:bg-slate-100 even:bg-white first:bg-slate-800 first:text-white">Team</li>
    <li class="odd:bg-slate-100 even:bg-white">Ada</li>
    <li class="odd:bg-slate-100 even:bg-white">Grace</li>
    <li class="odd:bg-slate-100 even:bg-white">Linus</li>
    <li class="odd:bg-slate-100 even:bg-white last:italic">Margaret (last)</li>
  </ul>
</body>
</html>

Styling based on a parent: group

Mark a parent with group, then use group-hover:, group-focus: … on its children to react to the parent's state.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="p-6">
  <a href="#" class="group block max-w-xs rounded-xl border border-gray-200 p-4 hover:bg-sky-600 hover:shadow-lg">
    <p class="font-semibold text-gray-900 group-hover:text-white">New project →</p>
    <p class="text-sm text-gray-500 group-hover:text-sky-100">Hover anywhere on the card; both lines react.</p>
  </a>
</body>
</html>

Styling based on a sibling: peer

Mark an element with peer; a later sibling can then use peer-checked:, peer-invalid:, peer-focus: … This builds toggles and validation messages with zero JavaScript.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4.1.11"></script>
</head>
<body class="space-y-6 p-6">
  <label class="flex cursor-pointer items-center gap-3">
    <input type="checkbox" class="peer sr-only">
    <span class="h-6 w-11 rounded-full bg-gray-300 p-0.5 transition peer-checked:bg-emerald-500 after:block after:size-5 after:rounded-full after:bg-white after:shadow after:transition peer-checked:after:translate-x-5"></span>
    <span>Notifications</span>
  </label>
  <div>
    <input type="email" placeholder="Type an invalid email" class="peer w-64 rounded border border-gray-300 px-3 py-2 invalid:border-red-500">
    <p class="mt-1 hidden text-sm text-red-600 peer-invalid:block">Please enter a valid email address.</p>
  </div>
</body>
</html>

More useful variants

Variant Use
has-checked: / has-[img]: style a parent based on what it contains (:has())
before: / after: pseudo-elements (content is added automatically; set text with before:content-['★'])
placeholder: placeholder text
required: / invalid: / checked: form states
aria-expanded: / data-[state=open]: ARIA and data attributes
not-first: / not-hover: negate another variant (v4)
in-focus: when any ancestor is focused, without needing group (v4)

Tip: Stack order reads left to right like nested conditions: md:hover:underline = "at md and up, when hovered".

Test yourself

A child should turn white when its parent card is hovered. What do you need?

  1. hover:text-white on the child
  2. group on the card and group-hover:text-white on the child
  3. peer on the card
  4. parent-hover:text-white

Which variant shows an error message placed *after* an invalid input?

  1. group-invalid:
  2. peer-invalid:
  3. invalid:
  4. after:invalid

What does *:px-3 on a <ul> do?

  1. Nothing, it is invalid
  2. Adds horizontal padding to every direct child
  3. Adds padding to every descendant
  4. Adds padding to the list only