Hover, Focus, & Other States

Using utilities to style elements on hover, focus, and more.

Overview

Similar to how Tailwind handles responsive design, styling elements on hover, focus, and more can be accomplished by prefixing utilities with the appropriate state variant.

Hover, Focus, & Other States - 图1

  1. <form>
  2. <input class="border border-transparent focus:outline-none focus:ring-2 focus:ring-purple-600 focus:border-transparent ...">
  3. <button class="bg-purple-600 hover:bg-purple-700 focus:outline-none focus:ring-2 focus:ring-purple-600 focus:ring-opacity-50 ...">
  4. Sign up
  5. </button>
  6. </form>

Not all state variants are enabled for all utilities by default due to file-size considerations, but we’ve tried our best to enable the most commonly used combinations out of the box.

For a complete list of which variants are enabled by default, see the reference table at the end of this page.

If you need to target a state that Tailwind doesn’t support, you can extend the supported variants by writing a variant plugin.


Hover

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

Hover, Focus, & Other States - 图2

  1. <button class="bg-red-500 hover:bg-red-700 ...">
  2. Hover me
  3. </button>

By default, the hover variant is enabled for the following core plugins:

  • backgroundColor
  • backgroundOpacity
  • borderColor
  • borderOpacity
  • boxShadow
  • gradientColorStops
  • opacity
  • rotate
  • scale
  • skew
  • textColor
  • textDecoration
  • textOpacity
  • translate

You can control whether hover variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. padding: ['hover'],
  7. }
  8. },
  9. }

Focus

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

Hover, Focus, & Other States - 图3

  1. <input class="focus:ring-2 focus:ring-blue-600 ...">

By default, the focus variant is enabled for the following core plugins:

  • accessibility
  • backgroundColor
  • backgroundOpacity
  • borderColor
  • borderOpacity
  • boxShadow
  • gradientColorStops
  • opacity
  • outline
  • placeholderColor
  • placeholderOpacity
  • ringColor
  • ringOffsetColor
  • ringOffsetWidth
  • ringOpacity
  • ringWidth
  • rotate
  • scale
  • skew
  • textColor
  • textDecoration
  • textOpacity
  • translate
  • zIndex

You can control whether focus variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. maxHeight: ['focus'],
  7. }
  8. },
  9. }

Active

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

Hover, Focus, & Other States - 图4

  1. <button class="bg-green-500 active:bg-green-700 ...">
  2. Click me
  3. </button>

By default, the active variant is not enabled for any core plugins.

You can control whether active variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. backgroundColor: ['active'],
  7. }
  8. },
  9. }

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.

Hover, Focus, & Other States - 图5

  1. <div class="group border-indigo-500 hover:bg-white hover:shadow-lg hover:border-transparent ...">
  2. <p class="text-indigo-600 group-hover:text-gray-900 ...">New Project</p>
  3. <p class="text-indigo-500 group-hover:text-gray-500 ...">Create a new project from a variety of starting templates.</p>
  4. </div>

By default, the group-hover variant is enabled for the following core plugins:

  • backgroundColor
  • backgroundOpacity
  • borderColor
  • borderOpacity
  • boxShadow
  • opacity
  • textColor
  • textDecoration
  • textOpacity

You can control whether group-hover variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. divideColor: ['group-hover'],
  7. }
  8. },
  9. }

Group-focus

The group-focus variant works just like group-hover except for focus:

Hover, Focus, & Other States - 图6

  1. <button class="group bg-yellow-500 focus:bg-yellow-600 ...">
  2. <svg class="text-white group-focus:text-yellow-300 ..."></svg>
  3. Bookmark
  4. </button>

By default, the group-focus variant is not enabled for any core plugins.

You can control whether group-focus variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. backgroundColor: ['group-focus'],
  7. }
  8. },
  9. }

Focus-within

Add the focus-within: prefix to only apply a utility when that element or any of its descendants have focus.

Hover, Focus, & Other States - 图7

  1. <form>
  2. <div class="text-gray-400 focus-within:text-gray-600 ...">
  3. <div class="...">
  4. <svg fill="currentColor"></svg>
  5. </div>
  6. <input class="focus:ring-2 focus:ring-gray-300 ...">
  7. </div>
  8. </form>

By default, the focus-within variant is enabled for the following core plugins:

  • accessibility
  • backgroundColor
  • backgroundOpacity
  • borderColor
  • borderOpacity
  • boxShadow
  • opacity
  • outline
  • ringColor
  • ringOffsetColor
  • ringOffsetWidth
  • ringOpacity
  • ringWidth
  • textColor
  • textDecoration
  • textOpacity
  • zIndex

You can control whether focus-within variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. scale: ['focus-within'],
  7. }
  8. },
  9. }

Focus-visible

Hover, Focus, & Other States - 图8

Note that focus-visible currently requires JS and PostCSS polyfills for sufficient browser support.

Add the focus-visible: prefix to only apply a utility when an element has focus but only if the user is using the keyboard.

Hover, Focus, & Other States - 图9

  1. <button class="focus:ring-2 focus:ring-red-500 ...">
  2. Ring on focus
  3. </button>
  4. <button class="focus:outline-none focus-visible:ring-2 focus-visible:ring-rose-500 ...">
  5. Ring on focus-visible
  6. </button>

Note that currently only Chrome, Edge, and Firefox support focus-visible natively, so for sufficient browser support you should install and configure both the focus-visible JS polyfill and the focus-visible PostCSS polyfill. Make sure to include the PostCSS plugin after Tailwind in your list of PostCSS plugins:

  1. // postcss.config.js
  2. module.exports = {
  3. plugins: {
  4. tailwindcss: {},
  5. 'postcss-focus-visible': {},
  6. autoprefixer: {}
  7. }
  8. }

By default, the focus-visible variant is not enabled for any core plugins.

You can control whether focus-visible variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. textDecoration: ['focus-visible'],
  7. }
  8. },
  9. }

Motion-safe

Add the motion-safe: prefix to only apply a utility when the prefers-reduced-motion media feature matches no-preference.

For example, this button will only animate on hover if the user hasn’t enabled “Reduce motion” on their system.

Hover, Focus, & Other States - 图10

  1. <button class="transform motion-safe:hover:scale-110 ...">
  2. Hover me
  3. </button>

Note that unlike most other variants, motion-safe can be combined with both responsive variants and other variants like hover, by stacking them in this order:

  1. <div class="sm:motion-safe:hover:animate-spin">
  2. <!-- ... -->
  3. </div>

By default, the motion-safe variant is not enabled for any core plugins.

You can control whether motion-safe variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. animation: ['motion-safe'],
  7. }
  8. },
  9. }

Motion-reduce

Add the motion-reduce: prefix to only apply a utility when the prefers-reduced-motion media feature matches reduce.

For example, this button will animate on hover by default, but the animations will be disabled if the user has enabled “Reduce motion” on their system.

Hover, Focus, & Other States - 图11

  1. <button class="transform hover:scale-110 motion-reduce:transform-none ...">
  2. Hover me
  3. </button>

Note that unlike most other variants, motion-reduce can be combined with both responsive variants and other variants like hover, by stacking them in this order:

  1. <div class="sm:motion-reduce:hover:animate-none">
  2. <!-- ... -->
  3. </div>

By default, the motion-reduce variant is not enabled for any core plugins.

You can control whether motion-reduce variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. animation: ['motion-reduce'],
  7. }
  8. },
  9. }

Disabled

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

Hover, Focus, & Other States - 图12

  1. <button class="disabled:opacity-50 ...">
  2. Submit
  3. </button>
  4. <button class="disabled:opacity-50 ..." disabled>
  5. Submit
  6. </button>

By default, the disabled variant is not enabled for any core plugins.

You can control whether disabled variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. opacity: ['disabled'],
  7. }
  8. },
  9. }

Visited

Add the visited: prefix to only apply a utility when a link has been visited.

Hover, Focus, & Other States - 图13

  1. <a href="#" class="text-blue-600 visited:text-purple-600 ...">Link</a>

By default, the visited variant is not enabled for any core plugins.

You can control whether visited variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. textColor: ['visited'],
  7. }
  8. },
  9. }

Checked

Add the checked: prefix to only apply a utility when a radio or checkbox is currently checked.

Hover, Focus, & Other States - 图14

  1. <input type="checkbox" class="appearance-none checked:bg-blue-600 checked:border-transparent ...">

By default, the checked variant is not enabled for any core plugins.

You can control whether checked variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. backgroundColor: ['checked'],
  7. borderColor: ['checked'],
  8. }
  9. },
  10. }

First-child

Add the first: prefix to only apply a utility when it is the first-child of its parent. This is mostly useful when elements are being generated in some kind of loop.

Hover, Focus, & Other States - 图15

  1. <div class="...">
  2. <div v-for="item in items" class="transform first:rotate-45 ...">
  3. {{ item }}
  4. </div>
  5. </div>

It’s important to note that you should add any first: utilities to the child element, not the parent element.

By default, the first-child variant is not enabled for any core plugins.

You can control whether first variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. borderWidth: ['first'],
  7. }
  8. },
  9. }

Last-child

Add the last: prefix to only apply a utility when it is the last-child of its parent. This is mostly useful when elements are being generated in some kind of loop.

Hover, Focus, & Other States - 图16

  1. <div class="...">
  2. <div v-for="item in items" class="transform last:rotate-45 ...">
  3. {{ item }}
  4. </div>
  5. </div>

It’s important to note that you should add any last: utilities to the child element, not the parent element.

By default, the last-child variant is not enabled for any core plugins.

You can control whether last variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. borderWidth: ['last'],
  7. }
  8. },
  9. }

Odd-child

Add the odd: prefix to only apply a utility when it is an odd-child of its parent. This is mostly useful when elements are being generated in some kind of loop.

Hover, Focus, & Other States - 图17

  1. <div class="...">
  2. <div v-for="item in items" class="transform odd:rotate-45 ...">
  3. {{ item }}
  4. </div>
  5. </div>

It’s important to note that you should add any odd: utilities to the child element, not the parent element.

By default, the odd-child variant is not enabled for any core plugins.

You can control whether odd variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. backgroundColor: ['odd'],
  7. }
  8. },
  9. }

Even-child

Add the even: prefix to only apply a utility when it is an even-child of its parent. This is mostly useful when elements are being generated in some kind of loop.

Hover, Focus, & Other States - 图18

  1. <div class="...">
  2. <div v-for="item in items" class="transform even:rotate-45 ...">
  3. {{ item }}
  4. </div>
  5. </div>

It’s important to note that you should add any even: utilities to the child element, not the parent element.

By default, the even-child variant is not enabled for any core plugins.

You can control whether even variants are enabled for a plugin in the variants section of your tailwind.config.js file:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variants: {
  5. extend: {
  6. backgroundColor: ['even'],
  7. }
  8. },
  9. }

Combining with responsive prefixes

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

To apply a state variant at a specific breakpoint, add the responsive prefix first, before the state prefix:

  1. <button class="... hover:bg-green-500 sm:hover:bg-blue-500">
  2. <!-- ... -->
  3. </button>

Generating variants for custom utilities

You can generate state variants for your own custom CSS classes by wrapping them with the @variants directive:

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

For more information, see the @variants directive documentation.


Creating custom variants

You can create your own variants for any states Tailwind doesn’t support by default by writing a custom variant plugin.

For example, this simple plugin adds support for the required pseudo-class variant:

  1. // tailwind.config.js
  2. const plugin = require('tailwindcss/plugin')
  3. module.exports = {
  4. plugins: [
  5. plugin(function({ addVariant, e }) {
  6. addVariant('required', ({ modifySelectors, separator }) => {
  7. modifySelectors(({ className }) => {
  8. return `.${e(`required${separator}${className}`)}:required`
  9. })
  10. })
  11. })
  12. ]
  13. }

Learn more about writing variant plugins in the variant plugin documentation.


Default variants reference

Due to file-size considerations, Tailwind does not include all variants for all utilities by default.

To configure which variants are enabled for your project, see the configuring variants documentation.

  1. // Default configuration
  2. module.exports = {
  3. // ...
  4. variants: {
  5. accessibility: ['responsive', 'focus-within', 'focus'],
  6. alignContent: ['responsive'],
  7. alignItems: ['responsive'],
  8. alignSelf: ['responsive'],
  9. animation: ['responsive'],
  10. appearance: ['responsive'],
  11. backdropBlur: ['responsive'],
  12. backdropBrightness: ['responsive'],
  13. backdropContrast: ['responsive'],
  14. backdropFilter: ['responsive'],
  15. backdropGrayscale: ['responsive'],
  16. backdropHueRotate: ['responsive'],
  17. backdropInvert: ['responsive'],
  18. backdropOpacity: ['responsive'],
  19. backdropSaturate: ['responsive'],
  20. backdropSepia: ['responsive'],
  21. backgroundAttachment: ['responsive'],
  22. backgroundBlendMode: ['responsive'],
  23. backgroundClip: ['responsive'],
  24. backgroundColor: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  25. backgroundImage: ['responsive'],
  26. backgroundOpacity: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  27. backgroundPosition: ['responsive'],
  28. backgroundRepeat: ['responsive'],
  29. backgroundSize: ['responsive'],
  30. backgroundOrigin: ['responsive'],
  31. blur: ['responsive'],
  32. borderCollapse: ['responsive'],
  33. borderColor: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  34. borderOpacity: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  35. borderRadius: ['responsive'],
  36. borderStyle: ['responsive'],
  37. borderWidth: ['responsive'],
  38. boxDecorationBreak: ['responsive'],
  39. boxShadow: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  40. boxSizing: ['responsive'],
  41. brightness: ['responsive'],
  42. clear: ['responsive'],
  43. container: ['responsive'],
  44. contrast: ['responsive'],
  45. cursor: ['responsive'],
  46. display: ['responsive'],
  47. divideColor: ['responsive', 'dark'],
  48. divideOpacity: ['responsive', 'dark'],
  49. divideStyle: ['responsive'],
  50. divideWidth: ['responsive'],
  51. dropShadow: ['responsive'],
  52. fill: ['responsive'],
  53. filter: ['responsive'],
  54. flex: ['responsive'],
  55. flexDirection: ['responsive'],
  56. flexGrow: ['responsive'],
  57. flexShrink: ['responsive'],
  58. flexWrap: ['responsive'],
  59. float: ['responsive'],
  60. fontFamily: ['responsive'],
  61. fontSize: ['responsive'],
  62. fontSmoothing: ['responsive'],
  63. fontStyle: ['responsive'],
  64. fontVariantNumeric: ['responsive'],
  65. fontWeight: ['responsive'],
  66. gap: ['responsive'],
  67. gradientColorStops: ['responsive', 'dark', 'hover', 'focus'],
  68. grayscale: ['responsive'],
  69. gridAutoColumns: ['responsive'],
  70. gridAutoFlow: ['responsive'],
  71. gridAutoRows: ['responsive'],
  72. gridColumn: ['responsive'],
  73. gridColumnEnd: ['responsive'],
  74. gridColumnStart: ['responsive'],
  75. gridRow: ['responsive'],
  76. gridRowEnd: ['responsive'],
  77. gridRowStart: ['responsive'],
  78. gridTemplateColumns: ['responsive'],
  79. gridTemplateRows: ['responsive'],
  80. height: ['responsive'],
  81. hueRotate: ['responsive'],
  82. inset: ['responsive'],
  83. invert: ['responsive'],
  84. isolation: ['responsive'],
  85. justifyContent: ['responsive'],
  86. justifyItems: ['responsive'],
  87. justifySelf: ['responsive'],
  88. letterSpacing: ['responsive'],
  89. lineHeight: ['responsive'],
  90. listStylePosition: ['responsive'],
  91. listStyleType: ['responsive'],
  92. margin: ['responsive'],
  93. maxHeight: ['responsive'],
  94. maxWidth: ['responsive'],
  95. minHeight: ['responsive'],
  96. minWidth: ['responsive'],
  97. mixBlendMode: ['responsive'],
  98. objectFit: ['responsive'],
  99. objectPosition: ['responsive'],
  100. opacity: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  101. order: ['responsive'],
  102. outline: ['responsive', 'focus-within', 'focus'],
  103. overflow: ['responsive'],
  104. overscrollBehavior: ['responsive'],
  105. padding: ['responsive'],
  106. placeContent: ['responsive'],
  107. placeItems: ['responsive'],
  108. placeSelf: ['responsive'],
  109. placeholderColor: ['responsive', 'dark', 'focus'],
  110. placeholderOpacity: ['responsive', 'dark', 'focus'],
  111. pointerEvents: ['responsive'],
  112. position: ['responsive'],
  113. resize: ['responsive'],
  114. ringColor: ['responsive', 'dark', 'focus-within', 'focus'],
  115. ringOffsetColor: ['responsive', 'dark', 'focus-within', 'focus'],
  116. ringOffsetWidth: ['responsive', 'focus-within', 'focus'],
  117. ringOpacity: ['responsive', 'dark', 'focus-within', 'focus'],
  118. ringWidth: ['responsive', 'focus-within', 'focus'],
  119. rotate: ['responsive', 'hover', 'focus'],
  120. saturate: ['responsive'],
  121. scale: ['responsive', 'hover', 'focus'],
  122. sepia: ['responsive'],
  123. skew: ['responsive', 'hover', 'focus'],
  124. space: ['responsive'],
  125. stroke: ['responsive'],
  126. strokeWidth: ['responsive'],
  127. tableLayout: ['responsive'],
  128. textAlign: ['responsive'],
  129. textColor: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  130. textDecoration: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  131. textOpacity: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  132. textOverflow: ['responsive'],
  133. textTransform: ['responsive'],
  134. transform: ['responsive'],
  135. transformOrigin: ['responsive'],
  136. transitionDelay: ['responsive'],
  137. transitionDuration: ['responsive'],
  138. transitionProperty: ['responsive'],
  139. transitionTimingFunction: ['responsive'],
  140. translate: ['responsive', 'hover', 'focus'],
  141. userSelect: ['responsive'],
  142. verticalAlign: ['responsive'],
  143. visibility: ['responsive'],
  144. whitespace: ['responsive'],
  145. width: ['responsive'],
  146. wordBreak: ['responsive'],
  147. zIndex: ['responsive', 'focus-within', 'focus']
  148. }
  149. }

←Responsive Design Dark Mode→

Edit this page on GitHub