What is bgcolor in HTML?
In HTML, the bgcolor attribute sets the background color of a HTML element. The bgcolor attribute can be used with the HTML tags like <body>, <table>, <marquee>, <td>, <tr>, <th>.
Pencil: Deprecated warning: HTML5 does not support the bgcolor attribute. It is recommended to use CSS instead.
The syntax for the CSS is
The syntax for bgcolor
- tag - you can use any HTML tag like <body> , <p>, <button>, etc.
- rgb/hex/color_name - These are attribute values, there are different ways to use colors but can use only one at a time. The attribute values are specified below :
Attribute Values
Attribute value | Description |
---|---|
rgb_number | A rgb code determines the background color (like "rgb(255,225,0)") |
hex_number | Hex code specifies the color of the background (like "#ff0ff0") |
color_name | This color specifies the background color (like "blue") |
Examples
Example 1: set the bg color of the body to blue.
In the below example, we are using the bgcolor attribute with the <body> tag, and we are setting the value of the attribute to bgcolor="blue". Hence, we can see that the background color appears as blue in the output image below.
HTML Code
Browser Output
Example 2: HTML bgcolor Attribute by specifying the text as blue & background as pink color.
In below example, we are using the bgcolor attribute with the <body> tag, and we set the value of the attribute to pink along with that we set the text color inside the body to blue. Hence, we can see that the background color appears pink and the text is blue in the output image.
HTML Code
Browser Output
Example 3: Using bgcolor with table.
In the below example we see the bgcolor for the table as yellow inside the table tag along with that we are using the bgcolor attribute with the tag, and we set the value of the attribute to lightblue.
HTML Code
Browser Output
Supported Browsers
The bgcolor attribute in HTML is supported by the following browsers :
Attribute | Chrome | Microsoft Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
bgcolor | Yes | Yes | Yes | Yes | Yes |
Conclusion
Given below are the takeaways from this article :
- The bgcolor element is used to set the background color of an HTML element.
- The HTML5 does not support the bgcolor attribute.
- The bgcolor attribute can be used with the HTML tags like <body>, <table>, <marquee>, <td>, <tr>, <th>.
- The bgcolor almost supports all the main browsers like Chrome, Edge, Firefox, Safari and Opera.