Tailwind CSS on GitHub

Background Color

Utilities for controlling an element's background color.

Default class reference

Class
Preview 
bg-transparent
bg-current
bg-black
bg-white
bg-gray-50
bg-gray-100
bg-gray-200
bg-gray-300
bg-gray-400
bg-gray-500
bg-gray-600
bg-gray-700
bg-gray-800
bg-gray-900
bg-red-50
bg-red-100
bg-red-200
bg-red-300
bg-red-400
bg-red-500
bg-red-600
bg-red-700
bg-red-800
bg-red-900
bg-yellow-50
bg-yellow-100
bg-yellow-200
bg-yellow-300
bg-yellow-400
bg-yellow-500
bg-yellow-600
bg-yellow-700
bg-yellow-800
bg-yellow-900
bg-green-50
bg-green-100
bg-green-200
bg-green-300
bg-green-400
bg-green-500
bg-green-600
bg-green-700
bg-green-800
bg-green-900
bg-blue-50
bg-blue-100
bg-blue-200
bg-blue-300
bg-blue-400
bg-blue-500
bg-blue-600
bg-blue-700
bg-blue-800
bg-blue-900
bg-indigo-50
bg-indigo-100
bg-indigo-200
bg-indigo-300
bg-indigo-400
bg-indigo-500
bg-indigo-600
bg-indigo-700
bg-indigo-800
bg-indigo-900
bg-purple-50
bg-purple-100
bg-purple-200
bg-purple-300
bg-purple-400
bg-purple-500
bg-purple-600
bg-purple-700
bg-purple-800
bg-purple-900
bg-pink-50
bg-pink-100
bg-pink-200
bg-pink-300
bg-pink-400
bg-pink-500
bg-pink-600
bg-pink-700
bg-pink-800
bg-pink-900

Usage

Control the background color of an element using the bg-{color} utilities.

<button class="bg-green-500 ...">Button</button>

Changing opacity

Control the opacity of an element's background color using the bg-opacity-{amount} utilities.

100%
75%
50%
25%
0%
<div class="bg-purple-600 bg-opacity-100 ..."></div>
<div class="bg-purple-600 bg-opacity-75 ..."></div>
<div class="bg-purple-600 bg-opacity-50 ..."></div>
<div class="bg-purple-600 bg-opacity-25 ..."></div>
<div class="bg-purple-600 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.

<button class="bg-blue-500 md:bg-green-500 ...">Button</button>

For more information about Tailwind's responsive design features, check out the Responsive Design documentation.

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-red-700 to apply the bg-red-700 utility on hover.

<button class="bg-red-500 hover:bg-red-700 ...">Button</button>

Hover utilities can also be combined with responsive utilities by adding the responsive {screen}: prefix before the hover: 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',
      })
    }
  }

Variants

By default, only responsive, dark mode (if enabled), group-hover, focus-within, 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 variants:

  // tailwind.config.js
  module.exports = {
    variants: {
      extend: {
        // ...
+       backgroundColor: ['active'],
      }
    }
  }

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