JavaScript Document bgColor Property

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Deprecated
Instance property

What You’ll Learn

Document.bgColor is a deprecated instance property that gets or sets the document’s background color as a string. Learn how legacy pages used it, how it relates to sibling properties like fgColor and alinkColor, the MDN-recommended CSS replacement, and five examples with try-it labs.

01

Kind

Instance property

02

Type

string color

03

Status

Deprecated

04

Controls

Page background

05

Replace

CSS background

06

Legacy set

fg / link colors

Introduction

Before CSS became universal, developers sometimes colored entire pages with JavaScript or obsolete <body bgcolor="..."> attributes. The Document object exposed matching properties such as bgColor (background), fgColor (foreground text), and link colors.

MDN’s recommended alternative today is standard CSS background-color, reachable in the DOM through document.body.style.backgroundColor.

💡
Learn it, don’t ship it

Study bgColor to recognize legacy intranet pages and old tutorials. For new sites, put background colors in stylesheets or design tokens—not on deprecated Document properties.

Related Document tutorials: alinkColor, applets, Document constructor.

Understanding Document.bgColor

A getter/setter instance property on Document. Reading it returns the background color string; writing it changes the document background where the property is still honored.

  • Value — a color name (darkblue, white) or hex (#ff0000).
  • Scope — document-wide page background (legacy body coloring).
  • null — assigning null converts to "" (empty string).
  • Firefox default — MDN: white (#ffffff) in Mozilla Firefox.
  • Deprecated — MDN recommends CSS background-color instead.

📝 Syntax

JavaScript
document.bgColor

Get

JavaScript
console.log(document.bgColor);
// e.g. "#ffffff" in Firefox (browser-dependent)

Set (MDN example)

JavaScript
document.bgColor = "darkblue";
document.bgColor = "#f0f4f8";
document.bgColor = null; // becomes ""

⚡ Quick Reference

GoalCode / note
Read backgrounddocument.bgColor
Set name (MDN)document.bgColor = "darkblue"
Set hexdocument.bgColor = "#f0f4f8"
Clear (legacy)document.bgColor = null""
Modern replacementdocument.body.style.backgroundColor
MDN statusDeprecated

🔍 At a Glance

Four facts about document.bgColor.

Type
string

Color value

Targets
background

Document page

Status
deprecated

Legacy API

Use CSS
background-color

Modern fix

📋 bgColor vs body.style.backgroundColor

document.bgColorbody.style.backgroundColor
Recommended?NoYes (when JS is needed)
MDN statusDeprecatedStandard DOM CSSOM
Stylesheet friendlyNoPrefer CSS rules instead
Best forReading old codeRuntime theme toggles

Examples Gallery

Examples follow MDN Document: bgColor. Open try-it labs to see background changes where the property still applies.

📚 Getting Started

Set and read the deprecated document background color.

Example 1 — MDN: Set document.bgColor = "darkblue"

The classic MDN one-liner for a dark blue page background.

JavaScript
document.bgColor = "darkblue";
console.log(document.bgColor);
Try It Yourself

How It Works

Named colors are valid legacy inputs. Browsers may normalize the string when you read it back.

Example 2 — Read document.bgColor

Log the current background color (browser-dependent).

JavaScript
console.log(document.bgColor);
// Firefox default (MDN): "#ffffff"
// Other browsers may differ or ignore the property in practice
Try It Yourself

How It Works

If CSS already sets body { background-color }, that may override what you read from the legacy property.

📈 Hex, null & Modern CSS

Other assignment forms and the recommended replacement.

Example 3 — Set a Hex Background

Use a hexadecimal color for precise brand backgrounds.

JavaScript
document.bgColor = "#f0f4f8";
console.log(document.bgColor);
Try It Yourself

How It Works

Hex values work the same as color names on this legacy property.

Example 4 — null Becomes Empty String

MDN: assigning null is equivalent to assigning "".

JavaScript
document.bgColor = null;
console.log(document.bgColor === "");
console.log(JSON.stringify(document.bgColor));
Try It Yourself

How It Works

This null coercion is a Document property quirk shared with other legacy color properties.

Example 5 — Modern Replacement with CSS

MDN’s recommended approach—style backgrounds in CSS or via body.style.

JavaScript
body {
  background-color: #0f172a;
  color: #f8fafc;
}
JavaScript
// When JavaScript must set it at runtime:
document.body.style.backgroundColor = "#0f172a";
Try It Yourself

How It Works

CSS supports themes, media queries, and design systems without deprecated Document hooks.

🚀 Common Use Cases

  • Reading legacy intranet pages — recognize old background-color scripts.
  • Migrating old samples — replace with CSS background-color.
  • Debugging ancient tutorials — explain why a page flashes a new color on load.
  • Not for new apps — do not set page backgrounds via document.bgColor.
  • Teaching web history — contrast Document colors with CSS.
  • Theme toggles — use CSS variables and classes instead of legacy properties.

🧠 How Document Background Color Worked

1

Legacy script runs

document.bgColor = "darkblue" on page load.

Legacy
2

Browser paints background

Document-level color applied behind body content.

Render
3

CSS may override

Modern stylesheets often take precedence over legacy hooks.

Cascade
4

Prefer CSS background-color

Define page backgrounds in stylesheets or body.style for runtime changes.

📝 Notes

  • bgColor is deprecated in the HTML specification (MDN).
  • Assigning null becomes "" per MDN.
  • Firefox default is #ffffff (white) per MDN.
  • Sibling legacy properties: fgColor, linkColor, vlinkColor, alinkColor.
  • MDN replacement: CSS background-color via document.body.style.backgroundColor.
  • Related: alinkColor, applets, ownerDocument.

Legacy Browser Support

Document.bgColor is deprecated but may still work for compatibility in some browsers. MDN recommends CSS background-color instead. Logos use the shared browser-image-sprite.png sprite from this project.

Deprecated · Legacy

Document.bgColor

Legacy document background color getter/setter — use CSS background-color in new code.

Legacy Compatibility only
Google Chrome Compatibility support · prefer CSS
Legacy support
Mozilla Firefox Compatibility support · default #ffffff
Legacy support
Apple Safari Compatibility support
Legacy support
Microsoft Edge Chromium compatibility layer
Legacy support
Opera Follow Chromium behavior
Legacy support
Internet Explorer Historical legacy target
Legacy support
Document.bgColor Avoid in new code

Bottom line: Recognize document.bgColor in old scripts. For new page backgrounds, use CSS background-color on body or a layout wrapper — never document.bgColor.

Conclusion

Document.bgColor is a deprecated getter/setter for the page background color. It is useful for understanding legacy HTML-era scripts—not for building new features. Use CSS background-color instead.

Continue with body, ownerDocument, alinkColor, Document constructor, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use CSS background-color on body or layout wrappers
  • Use document.body.style.backgroundColor for runtime theme toggles
  • Replace document.bgColor when you touch legacy files
  • Use CSS variables for light/dark themes
  • Keep contrast accessible when choosing backgrounds

❌ Don’t

  • Set page backgrounds with document.bgColor in new projects
  • Mix legacy Document colors with modern CSS without testing cascade
  • Assume read values match what CSS currently displays
  • Rely on null coercion patterns in new code
  • Forget fgColor / link colors when migrating old pages

Key Takeaways

Knowledge Unlocked

Five things to remember about document.bgColor

Deprecated background color — prefer CSS background-color.

5
Core concepts
⚠️02

Status

deprecated

Legacy
🖼03

Targets

background

Page
🔢04

null

becomes ""

MDN
🛠05

Replace

background-color

CSS

❓ Frequently Asked Questions

It gets or sets the background color of the current document — the page background behind body content in legacy HTML pages.
Yes. MDN marks Document.bgColor as deprecated in the HTML specification. Use CSS background-color (for example on body) in new code.
A string color name (such as darkblue or white) or a hexadecimal color (such as #ff0000). Setting null is converted to the empty string.
MDN notes the default bgColor in Mozilla Firefox is white (#ffffff in hexadecimal).
Use CSS background-color, accessible through the DOM as document.body.style.backgroundColor, or preferably a stylesheet rule such as body { background-color: ... }.
No. Style page backgrounds with CSS or component styles. Learn bgColor only to read or migrate old scripts.
Did you know?

The legacy Document color family—bgColor, fgColor, linkColor, vlinkColor, and alinkColor—mirrored attributes you could once put directly on the <body> tag. CSS made those global JavaScript hooks unnecessary for new websites.

Next: body

Learn the standard Document.body reference to your page content.

body →

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.

6 people found this page helpful