Background Color

Utilities for controlling an element’s background color.

Class reference

Class
Preview 
.bg-transparent
.bg-current
.bg-black
.bg-white
.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-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-orange-100
.bg-orange-200
.bg-orange-300
.bg-orange-400
.bg-orange-500
.bg-orange-600
.bg-orange-700
.bg-orange-800
.bg-orange-900
.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-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-teal-100
.bg-teal-200
.bg-teal-300
.bg-teal-400
.bg-teal-500
.bg-teal-600
.bg-teal-700
.bg-teal-800
.bg-teal-900
.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-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-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-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.

Background Color - 图1

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

Background Color - 图2

  1. <div class="bg-blue-500 bg-opacity-100"></div>
  2. <div class="bg-blue-500 bg-opacity-75"></div>
  3. <div class="bg-blue-500 bg-opacity-50"></div>
  4. <div class="bg-blue-500 bg-opacity-25"></div>
  5. <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.

Background Color - 图3

all

Background Color - 图4

sm

Background Color - 图5

md

Background Color - 图6

lg

Background Color - 图7

xl

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

Background Color - 图8

  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 - 图9

  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 => ({
  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. }

← Background Clip   Background Opacity →