Configuring Variants
Overview
The variants
section of your tailwind.config.js
file is where you control which core utility plugins should have responsive variants and pseudo-class variants generated.
// tailwind.config.js
module.exports = {
variants: {
appearance: ['responsive'],
// ...
borderColor: ['responsive', 'hover', 'focus'],
// ...
outline: ['responsive', 'focus'],
// ...
zIndex: ['responsive'],
},
}
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:
'responsive'
'group-hover'
'focus-within'
'first'
'last'
'odd'
'even'
'hover'
'focus'
'active'
'visited'
'disabled'
It's important to note that your array of variants is not merged with the defaults, so if you'd like to enable another variant for a utility, you need to repeat the default variants for that utility as well.
Don't list only the extra variants you want to enable
// tailwind.config.js
module.exports = {
variants: {
backgroundColor: ['active'],
},
}
Always provide the complete list of variants you want to enable
// tailwind.config.js
module.exports = {
variants: {
backgroundColor: ['responsive', 'hover', 'focus', 'active'],
},
}
Ordering variants
It's important to note that 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.
In this example, focus
variants have the highest precedence for backgroundColor
utilities, but hover
variants have the highest precedence for borderColor
utilities:
// tailwind.config.js
module.exports = {
variants: {
backgroundColor: ['hover', 'focus'],
borderColor: ['focus', 'hover'],
},
}
/* Generated CSS */
.bg-black { background-color: #000 }
.bg-white { background-color: #fff }
/* ... */
.hover\:bg-black:hover { background-color: #000 }
.hover\:bg-white:hover { background-color: #fff }
/* ... */
.focus\:bg-black:focus { background-color: #000 }
.focus\:bg-white:focus { background-color: #fff }
/* ... */
.border-black { border-color: #000 }
.border-white { border-color: #fff }
/* ... */
.focus\:border-black:focus { border-color: #000 }
.focus\:border-white:focus { border-color: #fff }
/* ... */
.hover\:border-black:hover { border-color: #000 }
.hover\:border-white:hover { border-color: #fff }
/* ... */
This means that given the following HTML:
<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.
Generally, we recommend the following order for the built-in variants, although you are free to use whatever order makes the most sense for your own project:
['responsive', 'group-hover', 'group-focus', 'focus-within', 'first', 'last', 'odd', 'even', 'hover', 'focus', 'active', 'visited', 'disabled']
The responsive variant
The responsive
variant is the only variant that is not impacted by the order you list in your variants configuration.
This is because the responsive
variant automatically stacks with pseudo-class variants, meaning that if you specify both responsive
and hover
variants for a utility, Tailwind will generate responsive hover variants as well:
// tailwind.config.js
module.exports = {
variants: {
backgroundColor: ['responsive', 'hover'],
borderColor: ['responsive', 'focus'],
},
}
/* Generated CSS */
.bg-black { background-color: #000 }
/* ... */
.hover\:bg-black:hover { background-color: #000 }
/* ... */
.border-black { border-color: #000 }
/* ... */
.focus\:border-black:focus { border-color: #000 }
/* ... */
@media (min-width: 640px) {
.sm\:bg-black { background-color: #000 }
/* ... */
.sm\:hover\:bg-black:hover { background-color: #000 }
/* ... */
.sm\:border-black { border-color: #000 }
/* ... */
.sm\:focus\:border-black:focus { border-color: #000 }
/* ... */
}
@media (min-width: 768px) {
.md\:bg-black { background-color: #000 }
/* ... */
.md\:hover\:bg-black:hover { background-color: #000 }
/* ... */
.md\:border-black { border-color: #000 }
/* ... */
.md\:focus\:border-black:focus { border-color: #000 }
/* ... */
}
@media (min-width: 1024px) {
.lg\:bg-black { background-color: #000 }
/* ... */
.lg\:hover\:bg-black:hover { background-color: #000 }
/* ... */
.lg\:border-black { border-color: #000 }
/* ... */
.lg\:focus\:border-black:focus { border-color: #000 }
/* ... */
}
@media (min-width: 1280px) {
.xl\:bg-black { background-color: #000 }
/* ... */
.xl\:hover\:bg-black:hover { background-color: #000 }
/* ... */
.xl\:border-black { border-color: #000 }
/* ... */
.xl\:focus\:border-black:focus { border-color: #000 }
/* ... */
}
Responsive variants are grouped together and inserted at the end of your stylesheet by default to avoid specificity issues. 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.
The default variant
You can use the special default
variant to control where the normal, non-prefixed versions of a utility are generated relative to the other variants.
// tailwind.config.js
module.exports = {
variants: {
backgroundColor: ['hover', 'default', 'focus'],
},
}
/* Generated CSS */
.hover\:bg-black:hover { background-color: #000 }
.hover\:bg-white:hover { background-color: #fff }
/* ... */
.bg-black { background-color: #000 }
.bg-white { background-color: #fff }
/* ... */
.focus\:bg-black:focus { background-color: #000 }
.focus\:bg-white:focus { background-color: #fff }
/* ... */
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.
// tailwind.config.js
module.exports = {
variants: {
backgroundColor: ['children', 'default', 'hover', 'focus'],
},
}
/* Generated CSS */
.children\:bg-black > * { background-color: #000; }
.children\:bg-white > * { background-color: #fff; }
.bg-black { background-color: #000 }
.bg-white { background-color: #fff }
/* ... */
.hover\:bg-black:hover { background-color: #000 }
.hover\:bg-white:hover { background-color: #fff }
/* ... */
.focus\:bg-black:focus { background-color: #000 }
.focus\:bg-white:focus { background-color: #fff }
/* ... */
Learn more about creating custom variants in the variant plugin documentation.
Enabling all variants
To specify a global set of variants that should be applied to all utilities, you can assign an array of variants directly to the variants
property:
// tailwind.config.js
module.exports = {
variants: ['responsive', 'group-hover', 'group-focus', 'focus-within', 'first', 'last', 'odd', 'even', 'hover', 'focus', 'active', 'visited', 'disabled']
}
Note that enabling all variants for all plugins will result in much bigger file sizes. Before you do this, be sure to read our guide on controlling file size.
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 visited
variant (among others):
// tailwind.config.js
{
variants: {
backgroundColor: ['responsive', 'hover', 'focus', 'visited'],
},
plugins: [
require('tailwindcss-interaction-variants')(),
],
}
Learn more about creating custom variants in the variant plugin documentation.
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.
// Default configuration
module.exports = {
// ...
variants: {
accessibility: ['responsive', 'focus']
alignContent: ['responsive']
alignItems: ['responsive']
alignSelf: ['responsive']
appearance: ['responsive']
backgroundAttachment: ['responsive']
backgroundColor: ['responsive', 'hover', 'focus']
backgroundOpacity: ['responsive', 'hover', 'focus']
backgroundPosition: ['responsive']
backgroundRepeat: ['responsive']
backgroundSize: ['responsive']
borderCollapse: ['responsive']
borderColor: ['responsive', 'hover', 'focus']
borderOpacity: ['responsive', 'hover', 'focus']
borderRadius: ['responsive']
borderStyle: ['responsive']
borderWidth: ['responsive']
boxShadow: ['responsive', 'hover', 'focus']
boxSizing: ['responsive']
container: ['responsive']
cursor: ['responsive']
display: ['responsive']
divideColor: ['responsive']
divideOpacity: ['responsive']
divideWidth: ['responsive']
fill: ['responsive']
flex: ['responsive']
flexDirection: ['responsive']
flexGrow: ['responsive']
flexShrink: ['responsive']
flexWrap: ['responsive']
float: ['responsive']
clear: ['responsive']
fontFamily: ['responsive']
fontSize: ['responsive']
fontSmoothing: ['responsive']
fontStyle: ['responsive']
fontWeight: ['responsive', 'hover', 'focus']
height: ['responsive']
inset: ['responsive']
justifyContent: ['responsive']
letterSpacing: ['responsive']
lineHeight: ['responsive']
listStylePosition: ['responsive']
listStyleType: ['responsive']
margin: ['responsive']
maxHeight: ['responsive']
maxWidth: ['responsive']
minHeight: ['responsive']
minWidth: ['responsive']
objectFit: ['responsive']
objectPosition: ['responsive']
opacity: ['responsive', 'hover', 'focus']
order: ['responsive']
outline: ['responsive', 'focus']
overflow: ['responsive']
overscrollBehavior: ['responsive']
padding: ['responsive']
placeholderColor: ['responsive', 'focus']
placeholderOpacity: ['responsive', 'focus']
pointerEvents: ['responsive']
position: ['responsive']
resize: ['responsive']
space: ['responsive']
stroke: ['responsive']
strokeWidth: ['responsive']
tableLayout: ['responsive']
textAlign: ['responsive']
textColor: ['responsive', 'hover', 'focus']
textOpacity: ['responsive', 'hover', 'focus']
textDecoration: ['responsive', 'hover', 'focus']
textTransform: ['responsive']
userSelect: ['responsive']
verticalAlign: ['responsive']
visibility: ['responsive']
whitespace: ['responsive']
width: ['responsive']
wordBreak: ['responsive']
zIndex: ['responsive']
gap: ['responsive']
gridAutoFlow: ['responsive']
gridTemplateColumns: ['responsive']
gridColumn: ['responsive']
gridColumnStart: ['responsive']
gridColumnEnd: ['responsive']
gridTemplateRows: ['responsive']
gridRow: ['responsive']
gridRowStart: ['responsive']
gridRowEnd: ['responsive']
transform: ['responsive']
transformOrigin: ['responsive']
scale: ['responsive', 'hover', 'focus']
rotate: ['responsive', 'hover', 'focus']
translate: ['responsive', 'hover', 'focus']
skew: ['responsive', 'hover', 'focus']
transitionProperty: ['responsive']
transitionTimingFunction: ['responsive']
transitionDuration: ['responsive']
transitionDelay: ['responsive']
animation: ['responsive']
}
}