Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS State Handling

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 interactive effects. By combining these state classes, you can easily implement style changes under hover, focus, click, and other states, improving user experience.

State Class Prefixes:

State Prefix Purpose
Hover State hover: Controls style changes of an element when the mouse hovers over it
Focus State focus: Controls style changes of an element when it gains focus
Active state active: Controls style changes of an element when it is clicked
Visited State visited: Controls style changes of visited links
Focus-within State focus-within: Controls style changes of a parent element when one of its children gains focus
Combinations of States No Specific Prefix You can combine multiple state prefixes at the same time, such as hover:, focus:, and active:

1. Hover

Hover (hover) state refers to the style of an element changing when the user hovers the mouse over it. Using the hover: prefix, you can easily achieve this effect.

Syntax:

hover:{class}

For example,hover:bg-sky-700 changes the element's background color to on mouse hover sky-700。

Common usage:

Example

Try it »

When hovering, the button background color changes from blue-500 becomes sky-700。

More Examples:

2. Focus

Focus (focus) state refers to an element gaining focus when the user interacts with a form element (such as an input). Using the focus: prefix, you can control the style of an element when it is focused.

Syntax:

focus:{class}

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

Example

Try it »

When the user clicks or selects the input, it displays a blue highlight ring.

More Examples:

3. Active

Active (active) state refers to the style of an element changing when it is clicked by the user. Using the active: prefix, you can control the style on click.

Syntax:

active:{class}

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

Common usage:

Example

Try it »

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

More Examples:

4. Combining States

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

Syntax:

{state}:{class}

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

Example

Try it »

In normal mode, the button background color changes to on hover sky-700, while in dark mode, the background color changes to fuchsia-600。

More Examples:

5. Pseudo-classes

Tailwind CSS also supports a variety of pseudo-class modifiers, allowing you to apply styles based on an element's position or order in the document.

Common Pseudo-class Modifiers:

Example

Try it »

6. Form States

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

Common Classes:

Example

Try it »


7. Styling Based on Parent State (Group Modifier)

Tailwind CSS allows affecting child element styles when the parent is hovered or focused. Through the group-hover and group-focus modifier, you can adjust child element styles when the parent state changes.

Syntax:

group-{state}:{class}

For example,group-hover:bg-gray-300 applies a gray background color to the child element when the parent is hovered.

Example

 

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:

Example

Try it »

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

9. Configuring Variants

in tailwind.config.js file, you can variants section to configure which state variants are enabled. This way, you can enable or disable style changes for specific states as needed by your project.

Example

module.

exports

=

{

  theme

:

{

    extend

:

{

}

,

 

}

,

  variants

:

{

    extend

:

{

      backgroundColor

:

[

'active'

,

'group-hover'

]

,

      textColor

:

[

'focus-visible'

,

'disabled'

]

,

   

}

,

 

}

,

}

In this configuration,backgroundColor adds active and group-hover state, while textColor adds focus-visible and disabled status.

10. First, Last, Odd, and Even

When an element is the first or last child, use the first and last modifiers to style the element:

Example

Try it »

Effect:

odd modifier: odd is used to select all child elements at odd positions in the parent container (based on 1-based indexing).

Example

Try it »

**Effect:** The 1st and 3rd items in the list have their background color change to light gray with bg-gray-100.

even modifier: even is used to select all child elements at even positions in the parent container (based on 1-based indexing).

Example

Try it »

**Effect:** The 2nd and 4th items in the list have their background color change to a darker gray with bg-gray-200.

Modifier Reference

| Modifier | Corresponding CSS pseudo-class/
pseudo-element or rule
| Description | Example Code | | --- | --- | --- | --- | | hover | :hover | Applied on mouse hover | | | focus | :focus | Applied when the element gains focus | | | focus-within | :focus-within | Applied when any child within the parent container gains focus |

| | focus-visible | :focus-visible | Focus activated by keyboard navigation or assistive technology | | | active | :active | Applied when the element is pressed or in an active state | | | visited | :visited | Visited links | Visited link | | target | :target | Applied when the target link is activated |
| | first | :first-child | First child element |
  • First
  • Other
| | last | :last-child | Last child element |
  • Other
  • Last
| | odd | :nth-child(odd) | Odd child elements (counting from 1) |
  • Odd
  • Even
| | even | :nth-child(even) | Even child elements |
  • Odd
  • Even
| | empty | :empty | Elements with empty content |
| | disabled | :disabled | Disabled form elements | | | enabled | :enabled | Enabled form elements | | | checked | :checked | Checked checkboxes or radio buttons | | | required | :required | Form elements that are required | | | before | ::before | Pseudo-element inserted before the element |
| | after | ::after | Pseudo-element inserted after the element |
| | sm | @media (min-width: 640px) | Small screens (from 640px) |
Resize to small screen
| | dark | @media (prefers-color-scheme: dark) | Dark mode |
Dark mode content
| | rtl | [dir="rtl"] | Right-to-left text direction |
Right to Left
| | aria-checked | [aria-checked="true"] | Apply styles to elements with ARIA attributes |
Checked
| | motion-safe | @media (prefers-reduced-motion: no-preference) | Animation preference: applied when there is no preference |
|