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:
hover:text-white: Change text color to white on hover.hover:underline: Add an underline to text on hover.hover:border-2 hover:border-gray-500: Add a 2px border on hover and set it to gray.
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:
focus:text-black: Change text color to black on focus.focus:border-blue-500: Change the input's border color to blue on focus.focus:ring-4 focus:ring-opacity-50 focus:ring-green-500: Display a green ring on focus, highlighted, with opacity set.
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:
active:text-gray-600: Change text color to gray on click.active:ring-2 active:ring-opacity-50 active:ring-green-500: Add a green ring highlight effect to the element on click.
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:
sm:focus:ring-2: On small screen devices, add a 2px highlight 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 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:
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
-
Item 1
-
Item 2
-
Item 3
Try it »
- The first list item
Item 1will be bold, and the last list itemItem 3will be given a lighter font. - 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 under different states, such as required (required), invalid (invalid), and disabled (disabled) states.
Common Classes:
required: Indicates the input field is required.invalid: For invalid form inputs.disabled: Disabled form elements.
Example
Try it »
- If the input is invalid, a yellow border is displayed.
- If the input is disabled, the background color turns gray.
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:
- Responsive Prefixes:
sm:,md:,lg:,xl:,2xl: - Dark Mode Prefixes:
dark:
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
-
First item
-
Second item
-
Last item
Try it »
Effect:
- The first list item's text color turns red with text-red-500.
- The last list item's font becomes bold with font-bold.
odd modifier: odd is used to select all child elements at odd positions in the parent container (based on 1-based indexing).
Example
-
Item 1
-
Item 2
-
Item 3
-
Item 4
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
-
Item 1
-
Item 2
-
Item 3
-
Item 4
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 | | | last | :last-child | Last child element | | | odd | :nth-child(odd) | Odd child elements (counting from 1) | | | even | :nth-child(even) | Even child elements | | | 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) | | | dark | @media (prefers-color-scheme: dark) | Dark mode | | | rtl | [dir="rtl"] | Right-to-left text direction | | | aria-checked | [aria-checked="true"] | Apply styles to elements with ARIA attributes | | | motion-safe | @media (prefers-reduced-motion: no-preference) | Animation preference: applied when there is no preference | |