How to change background color of heading using CSS?

Answer

To change background color of heading using CSS, you can set the background-color style property for the heading 2 elements with the values like #FF5588, Yellow, rgb(80, 165, 201), etc.

CSS Code

The following is the typical CSS code to change background color of heading in HTML page using tag name h2.

Using hex color

h2 {
   background-color: #FF5588;
}

Using color name

h2 {
   background-color: Yellow;
}

Using rgb() function to specify color

h2 {
   background-color: rgb(80, 165, 201);
}

Examples

We shall go through some working examples on how to change background color of heading using CSS. In the following examples, you may change the value of background-color and Run the code to visually observe the changes made to the Heading 2 elements.

Example 1

This is an example on how to change background color of heading using hex color.

You may edit the following code, and click on Run button.

Example 2

This is an example on how to change background color of heading using color name.

You may edit the following code, and click on Run button.

Example 3

This is an example on how to change background color of heading using rgb() function to specify color.

You may edit the following code, and click on Run button.