Home HTML Attributes bgcolor HTML bgcolor Attribute Definition
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.
Syntax
HTML Syntax <element bgcolor=" color ">
Historical form only. New markup should omit bgcolor and use CSS.
Values
Attribute Values What bgcolor accepted, and the modern CSS equivalent:
Value Description Try it Named color Keywords such as red, silver, navy Try it#RRGGBBSix hex digits with a leading # (e.g. #FFEECC) Try itNot rgb() / hsl() Those CSS functions are invalid for the attribute — use CSS instead Try itCSS background-color Modern replacement for any element, with full color syntax Try itOn <body> Legacy page background — replace with body { background-color: … } Try it
Try it
Live Preview Change the color applied via the deprecated bgcolor attribute (compatibility demo). Prefer CSS in real projects.
Code
HTML Examples Example 1 is historical. Examples 2–3 show the CSS approach you should ship.
1. Legacy bgcolor 2. CSS replacement 3. Body background Example 1 — Legacy Table bgcolor (Do Not Ship) How older pages tinted whole tables or cells.
<table bgcolor="#f0f0f0">
<tr>
<th>Name</th>
<th>Score</th>
</tr>
<tr>
<td bgcolor="#d6ecd4">Ada</td>
<td>12</td>
</tr>
</table> Browsers may still show these colors — prefer CSS in new work.
How It Works bgcolor on <table> paints the table background.A cell can override with its own bgcolor.Only HTML colors — keywords or #RRGGBB.Deprecated. Keep for reading old HTML, not for new designs.Example 2 — Modern CSS background-color Same look without the deprecated attribute.
<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> How It Works Move color into CSS with background-color.Use classes so many cells share one rule.Full CSS colors are allowed (rgb(), hsl(), and more).HTML stays semantic — presentation lives in stylesheets.Example 3 — Page Background (Body) Replace <body bgcolor="…"> with a CSS rule on body.
<!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> Open Try It Yourself to see a full document with body { background-color: … }.
How It Works Old way: <body bgcolor="#f5f7fb">.New way: style the body element with background-color.Same idea for tables/cells — target the element in CSS, not with presentational attributes.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.
Compatibility
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.
Remember
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 What does the HTML bgcolor attribute do? 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.
Is bgcolor deprecated? Yes. MDN marks bgcolor as deprecated. Use the CSS background-color property instead of the HTML attribute.
Which elements used bgcolor? Historically body, table, tr, td, th, and a few other presentational elements. Do not put bgcolor on a generic div — use CSS there.
Can bgcolor use rgb() or hsl()? 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.
Will old bgcolor pages still work? Many browsers still paint the color for compatibility, but new pages should use CSS so styling stays consistent and maintainable.
How do I replace bgcolor with CSS? 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.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion