Background Color
Utilities for controlling an element's background color.
Usage
Control the background color of an element using the .bg-{color}
utilities.
<button class="bg-blue-500 ...">Button</button>
Changing opacityv1.4.0+
Control the opacity of an element's background color using the .bg-opacity-{amount}
utilities.
<div class="bg-blue-500 bg-opacity-100"></div>
<div class="bg-blue-500 bg-opacity-75"></div>
<div class="bg-blue-500 bg-opacity-50"></div>
<div class="bg-blue-500 bg-opacity-25"></div>
<div class="bg-blue-500 bg-opacity-0"></div>
Learn more in the background opacity documentation.
Responsive
To control the background color of an element at a specific breakpoint, add a {screen}:
prefix to any existing background color utility. For example, use md:bg-green-500
to apply the bg-green-500
utility at only medium screen sizes and above.
For more information about Tailwind's responsive design features, check out the Responsive Design documentation.
<button class="bg-blue-500 sm:bg-green-500 md:bg-indigo-500 lg:bg-red-500 xl:bg-black ...">Button</button>
Hover
To control the background color of an element on hover, add the hover:
prefix to any existing background color utility. For example, use hover:bg-blue-500
to apply the bg-blue-500
utility on hover.
<button class="bg-blue-500 hover:bg-blue-700 ...">
Hover me
</button>
Hover utilities can also be combined with responsive utilities by adding the responsive {screen}:
prefix before the focus:
prefix.
<button class="... md:bg-blue-500 md:hover:bg-blue-700 ...">Button</button>
Focus
To control the background color of an element on focus, add the focus:
prefix to any existing background color utility. For example, use focus:bg-blue-500
to apply the bg-blue-500
utility on focus.
<input class="bg-gray-200 focus:bg-white ...">
Focus utilities can also be combined with responsive utilities by adding the responsive {screen}:
prefix before the focus:
prefix.
<input class="... md:bg-gray-200 md:focus:bg-white ...">
Customizing
Background Colors
By default Tailwind makes the entire default color palette available as background colors.
You can customize your color palette by editing the theme.colors
variable in your tailwind.config.js
file, or customize just your background colors using the theme.backgroundColor
section of your Tailwind config.
// tailwind.config.js
module.exports = {
theme: {
backgroundColor: theme => ({
- ...theme('colors'),
+ 'primary': '#3490dc',
+ 'secondary': '#ffed4a',
+ 'danger': '#e3342f',
})
}
}
Responsive and pseudo-class variants
By default, only responsive, hover and focus variants are generated for background color utilities.
You can control which variants are generated for the background color utilities by modifying the backgroundColor
property in the variants
section of your tailwind.config.js
file.
For example, this config will also generate active and group-hover variants:
// tailwind.config.js
module.exports = {
variants: {
// ...
- backgroundColor: ['responsive', 'hover', 'focus'],
+ backgroundColor: ['responsive', 'hover', 'focus', 'active', 'group-hover'],
}
}
Disabling
If you don't plan to use the background color utilities in your project, you can disable them entirely by setting the backgroundColor
property to false
in the corePlugins
section of your config file:
// tailwind.config.js
module.exports = {
corePlugins: {
// ...
+ backgroundColor: false,
}
}