Which is the HTML Tag for Background Color?

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

The color combination of various elements on a webpage is really important. Companies keep it styled in a way to attract customers to their websites and keeps them hooked to it. And this is where the background color of elements came into the picture. Html provides us with a number of ways to modify the background color of any element of a webpage.

Introduction

The attribute that is used to set background color of an HTML element is bg color. Depending on the element whose background color is to be set, we use the appropriate tag. The bgcolor attribute can be used with the following tags- body, table, td, th, tr, marquee.

How to Set Background Color in HTML?

Let us see how to set the background color in HTML using the bgcolor attribute and other methods. The syntax for setting the background color using the bgcolor attribute is as follows:

Here value can be the color name, RGB number, or hex number of the color.

Let us see this with the help of an example:

Output:

set background color in HTML

The bgcolor property discussed above was used earlier but has been omitted from HTML 5 onwards. We now utilize the style attribute to set the background color of a page. In this section, we'll go through the style attribute in detail.

The style attribute used with HTML tags specifies inline styles for an element. This attribute has a background-color property, which can be used to set the background color of the HTML element. If the style attribute is used, it will override all other global style sets. We can use an inline style attribute or use internal CSS for the same.

Inline CSS allows us to style specific HTML elements. Let us see this with the help of the following code:

Output:

style property using inline css

Internal CSS is used to include CSS inside the head section of the document.

Let us see this with the help of the following code:

Output: style attribute using internal css

Let us now explore the various ways in which style attributes can be used to set the background color of the web page.

  1. By specifying the desired color - In this method, we mention the color that we want our background to be. Eg - red, blue, etc..

Syntax

  1. By using hex color codes - The background color can be specified through hexadecimal color codes.

Syntax

  1. By using RGB color values - The background color can be specified through RGB color values. These values refer to the amount of red, green and blue in color. Each RGB value is a number between 0 and 255.

Syntax

  1. By using HSL color values - HSL stands for hue, saturation, and lightness. The background color can be specified through HSL color values. Hue refers to the degree of intensity on the color wheel, where 0 is red, 240 is blue, and 120 is green. The saturation level is a percentage between 0 to 100 where 0% means grey and 100% means full color. Lightness is also a percentage between 0 to 100, defining the intensity of the color, where 0% is black and, 100% is white, 50% is medium.

Syntax

  1. By creating gradient background - We can set the background color of a webpage by creating a gradient background. It can be achieved by creating a background color gradient by mentioning the direction of the gradient along with the colors. Gradients can be linear, radial, or conic.

Let us see this with the help of the following example:

Output: background linear gradient

In the above example, we define a linear gradient that starts from the left with the color red and transits to yellow. If more control over the direction of the gradient is desired, angles can be used instead of predefined directions.

Examples

We'll now see various examples of code snippets demonstrating how to use the background-color property of the style attribute with different HTML elements. We shall build a short and sweet webpage step by step that contains a heading and some text. We shall use imported fonts to make the texts eye-appealing.

Example 1: Setting the Background Color of Body

Output:

background color property of the style attribute

We have created a heading and applied our imported font. We have set the background color of the body as pink by using the style attribute inside the body tag.

Example 2: Setting the Background Color of div Elements

Output:

Setting the background color of div elements

In this example, the style attribute has been used with the div tag, too, along with the body tag. The background color that we want the div element to be has been mentioned in the appropriate style attribute. We have set the background color of the second div element as light green and added some text to it.

Browser Support

Browser Support for

BROWSERYES/NO
ChromeYes
Internet ExplorerYes
FirefoxYes
SafariYes
OperaYes

Browser Support for background-color

BROWSERYES/NO
ChromeYes
Internet ExplorerYes
FirefoxYes
SafariYes
OperaYes
EdgeYes

Learn more

  1. HTML tags
  2. Body tag in HTML
  3. Headings and Paragraphs in HTML
  4. Inline and Block Elements

Conclusion

  • bgcolor attribute was used to set the background color of a page till HTML 5.
  • Now, we use the style attribute for changing the background color.
  • In style attribute, we have background-color property, which is used to set the background color of tags.
  • Background color can be set by specifying the color, using RGB codes, hex color codes, HSL values, or by creating a color gradient.