Background Color

Utilities for controlling an element’s background color.

Class reference

Background Color - 图1 Background Color - 图2 Background Color - 图3 Background Color - 图4

Usage

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

Background Color - 图5

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

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.

  1. <button class="bg-blue-500 sm:bg-green-500 md:bg-indigo-500 lg:bg-red-500 xl:bg-black ...">Button</button>

Background Color - 图6

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.

Background Color - 图7

  1. <button class="bg-blue-500 hover:bg-blue-700 ...">
  2. Hover me
  3. </button>

Hover utilities can also be combined with responsive utilities by adding the responsive {screen}: prefix before the focus: prefix.

  1. <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.

Background Color - 图8

  1. <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.

  1. <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.

  1. // tailwind.config.js
  2. module.exports = {
  3. theme: {
  4. backgroundColor: theme =&gt; ({
  5. - ...theme('colors'),
  6. + 'primary': '#3490dc',
  7. + 'secondary': '#ffed4a',
  8. + 'danger': '#e3342f',
  9. })
  10. }
  11. }

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. variants: {
  4. // ...
  5. - backgroundColor: ['responsive', 'hover', 'focus'],
  6. + backgroundColor: ['responsive', 'hover', 'focus', 'active', 'group-hover'],
  7. }
  8. }

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:

  1. // tailwind.config.js
  2. module.exports = {
  3. corePlugins: {
  4. // ...
  5. + backgroundColor: false,
  6. }
  7. }