Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท Responsive Design and State Variants

State Variants: Hover, Focus and More

Style hover, focus, active, disabled, first, last, odd and placeholder states in Tailwind CSS with variant prefixes, and learn how to stack variants safely.

A button looks different when you point at it, press it, tab to it or when it is disabled. In plain CSS each of those states needs its own rule with a pseudo-class selector. In Tailwind, the state is a variant prefix: hover:bg-sky-700 means "set this background color, but only while the pointer is over the element".

This lesson covers the variants you will use every day: interaction states, form states, position in a list, and generated content such as placeholders and selection. You will also learn how to stack variants and how the order of classes in the HTML does not decide which one wins.

Interaction states

The following variants map one-to-one to CSS pseudo-classes.

Variant CSS pseudo-class When it matches
hover: :hover the pointer is over the element
focus: :focus the element has focus (also by mouse click)
focus-visible: :focus-visible focus that should show a ring, mostly keyboard
active: :active the element is being pressed
visited: :visited a link has been visited
target: :target the element matches the URL fragment

Always give keyboard users a visible focus style. focus-visible: is usually better than focus: for buttons, because it does not draw a ring after a mouse click.

A button with every state

Notice transition: it is not a state, but it makes the color change fade instead of snapping. Transitions get their own lesson in Transitions and Transforms.

Form states

Forms have many states, and each has a variant.

  • disabled: for :disabled, enabled: for :enabled.
  • checked: for checked checkboxes and radios, indeterminate: for the third state.
  • required:, optional: and read-only:.
  • valid: and invalid: for built-in browser validation, user-invalid: and user-valid: which wait until the user has interacted, so a pristine empty field is not red on page load.
  • placeholder: for the ::placeholder text.
  • focus-within: for a wrapper whose child has focus.
Input states

The last box has no focus style of its own. focus-within: lights up the whole wrapper when the inner input is focused, a handy trick for search bars with icons.

Position and structure variants

You can style an element based on where it sits among its siblings, with no extra classes in the HTML.

Variant Matches
first: / last: first or last child
only: the only child
odd: / even: alternating children (zebra stripes)
nth-3: / nth-[3n+1]: a specific position or formula
empty: an element with no children
first-of-type: / last-of-type: same, but among siblings of the same tag
Zebra list with a rounded first and last item

Pseudo-elements and special selectors

Several variants target generated content or browser-owned parts of an element:

  • before: and after: for ::before and ::after. Tailwind adds content: "" automatically when you use them, so you only write before:content-['โ˜…'] when you want visible text.
  • placeholder: for ::placeholder, selection: for highlighted text, marker: for list bullets, first-line: and first-letter: for typography, file: for the button of a file input, backdrop: for the <dialog> backdrop.
HTML
<p class="first-letter:float-left first-letter:mr-2 first-letter:text-4xl first-letter:font-bold selection:bg-yellow-200">
  A drop cap with nothing but classes.
</p>

before: and after: need a size or display to show anything, such as before:block before:h-1 before:bg-indigo-500. Without it the empty pseudo-element has no box.

Stacking variants

Variants combine by chaining them left to right, and the chain reads as a sentence. md:hover:bg-sky-700 means "from 768px up, on hover". dark:md:hover:bg-sky-300 adds dark mode. Tailwind generates one rule per combination, so order matters only for readability.

A useful rule of thumb is to write them from "broad" to "narrow": theme, then breakpoint, then state. Tailwind accepts any order, but consistency helps your teammates.

Stacked variants

Order in the HTML does not decide

In CSS, the last rule wins when specificity is equal. In Tailwind, the order of classes in the class attribute does not matter; the order of rules in the generated stylesheet does. Tailwind sorts variants so that hover: beats a base utility and responsive variants beat unprefixed ones. That is why bg-white hover:bg-gray-100 works no matter how you order them.

The one thing to avoid is adding two utilities of the same kind without a variant, such as p-2 p-4. Which one wins depends on the generated CSS, not on what you meant. See Arbitrary Values and Resolving Conflicts for how to deal with that.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which variant is usually best for a focus ring on a button?

    focus-visible matches when the browser decides the focus indicator should be shown, which is mostly keyboard navigation.
  2. 2. What does focus-within:ring-2 on a wrapper do?

    focus-within maps to :focus-within, which matches an element that contains the focused element.
  3. 3. Why is user-invalid: often nicer than invalid:?

    An empty required field is invalid from the first paint, so invalid: would show errors immediately. user-invalid: waits for interaction.
  4. 4. You write class="hover:bg-red-500 bg-white". Which background shows on hover?

    The order of class names in the HTML is irrelevant. Tailwind generates variants after base utilities so the hover rule wins.

Summary

  • A variant prefix applies a utility only in a state: hover:, focus-visible:, active:, disabled:, checked:, user-invalid:.
  • Structural variants (first:, last:, odd:, even:, empty:) replace extra classes on list items.
  • before:, after:, placeholder:, selection: and friends style pseudo-elements; before: and after: insert content: "" for you.
  • Chain variants (md:hover:) to combine conditions; their order in the class name is free, and the order of classes in the attribute never matters.

Next: Group, Peer and Has Variants