HTML bgcolor Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 5 Examples
Layout & Styling

Introduction

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.

What You’ll Learn

01

Color Names

red, blue, green.

02

Hex Codes

#FF0000, #00FF00.

03

Table Cells

td, th, table.

04

CSS Alternative

background-color.

05

JavaScript

Dynamic colors.

06

Deprecated

Use CSS instead.

Purpose of bgcolor

The 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.

⚠️
Deprecated in HTML5

The bgcolor attribute is not recommended for new projects. Use CSS background-color instead for better control and maintainability.

📝 Syntax

Add bgcolor to a supported element with a color name or hex code:

bgcolor.html
<td bgcolor="lightblue">Cell content</td>
<table bgcolor="#EFEFEF">...</table>

Syntax Rules

  • Value is a color name (e.g. red) or hex code (e.g. #FF0000).
  • Valid on table, tr, td, th, and body.
  • Not valid on generic elements like <div>.
  • For rgb() colors, use CSS background-color instead.

💎 Values

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:

  • Color Names — You can use predefined color names such as red, blue, or green to set the background color.
  • Hexadecimal Codes — Specify colors using hexadecimal codes like #FF0000 for red or #00FF00 for green.
  • RGB Values — Values like rgb(255, 0, 0) are best applied with CSS background-color, not the legacy bgcolor attribute.
bgcolor-values.html
<td bgcolor="red">Named color</td>
<td bgcolor="#00FF00">Hex color</td>
<td style="background-color: rgb(255, 0, 0);">RGB via CSS</td>

⚡ Quick Reference

FormatExampleCSS Alternative
Color namebgcolor="lightblue"background-color: lightblue
Hex codebgcolor="#EFEFEF"background-color: #EFEFEF
RGBUse CSS insteadbackground-color: rgb(255, 0, 0)
On table<table bgcolor>Style the table element
On cell<td bgcolor>Most common legacy use
HTML5 statusDeprecatedUse CSS for new code

Applicable Elements

ElementSupported?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>NoUse CSS background-color

Examples Gallery

Color names, hex codes, RGB via CSS, table styling, and dynamic JavaScript bgcolor.

👀 Live Preview

Table with mixed cell background colors:

Row 1, Cell 1Row 1, Cell 2
Row 2, Cell 1Row 2, Cell 2

Color Names

You can use predefined color names such as red, blue, or green to set the background color:

color-names.html
<td bgcolor="red">This cell has a red background.</td>
Try It Yourself

Hexadecimal Codes

Specify colors using hexadecimal codes like #FF0000 for red or #00FF00 for green:

hexadecimal-codes.html
<td bgcolor="#00FF00">This cell has a green background.</td>
Try It Yourself

RGB Values

Use RGB values like rgb(255, 0, 0) for red or rgb(0, 255, 0) for green via CSS:

rgb-values.html
<td style="background-color: rgb(255, 0, 0);">This cell has a red background using RGB values.</td>
Try It Yourself

Example

Let’s see an example of how to use the bgcolor attribute in an HTML table:

bgcolor.html
<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>
Try It Yourself

How It Works

In this example, the bgcolor attribute is used to set the background color for the entire table (#EFEFEF) and individual table cells.

Dynamic Values with JavaScript

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:

bgcolor.html
<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>
Try It Yourself

How It Works

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.

♿ Accessibility

  • Contrast matters — Ensure sufficient contrast between background color and text color for readability.
  • Do not rely on color alone — Pair color cues with text labels or icons for color-blind users.
  • Avoid low-contrast pastels — Light yellow backgrounds with white text fail WCAG contrast guidelines.
  • Use CSS for theming — CSS classes make it easier to support high-contrast and dark modes.
  • Test with screen readers — Background color alone does not convey meaning to assistive tech.

🧠 How bgcolor Works

1

Author sets bgcolor

Add a color name or hex value to a supported element.

Markup
2

Browser parses color

The user agent resolves the color value to a display color.

Parse
3

Background is painted

The element’s background area fills with the chosen color.

Render
=

Colored background displayed

In modern projects, CSS background-color achieves the same result with more flexibility.

Browser Support

The bgcolor attribute is deprecated in HTML5 but still rendered by browsers on legacy elements like table and td.

⚠️ HTML4 legacy · Deprecated

Still renders in browsers

Use CSS background-color for all new styling.

Legacy Rendering support
Google Chrome Legacy rendering
Deprecated
Mozilla Firefox Legacy rendering
Deprecated
Apple Safari Legacy rendering
Deprecated
Microsoft Edge Legacy rendering
Deprecated
bgcolor attribute Deprecated

Bottom line: Browsers still paint bgcolor on tables, but CSS is the standard for new code.

💡 Best Practices

✅ Do

  • Use CSS background-color for all new projects
  • Ensure strong contrast between text and background
  • Keep table styling consistent across rows and columns
  • Use hex codes for precise brand colors
  • Test colors in light and dark environments

❌ Don’t

  • Use bgcolor on <div> elements
  • Rely on bgcolor for layout or spacing
  • Use clashing colors that reduce readability
  • Mix bgcolor and CSS without a clear pattern
  • Depend on background color alone to convey meaning
  • Use the bgcolor attribute selectively to enhance the visual appeal of your web page when maintaining legacy HTML.
  • Ensure sufficient contrast between the background color and the text color for better readability.
  • Experiment with different color values to find the combination that aligns with your design preferences.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about bgcolor

Bookmark these before coloring your HTML elements.

5
Core concepts
📊 02

Table Cells

td, th, table.

Scope
💻 03

CSS Wins

background-color.

Modern
⚙️ 04

Dynamic JS

setAttribute.

Script
05

Contrast

Readability.

A11y

❓ Frequently Asked Questions

It sets the background color of supported elements such as table cells, rows, tables, and body.
It is deprecated. Browsers still render it on legacy elements, but CSS background-color is the standard.
No. Use CSS background-color on div and other generic elements.
Not reliably. Use color names or hex in bgcolor, or apply rgb() via CSS background-color.
Use element.style.backgroundColor = "#FFD700" for modern code, or setAttribute("bgcolor", "#FFD700") on legacy table elements.
background-color in a stylesheet or inline style attribute.

Color HTML tables the modern way

Practice the legacy bgcolor attribute and compare it with CSS background-color in the Try It editor.

Try table example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful