HTML bgcolor Attribute
The obsolete HTML bgcolor attribute was historically used to specify the background color of a webpage, table, table row, table cell, or certain other elements.
bgcolor attribute is obsolete and should not be used in modern HTML. Use the CSS background-color property instead.
Historical Syntax
The bgcolor attribute was added directly to an HTML element and assigned a color value:
<body bgcolor="#e6f4ff">
Color values could be specified using formats recognized by browsers at the time, including named colors and hexadecimal color values.
Historical Elements
The bgcolor attribute was historically used with several elements to apply background colors directly through HTML.
| Element | Historical Purpose |
|---|---|
<body> |
Specified the background color of the webpage. |
<table> |
Specified the background color of a table. |
<thead>, <tbody>, <tfoot> |
Specified the background color of a group of table rows. |
<tr> |
Specified the background color of a table row. |
<td>, <th> |
Specified the background color of an individual table cell. |
Historical Use
The bgcolor attribute provided a simple way to add background colors before CSS became the standard method for styling webpages. It could be used to change the background of an entire document or to visually distinguish individual areas of a table.
Because each color was written directly into the HTML markup, changing the appearance of a large website could require editing the attribute on many individual elements and pages.
Why It Became Obsolete
The bgcolor attribute mixed visual presentation with the structure and content of the HTML document. CSS separates presentation from HTML and provides much greater control over how background colors are applied.
CSS rules can also be reused across many elements and pages, making colors easier to maintain and allowing styles to change without modifying the underlying HTML structure.
Modern Alternative
Use the CSS background-color property to set the background color of an element.
Historical HTML
<table bgcolor="#e6f4ff">
<tr>
<td>Table content</td>
</tr>
</table>
Modern HTML and CSS
<table class="colored-table">
<tr>
<td>Table content</td>
</tr>
</table>
<style>
.colored-table {
background-color: #e6f4ff;
}
</style>
The background-color property can be applied to the page itself or to individual elements such as tables, rows, cells, sections, and other containers.
Learn More About CSS Background Colors
The CSS tutorials provide more information about applying colors and backgrounds with CSS:
- CSS Background Color - Learn how to set the background color of an element.
- CSS Backgrounds - Learn how CSS controls element backgrounds.
- CSS Color Values - Learn about the color formats that can be used in CSS.
Legacy Browser Support
The obsolete bgcolor attribute was widely recognized by older browsers on the <body> element and several table-related elements. Some modern browsers may continue to recognize historical uses for compatibility with older webpages.
Browser recognition of the bgcolor attribute does not make it valid modern HTML. When maintaining older source code, replace the attribute with the CSS background-color property.
For additional information about historical browser compatibility, visit Can I Use? . Search for the HTML element or attribute you want to check. Search results can include related HTML features, element attributes, APIs, and other technologies, so select the result that most closely matches the legacy feature you are researching.
Related Attributes
Other obsolete HTML attributes historically used to control colors and visual presentation include:
background- Historically used to specify a background image.text- Historically used to specify the document text color.link- Historically used to specify the color of unvisited links.alink- Historically used to specify the color of active links.vlink- Historically used to specify the color of visited links.
Specifications
The bgcolor attribute on elements where it was historically used for presentation is obsolete in modern HTML. Background colors should be defined with CSS. See the WHATWG HTML Living Standard for information about obsolete and non-conforming features.
