Configuring Variants

Configuring which utility variants are enabled in your project.

Overview

The variants section of your tailwind.config.js file is where you control which variants should be enabled for each core plugin:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. extend: {
  5. backgroundColor: ['active'],
  6. // ...
  7. borderColor: ['focus-visible', 'first'],
  8. // ...
  9. textColor: ['visited'],
  10. }
  11. },
  12. }

Each property is a core plugin name pointing to an array of variants to generate for that plugin.

The following variants are supported out of the box:

VariantDescription
responsiveResponsive variants like sm, md, lg, and xl.
darkTargets dark mode.
motion-safeTargets the prefers-reduced-motion: no-preference media query.
motion-reduceTargets the prefers-reduced-motion: reduce media query.
firstTargets the first-child pseudo-class.
lastTargets the last-child pseudo-class.
oddTargets the odd-child pseudo-class.
evenTargets the even-child pseudo-class.
visitedTargets the visited pseudo-class.
checkedTargets the checked pseudo-class.
group-hoverTargets an element when a marked parent matches the hover pseudo-class.
group-focusTargets an element when a marked parent matches the focus pseudo-class.
focus-withinTargets the focus-within pseudo-class.
hoverTargets the hover pseudo-class.
focusTargets the focus pseudo-class.
focus-visibleTargets the focus-visible pseudo-class.
activeTargets the active pseudo-class.
disabledTargets the disabled pseudo-class.

For more information about how variants work, read our documentation on responsive variants, dark mode variants, and hover, focus and other state variants.


Enabling extra variants

If you’d like to enable extra variants for a plugin in addition to the defaults, you can configure your variants using the extend keyword, similar to how you can use extend inside of the theme section:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // The 'active' variant will be generated in addition to the defaults
  5. extend: {
  6. backgroundColor: ['active']
  7. }
  8. },
  9. }

Because the order of variants is important, any variants added under the extend key are automatically ordered for you using a sensible default variant order. You can customize this order using the variantOrder option if necessary.


Overriding default variants

Any variants configured directly under the variants key will override the default variants for that plugin.

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // Only 'active' variants will be generated
  5. backgroundColor: ['active'],
  6. },
  7. }

When overriding the default variants, make sure you always specify all the variants you’d like to enable, not just the new ones you’d like to add.

Ordering variants

It’s important to note that when overriding variants, variants are generated in the order you specify them, so variants at the end of the list will take precedence over variants at the beginning of the list.

For example, here focus variants have the highest precedence for backgroundColor utilities, but hover variants have the highest precedence for borderColor utilities:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. backgroundColor: ['hover', 'focus'],
  5. borderColor: ['focus', 'hover'],
  6. },
  7. }
  1. /* Generated CSS */
  2. .bg-black { background-color: #000 }
  3. .bg-white { background-color: #fff }
  4. /* ... */
  5. .hover\:bg-black:hover { background-color: #000 }
  6. .hover\:bg-white:hover { background-color: #fff }
  7. /* ... */
  8. .focus\:bg-black:focus { background-color: #000 }
  9. .focus\:bg-white:focus { background-color: #fff }
  10. /* ... */
  11. .border-black { border-color: #000 }
  12. .border-white { border-color: #fff }
  13. /* ... */
  14. .focus\:border-black:focus { border-color: #000 }
  15. .focus\:border-white:focus { border-color: #fff }
  16. /* ... */
  17. .hover\:border-black:hover { border-color: #000 }
  18. .hover\:border-white:hover { border-color: #fff }
  19. /* ... */

This means that given the following HTML:

  1. <input class="focus:bg-white hover:bg-black focus:border-white hover:border-black">

…if the input was hovered and focused at the same time, the background would be white but the border would be black.

Generating variants in order this way gives you the most flexibility as an end-user, but it’s also a sharp tool and can have unintended consequences if you aren’t careful. We recommend enabling extra variants instead of overriding the defaults whenever possible, and using this feature only as an escape hatch.


Special variants

Responsive

The responsive variant is a special case in Tailwind and is not impacted by the order you list in your variants configuration.

This is because the responsive variant automatically stacks with other variants, meaning that if you specify both responsive and hover variants for a utility, Tailwind will generate responsive hover variants as well:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. backgroundColor: ['responsive', 'hover'],
  5. borderColor: ['responsive', 'focus'],
  6. },
  7. }

Responsive variants are grouped together and inserted at the end of your stylesheet by default to avoid specificity issues, regardless of where responsive appears in your variants list.

If you’d like to customize this behavior for whatever reason, you can use the @tailwind screens directive to specify where responsive variants should be inserted.

Dark, motion-safe, and motion-reduce

The dark, motion-safe, and motion-reduce variants also stack with other variants, but unlike responsive, they stack in the same “slot”, so you can combine them with both responsive and simple state variants, but not with each other.

The order of these variants matter relative to each other, but not relative to other variants. There is just about no situation imaginable where these would conflict with each other in practice, so this ends up being a non-issue anyways.

You can include these variants in any order in your variants configuration and never notice the difference.

Default

You can use the special DEFAULT variant to control where the normal, non-prefixed version of a utility is generated relative to the other variants.

This is an advanced feature and only really useful if you have a custom variant (like children in the example below) that should have a lower precedence than the normal version of a utility.

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. backgroundColor: ['children', 'DEFAULT', 'hover', 'focus'],
  5. },
  6. }
  1. /* Generated CSS */
  2. .children\:bg-black > * { background-color: #000; }
  3. .children\:bg-white > * { background-color: #fff; }
  4. .bg-black { background-color: #000 }
  5. .bg-white { background-color: #fff }
  6. /* ... */
  7. .hover\:bg-black:hover { background-color: #000 }
  8. .hover\:bg-white:hover { background-color: #fff }
  9. /* ... */
  10. .focus\:bg-black:focus { background-color: #000 }
  11. .focus\:bg-white:focus { background-color: #fff }
  12. /* ... */

Learn more about creating custom variants in the variant plugin documentation.


Using custom variants

If you’ve written or installed a plugin that adds a new variant, you can enable that variant by including it in your variants configuration just like if it were a built-in variant.

For example, the tailwindcss-interaction-variants plugin adds a group-disabled variant (among others):

  1. // tailwind.config.js
  2. {
  3. variants: {
  4. backgroundColor: ['responsive', 'hover', 'focus', 'group-disabled'],
  5. },
  6. plugins: [
  7. require('tailwindcss-interaction-variants'),
  8. ],
  9. }

Learn more about creating custom variants in the variant plugin documentation.

Ordering custom variants

If you’d like to specify a default sort position for a custom variant, override your variantOrder to include the custom variant:

  1. // tailwind.config.js
  2. module.exports = {
  3. // ...
  4. variantOrder: [
  5. 'first',
  6. 'last',
  7. 'odd',
  8. 'even',
  9. 'visited',
  10. 'checked',
  11. 'group-hover',
  12. 'group-focus',
  13. 'focus-within',
  14. 'hover',
  15. 'focus',
  16. 'focus-visible',
  17. 'active',
  18. 'group-disabled', // Custom variant
  19. 'disabled',
  20. ],
  21. variants: {
  22. extend: {
  23. backgroundColor: ['group-disabled'],
  24. }
  25. }
  26. }

You’ll need to specify the entire list when overriding the variantOrder to include any custom variants.


Default variants reference

Here is a complete reference of Tailwind’s default variants configuration, which can be useful when you’d like to add a new variant while preserving the defaults.

  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. }

←SpacingPlugins→

Edit this page on GitHub