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
Fundamentals
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.
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.
MDN recommends accessing background color through document.body.style.backgroundColor when you need DOM-level control, but stylesheets are preferred for maintainability.
Cheat Sheet
⚡ Quick Reference
Goal
Code / note
Read background
document.bgColor
Set name (MDN)
document.bgColor = "darkblue"
Set hex
document.bgColor = "#f0f4f8"
Clear (legacy)
document.bgColor = null → ""
Modern replacement
document.body.style.backgroundColor
MDN status
Deprecated
Snapshot
🔍 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
Compare
📋 bgColor vs body.style.backgroundColor
document.bgColor
body.style.backgroundColor
Recommended?
No
Yes (when JS is needed)
MDN status
Deprecated
Standard DOM CSSOM
Stylesheet friendly
No
Prefer CSS rules instead
Best for
Reading old code
Runtime theme toggles
Hands-On
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.
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.
LegacyCompatibility only
Google ChromeCompatibility support · prefer CSS
Legacy support
Mozilla FirefoxCompatibility support · default #ffffff
Legacy support
Apple SafariCompatibility support
Legacy support
Microsoft EdgeChromium compatibility layer
Legacy support
OperaFollow Chromium behavior
Legacy support
Internet ExplorerHistorical legacy target
Legacy support
Document.bgColorAvoid 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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.bgColor
Deprecated background color — prefer CSS background-color.
5
Core concepts
🎨01
Type
string color
API
⚠️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.