Similar to our responsive prefixes, Tailwind makes it easy to style elements on hover, focus, and more using state prefixes.

Hover

Add the hover: prefix to only apply a utility on hover.

State Variants - 图1

State Variants - 图2

By default, hover variants are only generated for background color, border color, font weight, shadow, text color, and text style utilities.

You can customize this in the modules section of your configuration file.

Focus

Add the focus: prefix to only apply a utility on focus.

State Variants - 图3

State Variants - 图4

By default, focus variants are only generated for background color, border color, font weight, outline, shadow, text color, and text style utilities.

You can customize this in the modules section of your configuration file.

Active

Add the active: prefix to only apply a utility when an element is active.

State Variants - 图5

State Variants - 图6

By default, active variants are not generated for any utilities.

You can customize this in the modules section of your configuration file.

Group Hover

If you need to style a child element when hovering over a specific parent element, add the .group class to the parent element and add the group-hover: prefix to the utility on the child element.

State Variants - 图7

State Variants - 图8

By default, group hover variants are not generated for any utilities.

You can customize this in the modules section of your configuration file.

Focus-Within

State Variants - 图9

Add the focus-within: prefix to only apply a utility when a child element has focus.

State Variants - 图10

State Variants - 图11

By default, focus-within variants are not generated for any utilities.

You can customize this in the modules section of your configuration file.

Responsive Prefixes

State variants are also responsive, meaning you can change an element’s hover style for example at different breakpoints.

To apply a state variant responsively, add the responsive prefix first, before the state prefix.

  1. <button class="... md:bg-orange md:hover:bg-red ...">Button</button>

Custom Utilities

You can generate state variants for your own custom utilities using the @variants directive:

  1. // Input:
  2. @variants hover, focus {
  3. .banana {
  4. color: yellow;
  5. }
  6. }
  7. // Output:
  8. .banana {
  9. color: yellow;
  10. }
  11. .focus\:banana:focus {
  12. color: yellow;
  13. }
  14. .hover\:banana:hover {
  15. color: yellow;
  16. }

For more information, see the @variants directive documentation.