HTML bgcolor Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The bgcolor attribute set a background color on legacy elements such as <body>, <table>, <tr>, <th>, and <td>. The value is an HTML color: a named color or a 6-digit hex code with #.

Do not use it in new pages. MDN recommends the CSS background-color property instead (via a stylesheet or the style attribute when needed).

Deprecated.

Presentational HTML. Kept for reading old markup and email-like quirks — not for modern site styling. Prefer CSS.

Still painted in many browsers.

Engines often still honor bgcolor for compatibility, which is why old pages look colored — but that is not a reason to write new code with it.

HTML Syntax

syntax.html
<element bgcolor=" color">

Historical form only. New markup should omit bgcolor and use CSS.

Attribute Values

What bgcolor accepted, and the modern CSS equivalent:

ValueDescriptionTry it
Named colorKeywords such as red, silver, navyTry it
#RRGGBBSix hex digits with a leading # (e.g. #FFEECC)Try it
Not rgb() / hsl()Those CSS functions are invalid for the attribute — use CSS insteadTry it
CSS background-colorModern replacement for any element, with full color syntaxTry it
On <body>Legacy page background — replace with body { background-color: … }Try it

Live Preview

Change the color applied via the deprecated bgcolor attribute (compatibility demo). Prefer CSS in real projects.

Legacy attribute on a table cell for study only.

Table cell
Sample cellNeighbor cell

bgcolor="#e8f4ff"

HTML Examples

Example 1 is historical. Examples 2–3 show the CSS approach you should ship.

Example 1 — Legacy Table bgcolor (Do Not Ship)

How older pages tinted whole tables or cells.

HTML
<table bgcolor="#f0f0f0">
  <tr>
    <th>Name</th>
    <th>Score</th>
  </tr>
  <tr>
    <td bgcolor="#d6ecd4">Ada</td>
    <td>12</td>
  </tr>
</table>
Try It Yourself

How It Works

  1. bgcolor on <table> paints the table background.
  2. A cell can override with its own bgcolor.
  3. Only HTML colors — keywords or #RRGGBB.
  4. Deprecated. Keep for reading old HTML, not for new designs.

Example 2 — Modern CSS background-color

Same look without the deprecated attribute.

HTML
<style>
  table.scores { background-color: #f0f0f0; }
  table.scores td.highlight { background-color: #d6ecd4; }
</style>

<table class="scores">
  <tr>
    <th>Name</th>
    <th>Score</th>
  </tr>
  <tr>
    <td class="highlight">Ada</td>
    <td>12</td>
  </tr>
</table>
Try It Yourself

How It Works

  1. Move color into CSS with background-color.
  2. Use classes so many cells share one rule.
  3. Full CSS colors are allowed (rgb(), hsl(), and more).
  4. HTML stays semantic — presentation lives in stylesheets.

Example 3 — Page Background (Body)

Replace <body bgcolor="…"> with a CSS rule on body.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Page background</title>
  <style>
    body {
      background-color: #f5f7fb;
      margin: 1rem;
      font-family: system-ui, sans-serif;
    }
  </style>
</head>
<body>
  <p>Page background set with CSS, not bgcolor.</p>
</body>
</html>
Try It Yourself

How It Works

  1. Old way: <body bgcolor="#f5f7fb">.
  2. New way: style the body element with background-color.
  3. Same idea for tables/cells — target the element in CSS, not with presentational attributes.
  4. Related legacy: background (image URL) is also obsolete; use background-image.

Edge Cases & Pitfalls

Most mistakes mix CSS color syntax into the old attribute.

deprecated

Not for new HTML

Validators and style guides reject presentational attributes. Move colors to CSS when you touch the page.

values

No rgb() in bgcolor

Only keywords and #RRGGBB. Put advanced colors in background-color.

scope

Not a generic div attribute

bgcolor was defined for specific elements (body/table family). Style any element with CSS instead.

cascade

CSS can override legacy colors

A stylesheet background-color may win over an old bgcolor, depending on specificity — another reason to pick one approach (CSS).

a11y

Check contrast

Whether you use legacy attributes or CSS, text must remain readable against the background.

Browser Support

bgcolor is a deprecated HTML attribute. Many browsers still render it for compatibility, but authors should use CSS.

✓ Deprecated · Legacy only

bgcolor

Still painted in Chromium, Firefox, Safari, and Edge for old pages. Not recommended for new work — use background-color.

0% Recommended for new work
Google Chrome Still paints · legacy
Legacy only
Mozilla Firefox Still paints · legacy
Legacy only
Apple Safari Still paints · legacy
Legacy only
Microsoft Edge Still paints · legacy
Legacy only
Opera Still paints · legacy
Legacy only
Internet Explorer Legacy support
Legacy only
bgcolor Deprecated — use CSS

Bottom line: Expect old pages to keep their colors. Write new backgrounds with CSS background-color.

Key Takeaways

  • Was: HTML background color on body and table elements.
  • Now: deprecated — use background-color in CSS.
  • Values: named colors or #RRGGBB only on the attribute.
  • Compatibility: browsers may still render it — do not rely on that for new code.

One line: learn bgcolor to read old pages — ship background-color instead.

Frequently Asked Questions

It sets a background color on supported legacy elements such as body, table, tr, th, and td. The value is a named color or a #RRGGBB hex code.
Yes. MDN marks bgcolor as deprecated. Use the CSS background-color property instead of the HTML attribute.
Historically body, table, tr, td, th, and a few other presentational elements. Do not put bgcolor on a generic div — use CSS there.
No. The attribute only accepts HTML colors: keywords like red or a 6-digit hex value with a #. Full CSS color syntax belongs in CSS.
Many browsers still paint the color for compatibility, but new pages should use CSS so styling stays consistent and maintainable.
Use background-color in a stylesheet, a style element, or (sparingly) a style attribute — for example td { background-color: #eef; }.

Next: border

Another presentational HTML attribute — CSS is the modern path.

border tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful