Background Color
Usage
Control the background color of an element using the utilities.
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-50"></div>
<div class="bg-blue-500 bg-opacity-25"></div>
<div class="bg-blue-500 bg-opacity-0"></div>
Learn more in the .
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.
all
sm
md
lg
xl
<button class="bg-blue-500 sm:bg-green-500 md:bg-indigo-500 lg:bg-red-500 xl:bg-black ...">Button</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.
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>
To control the background color of an element on focus, add the 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.
Customizing
By default Tailwind makes the entire 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'),
+ 'secondary': '#ffed4a',
+ 'danger': '#e3342f',
}
}
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'],
}