Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Tailwind CSS Background Color

Tailwind CSS Background Color

In Tailwind CSS,Background Color(Background Color) is one of the most commonly used utility classes, which allows you to quickly set the background color of an element.

Tailwind provides a variety of color options and different shades, ensuring you can flexibly apply the styles you need.

Basic Background Color Syntax:

Common Background Color Classes:

Class Names Description Example
bg-{color} Set background color bg-red-500,bg-blue-200
bg-transparent Set a transparent background color bg-transparent
bg-current Set the background color to the current text color (often used for icon backgrounds) bg-current
bg-white Set the background color to white bg-white
bg-black Set the background color to black bg-black
bg-gray-{n} Set the background color to gray,{n} is the shade of gray (from 50 to 900) bg-gray-100, bg-gray-700

Background Color Gradients (Gradient):

Tailwind CSS supports linear gradients, radial gradients, and more for backgrounds. You can use bg-gradient-to-{direction} to specify the gradient direction, and use from-{color}、to-{color} to set the starting and ending colors of the gradient.

Class Names Description Example
bg-gradient-to-{direction} Set the direction of the gradient background,direction can be t、r、b、l、tr、tl、br、bletc. bg-gradient-to-r (left-to-right gradient)
from-{color} Set the starting color of the gradient background from-blue-500
to-{color} Set the ending color of the gradient background to-green-300
via-{color} Set the middle color of the gradient (optional) via-purple-400

Example

  Gradient background from blue to green

Background Color Opacity(Opacity):

Tailwind also provides utility classes to control background opacity, use bg-opacity-{value} to set the opacity value,{value} an integer from 0 to 100 representing the opacity percentage.

Class Names Description Example
bg-opacity-{n} Set the opacity of the background color bg-opacity-50
bg-opacity-0 Set the background color to fully transparent bg-opacity-0
bg-opacity-100 Set the background color to fully opaque bg-opacity-100

Example

  Semi-transparent blue background

Using Custom Background Colors in Tailwind Configuration:

You can tailwind.config.js Customize background colors in the configuration file. By defining colors in the theme.extend section, under the colors property, you can extend the default color set.

Example

module.

exports

=

{

  theme

:

{

    extend

:

{

      colors

:

{

       

'custom-blue'

:

'#1D4ED8'

,

       

'custom-gray'

:

'#4B5563'

,

     

}

   

}

 

}

}

Using Custom Background Colors in HTML:

Example

  Use a custom blue background

Comprehensive example

Example

   

Blue Background

   

This is a container with a blue background and white text color.

   

Gradient Background

   

This is a linear gradient background from purple to pink to red.

   

Opacity Background

   

This is a container with a semi-transparent green background.

Try it »

Analysis:

Basic Background Colors:

Gradient Background:

Background Opacity: