Tailwind CSS State Handling

Tailwind CSS provides powerful state classes (state modifiers) for controlling style changes of elements under different interaction states.

Tailwind CSS provides a variety of state classes (such as hover:, focus:, active:, etc.) to help developers quickly handle interaction effects. By combining these state classes, you can easily implement style changes for hover, focus, click, and other states, improving user experience.

State class prefixes:

StatePrefixEffect
Hover statehover:Controls style changes of an element when the mouse hovers over it.
Focus statefocus:Controls style changes of an element when it gains focus.
Active stateactive:Controls style changes of an element when it is clicked.
Visited statevisited:Controls style changes of visited links.
Focus-within statefocus-within:Controls style changes of a parent element when its child element gains focus.
State combinationsNo specific prefixYou can combine multiple state prefixes simultaneously, such ashover:, focus:, andactive:

1. Hover

Hover (hover) state refers to when the user hovers the mouse over an element, the element's style changes. Usehover:prefix, you can easily achieve this effect.

Syntax:

hover:{class}

For example,hover:bg-sky-700will change the element's background color to when the mouse hovers over it.sky-700。

Common usage:

Example

<button class="px-4 py-2 bg-blue-500 text-white hover:bg-sky-700">
Move the mouse here
</button>

Try it »

When the mouse hovers, the button background color changes fromblue-500tosky-700。

More examples:

  • hover:text-white: Turns the text color to white on hover.
  • hover:underline: Adds an underline to the text on hover.
  • hover:border-2 hover:border-gray-500: Adds a 2px border on hover and sets it to gray.

2. Focus

Focus (focus) state refers to when the user interacts with a form element (such as an input field), the element gains focus. Usefocus:prefix, you can control the element's style when focused.

Syntax:

focus:{class}

For example,focus:ring-2 focus:ring-blue-600displays a blue ring when the element gains focus.

Example

<input class="border p-2 focus:outline-none focus:ring-2 focus:ring-blue-600" type="text" placeholder="Click me to focus!">

Try it »

When the user clicks or selects the input field, the input field will display a blue highlighted ring.

More examples:

  • focus:text-black: Turns the text color to black on focus.
  • focus:border-blue-500: Changes the input field's border color to blue on focus.
  • focus:ring-4 focus:ring-opacity-50 focus:ring-green-500: Displays a green ring on focus, highlights it, and sets opacity.

3. Active

Active (active) state refers to when an element is clicked by the user, the element's style will change. Useactive:prefix to control the style when clicked.

Syntax:

active:{class}

For example,active:bg-green-700changes the background color to green when the element is pressed.

Common usage:

Example

<button class="px-4 py-2 bg-blue-500 text-white active:bg-green-700">
Click me
</button>

Try it »

When the button is pressed, the background color turns green.green-700。

More examples:

  • active:text-gray-600: Turns the text color to gray on click.
  • active:ring-2 active:ring-opacity-50 active:ring-green-500: Adds a green ring highlight effect to the element on click.

4. Combined States

Tailwind CSS supports combining multiple state modifiers, allowing you to more precisely control style changes under interaction states. For example, you can combine hover, focus, active states, as well as responsive or dark mode.

Syntax:

{state}:{class}

For example,dark:md:hover:bg-fuchsia-600changes the background color in dark mode, at medium breakpoints, and on hover.

Example

<button class="px-4 py-2 bg-blue-500 text-white hover:bg-sky-700 focus:outline-none dark:hover:bg-fuchsia-600">
Hover or focus on me
</button>

Try it »

In normal mode, the button background color changes to on hoversky-700, and in dark mode, the background color changes tofuchsia-600。

More examples:

  • sm:focus:ring-2: On small screen devices, add a 2px highlighted ring when the element gains focus.
  • lg:active:bg-red-500: On large screen devices, change the background color to red when the button is clicked.

5. Pseudo-classes

Tailwind CSS also supports multiple pseudo-class modifiers, allowing you to apply styles based on the position or order of elements in the document.

Common pseudo-class modifiers:

  • first-child: Applies to the first child element.
  • last-child: Applies to the last child element.
  • odd-child: Applies to child elements at odd positions.
  • even-child: Applies to child elements at even positions.

Example

<ul class="space-y-4">
  <li class="first:font-bold last:font-light">Item 1</li>
  <li class="odd:bg-gray-100">Item 2</li>
  <li class="even:bg-gray-200">Item 3</li>
</ul>

Try it »
  • The first list itemItem 1will be bold, and the last list itemItem 3will become a lighter font weight.
  • Odd and even items are distinguished by different background colors.

6. Form States

Tailwind CSS provides some form state classes for controlling style changes of form elements in different states, such as required (required), invalid (invalid) and disabled (disabled) states.

Common classes:

  • required: Indicates that the input field is required.
  • invalid: Used for invalid form input.
  • disabled: Disabled form elements.

Example

<input class="border p-2 required:border-red-500 invalid:border-yellow-500 disabled:bg-gray-300" type="text" placeholder="Enter content">

Try it »
  • If the input field is invalid, a yellow border will be displayed.
  • If the input field is disabled, the background color will turn gray.

7. Styling Based on Parent State (Group Modifier)

Tailwind CSS allows you to affect the style of child elements when the parent element is hovered or focused. Using thegroup-hoverandgroup-focusmodifier, you can adjust the child element's style when the parent element's state changes.

Syntax:

group-{state}:{class}

For example,group-hover:bg-gray-300will set the child element's background color to gray when the parent element is hovered.

Example

<div class="group">
  <button class="px-4 py-2 bg-blue-500 text-white group-hover:bg-gray-300">
Hover over the parent
  </button>
</div>

Try it »

When the parent container is hovered, the button background color turns gray.

8. Responsive and Dark Mode

Tailwind CSS allows you to combine responsive prefixes and dark mode modifiers to apply different styles at different screen sizes and modes.

Syntax:

  • Responsive prefixes:sm:, md:, lg:, xl:, 2xl:
  • Dark mode prefix:dark:

Example

<button class="px-4 py-2 bg-blue-500 text-white dark:bg-gray-800 sm:hover:bg-blue-700 md:focus:ring-2 md:focus:ring-blue-500">
Responsive and dark mode button
</button>

Try it »

On small screens, the background color changes to on hoverblue-700; on medium screens, a blue highlighted ring is added on focus, and in dark mode, the background color turns gray.

9. Configuring Variants

Intailwind.config.jsIn the file, you can use thevariantssection to configure which state variants are enabled.

Example

module.exports = {
  theme: {
    extend: {},
  },
  variants: {
    extend: {
      backgroundColor: ['active', 'group-hover'],
      textColor: ['focus-visible', 'disabled'],
    },
  },
}

In this configuration,backgroundColoradds theactiveandgroup-hoverstate, andtextColoradds thefocus-visibleanddisabledstate.

10. First, Last, Odd and Even

When an element is the first child or the last element, usefirstandlastmodifier to set the element's style:

Example

<ul class="space-y-2">
  <li class="first:text-red-500">First item</li>
  <li>Second item</li>
  <li class="last:font-bold">Last item</li>
</ul>

Try it »

Effect:

  • The text color of the first list item changes to red text-red-500.
  • The font of the last list item becomes bold font-bold.

oddModifier: odd is used to select all odd-positioned child elements in the parent container (based on 1-based index).

Example

<ul class="space-y-2">
  <li class="odd:bg-gray-100">Item 1</li>
  <li>Item 2</li>
  <li class="odd:bg-gray-100">Item 3</li>
  <li>Item 4</li>
</ul>

Try it Yourself »

Effect:The background color of items 1 and 3 in the list changes to light gray bg-gray-100.

evenModifier: even is used to select all even-positioned child elements in the parent container (based on 1-based index).

Example

<ul class="space-y-2">
  <li>Item 1</li>
  <li class="even:bg-gray-200">Item 2</li>
  <li>Item 3</li>
  <li class="even:bg-gray-200">Item 4</li>
</ul>

Try it Yourself »

Effect:The background color of items 2 and 4 in the list changes to a darker gray bg-gray-200.

Modifier Reference

Modifier CSS corresponding pseudo-class/
pseudo-element or rule
Description Example code
hover :hover Applies on mouse hover <button class="hover:bg-blue-500">Hover me</button>
focus :focus Applies when the element gains focus <input class="focus:ring-2 focus:ring-blue-600" />
focus-within :focus-within Applies when any child element within the parent container gains focus <div class="focus-within:border-blue-500"><input /></div>
focus-visible :focus-visible Focus activated by keyboard navigation or assistive technologies <input class="focus-visible:ring-2 focus-visible:ring-blue-600" />
active :active Applies when the element is pressed or in an active state <button class="active:bg-green-700">Click me</button>
visited :visited Visited link <a class="visited:text-purple-600" href="../index.html">Visited link</a>
target :target Applies when the target link is activated <div id="section" class="target:bg-yellow-500"></div>
first :first-child First child element <ul><li class="first:text-red-500">First</li><li>Other</li></ul>
last :last-child Last child element <ul><li>Other</li><li class="last:font-bold">Last</li></ul>
odd :nth-child(odd) Odd child elements (counting from 1) <ul><li class="odd:bg-gray-100">Odd</li><li>Even</li></ul>
even :nth-child(even) Even child elements <ul><li>Odd</li><li class="even:bg-gray-200">Even</li></ul>
empty :empty Element with empty content <div class="empty:bg-gray-50"></div>
disabled :disabled Disabled form element <button class="disabled:opacity-50" disabled>Disabled</button>
enabled :enabled Enabled form element <input class="enabled:border-blue-500" />
checked :checked Checked checkbox or radio button <input type="checkbox" class="checked:bg-blue-500" checked />
required :required Form element is required <input class="required:border-red-500" required />
before ::before Pseudo-element inserted before the element <div class="before:content-['*'] before:text-red-500"></div>
after ::after Pseudo-element inserted after the element <div class="after:content-['!'] after:text-green-500"></div>
sm @media (min-width: 640px) Small screens (starting from 640px) <div class="sm:bg-blue-100">Resize to small screen</div>
dark @media (prefers-color-scheme: dark) Dark mode <div class="dark:bg-black dark:text-white">Dark mode content</div>
rtl [dir="rtl"] Right-to-left text direction <div dir="rtl" class="rtl:text-right">Right to Left</div>
aria-checked [aria-checked="true"] Apply styles to elements with ARIA attributes <div aria-checked="true" class="aria-checked:bg-green-500">Checked</div>
motion-safe @media (prefers-reduced-motion: no-preference) Animation preference: applies when no preference is set <div class="motion-safe:transition-transform motion-safe:duration-500"></div>
Other extensions