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. backgroundAttachment: ['responsive'],
  12. backgroundClip: ['responsive'],
  13. backgroundColor: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  14. backgroundImage: ['responsive'],
  15. backgroundOpacity: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  16. backgroundPosition: ['responsive'],
  17. backgroundRepeat: ['responsive'],
  18. backgroundSize: ['responsive'],
  19. borderCollapse: ['responsive'],
  20. borderColor: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  21. borderOpacity: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  22. borderRadius: ['responsive'],
  23. borderStyle: ['responsive'],
  24. borderWidth: ['responsive'],
  25. boxShadow: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  26. boxSizing: ['responsive'],
  27. clear: ['responsive'],
  28. container: ['responsive'],
  29. cursor: ['responsive'],
  30. display: ['responsive'],
  31. divideColor: ['responsive', 'dark'],
  32. divideOpacity: ['responsive'],
  33. divideStyle: ['responsive'],
  34. divideWidth: ['responsive'],
  35. fill: ['responsive'],
  36. flex: ['responsive'],
  37. flexDirection: ['responsive'],
  38. flexGrow: ['responsive'],
  39. flexShrink: ['responsive'],
  40. flexWrap: ['responsive'],
  41. float: ['responsive'],
  42. fontFamily: ['responsive'],
  43. fontSize: ['responsive'],
  44. fontSmoothing: ['responsive'],
  45. fontStyle: ['responsive'],
  46. fontVariantNumeric: ['responsive'],
  47. fontWeight: ['responsive'],
  48. gap: ['responsive'],
  49. gradientColorStops: ['responsive', 'dark', 'hover', 'focus'],
  50. gridAutoColumns: ['responsive'],
  51. gridAutoFlow: ['responsive'],
  52. gridAutoRows: ['responsive'],
  53. gridColumn: ['responsive'],
  54. gridColumnEnd: ['responsive'],
  55. gridColumnStart: ['responsive'],
  56. gridRow: ['responsive'],
  57. gridRowEnd: ['responsive'],
  58. gridRowStart: ['responsive'],
  59. gridTemplateColumns: ['responsive'],
  60. gridTemplateRows: ['responsive'],
  61. height: ['responsive'],
  62. inset: ['responsive'],
  63. justifyContent: ['responsive'],
  64. justifyItems: ['responsive'],
  65. justifySelf: ['responsive'],
  66. letterSpacing: ['responsive'],
  67. lineHeight: ['responsive'],
  68. listStylePosition: ['responsive'],
  69. listStyleType: ['responsive'],
  70. margin: ['responsive'],
  71. maxHeight: ['responsive'],
  72. maxWidth: ['responsive'],
  73. minHeight: ['responsive'],
  74. minWidth: ['responsive'],
  75. objectFit: ['responsive'],
  76. objectPosition: ['responsive'],
  77. opacity: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  78. order: ['responsive'],
  79. outline: ['responsive', 'focus-within', 'focus'],
  80. overflow: ['responsive'],
  81. overscrollBehavior: ['responsive'],
  82. padding: ['responsive'],
  83. placeContent: ['responsive'],
  84. placeItems: ['responsive'],
  85. placeSelf: ['responsive'],
  86. placeholderColor: ['responsive', 'dark', 'focus'],
  87. placeholderOpacity: ['responsive', 'focus'],
  88. pointerEvents: ['responsive'],
  89. position: ['responsive'],
  90. resize: ['responsive'],
  91. ringColor: ['responsive', 'dark', 'focus-within', 'focus'],
  92. ringOffsetColor: ['responsive', 'dark', 'focus-within', 'focus'],
  93. ringOffsetWidth: ['responsive', 'focus-within', 'focus'],
  94. ringOpacity: ['responsive', 'focus-within', 'focus'],
  95. ringWidth: ['responsive', 'focus-within', 'focus'],
  96. rotate: ['responsive', 'hover', 'focus'],
  97. scale: ['responsive', 'hover', 'focus'],
  98. skew: ['responsive', 'hover', 'focus'],
  99. space: ['responsive'],
  100. stroke: ['responsive'],
  101. strokeWidth: ['responsive'],
  102. tableLayout: ['responsive'],
  103. textAlign: ['responsive'],
  104. textColor: ['responsive', 'dark', 'group-hover', 'focus-within', 'hover', 'focus'],
  105. textDecoration: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  106. textOpacity: ['responsive', 'group-hover', 'focus-within', 'hover', 'focus'],
  107. textOverflow: ['responsive'],
  108. textTransform: ['responsive'],
  109. transform: ['responsive'],
  110. transformOrigin: ['responsive'],
  111. transitionDelay: ['responsive'],
  112. transitionDuration: ['responsive'],
  113. transitionProperty: ['responsive'],
  114. transitionTimingFunction: ['responsive'],
  115. translate: ['responsive', 'hover', 'focus'],
  116. userSelect: ['responsive'],
  117. verticalAlign: ['responsive'],
  118. visibility: ['responsive'],
  119. whitespace: ['responsive'],
  120. width: ['responsive'],
  121. wordBreak: ['responsive'],
  122. zIndex: ['responsive', 'focus-within', 'focus']
  123. }
  124. }

←Spacing   Plugins→