👀 Live Preview
Table with mixed cell background colors:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |

The bgcolor attribute in HTML is used to define the background color of an element. It allows developers to set a specific color for an element’s background, adding visual appeal and enhancing the overall design of a web page.
red, blue, green.
#FF0000, #00FF00.
td, th, table.
background-color.
Dynamic colors.
Use CSS instead.
bgcolorThe primary purpose of the bgcolor attribute is to customize the background color of various HTML elements, such as tables, table cells, and table headers. By using this attribute, you can tailor the appearance of your web page to match your design preferences.
The bgcolor attribute is not recommended for new projects. Use CSS background-color instead for better control and maintainability.
Add bgcolor to a supported element with a color name or hex code:
<td bgcolor="lightblue">Cell content</td>
<table bgcolor="#EFEFEF">...</table>red) or hex code (e.g. #FF0000).table, tr, td, th, and body.<div>.rgb() colors, use CSS background-color instead.The bgcolor attribute accepts values that represent different colors. These values can be specified using color names, hexadecimal codes, or RGB values. Here’s a quick overview:
red, blue, or green to set the background color.#FF0000 for red or #00FF00 for green.rgb(255, 0, 0) are best applied with CSS background-color, not the legacy bgcolor attribute.<td bgcolor="red">Named color</td>
<td bgcolor="#00FF00">Hex color</td>
<td style="background-color: rgb(255, 0, 0);">RGB via CSS</td>| Format | Example | CSS Alternative |
|---|---|---|
| Color name | bgcolor="lightblue" | background-color: lightblue |
| Hex code | bgcolor="#EFEFEF" | background-color: #EFEFEF |
| RGB | Use CSS instead | background-color: rgb(255, 0, 0) |
| On table | <table bgcolor> | Style the table element |
| On cell | <td bgcolor> | Most common legacy use |
| HTML5 status | Deprecated | Use CSS for new code |
| Element | Supported? | Notes |
|---|---|---|
<table> | Yes (legacy) | Entire table background |
<td>, <th> | Yes (legacy) | Individual cell colors |
<tr> | Yes (legacy) | Row background |
<body> | Yes (legacy) | Page background in HTML4 |
<div> | No | Use CSS background-color |
Color names, hex codes, RGB via CSS, table styling, and dynamic JavaScript bgcolor.
Table with mixed cell background colors:
| Row 1, Cell 1 | Row 1, Cell 2 |
| Row 2, Cell 1 | Row 2, Cell 2 |
You can use predefined color names such as red, blue, or green to set the background color:
<td bgcolor="red">This cell has a red background.</td>Specify colors using hexadecimal codes like #FF0000 for red or #00FF00 for green:
<td bgcolor="#00FF00">This cell has a green background.</td>Use RGB values like rgb(255, 0, 0) for red or rgb(0, 255, 0) for green via CSS:
<td style="background-color: rgb(255, 0, 0);">This cell has a red background using RGB values.</td>Let’s see an example of how to use the bgcolor attribute in an HTML table:
<table bgcolor="#EFEFEF">
<tr>
<td bgcolor="lightblue">Row 1, Cell 1</td>
<td bgcolor="lightgreen">Row 1, Cell 2</td>
</tr>
<tr>
<td bgcolor="lightcoral">Row 2, Cell 1</td>
<td bgcolor="lightyellow">Row 2, Cell 2</td>
</tr>
</table>In this example, the bgcolor attribute is used to set the background color for the entire table (#EFEFEF) and individual table cells.
Similar to other HTML attributes, you can dynamically set the bgcolor attribute using JavaScript. This becomes useful when you want to change the background color based on user interactions or specific conditions. Here’s a brief example:
<table id="dynamicElement">...</table>
<script>
// Dynamically set bgcolor for an element
document.getElementById("dynamicElement").setAttribute("bgcolor", "#FFD700");
// Modern alternative:
// document.getElementById("dynamicElement").style.backgroundColor = "#FFD700";
</script>In this script, the background color of an element with the id dynamicElement is set to a gold color (#FFD700). This dynamic approach allows you to adapt the background color based on runtime conditions. For new code, prefer style.backgroundColor.
Add a color name or hex value to a supported element.
The user agent resolves the color value to a display color.
The element’s background area fills with the chosen color.
In modern projects, CSS background-color achieves the same result with more flexibility.
The bgcolor attribute is deprecated in HTML5 but still rendered by browsers on legacy elements like table and td.
Use CSS background-color for all new styling.
Bottom line: Browsers still paint bgcolor on tables, but CSS is the standard for new code.
background-color for all new projectsbgcolor on <div> elementsbgcolor attribute selectively to enhance the visual appeal of your web page when maintaining legacy HTML.The bgcolor attribute is a versatile tool for customizing the background color of HTML elements, especially in legacy table markup. By leveraging this attribute, you can create visually appealing and well-designed web pages that align with your aesthetic preferences.
For modern development, reach for CSS background-color instead. It works on any element, supports rgb(), hsl(), and gradients, and keeps styling separate from structure.
bgcolorBookmark these before coloring your HTML elements.
red, #FF0000.
Valuestd, th, table.
Scopebackground-color.
ModernsetAttribute.
ScriptReadability.
A11ybackground-color is the standard.background-color on div and other generic elements.background-color.element.style.backgroundColor = "#FFD700" for modern code, or setAttribute("bgcolor", "#FFD700") on legacy table elements.background-color in a stylesheet or inline style attribute.Practice the legacy bgcolor attribute and compare it with CSS background-color in the Try It editor.
5 people found this page helpful