JavaScript Document fgColor Property

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

What You’ll Learn

Document.fgColor is a deprecated instance property that gets or sets the document’s foreground text color as a string. Learn how legacy pages used it with bgColor, the Firefox default, the MDN-recommended CSS color replacement, and five examples with try-it labs.

01

Kind

Instance property

02

Type

string color

03

Status

Deprecated

04

Controls

Text / foreground

05

Replace

CSS color

06

Pairs with

bgColor

Introduction

Before CSS took over page styling, scripts could tint an entire document with Document color properties: fgColor for text (foreground), bgColor for the page background, and link colors such as linkColor, vlinkColor, and alinkColor.

MDN’s recommended alternative today is the CSS color property—for example document.body.style.color = "red", or better, a stylesheet rule.

💡
Foreground = text color

“fg” stands for foreground. Setting fgColor aims at the document’s default text color, not borders or backgrounds.

Related Document tutorials: bgColor, alinkColor, Document constructor.

Understanding Document.fgColor

A get/set instance property on Document. It reads and writes the document foreground (text) color as a string (MDN).

  • Value — color name (e.g. "red") or hex (e.g. "#ff0000").
  • Get / set — both reading and assigning are supported where the property still exists.
  • Deprecated — HTML specification / MDN: avoid in new code.
  • Firefox default — black (#000000) per MDN.
  • Modern path — CSS color on body (or a theme root).

📝 Syntax

JavaScript
document.fgColor
document.fgColor = colorString;

Value

A string representing the color as a word (e.g. "red") or hexadecimal value (e.g. "#ff0000") (MDN).

MDN classic pairing

JavaScript
document.fgColor = "white";
document.bgColor = "darkblue";

Modern replacement

JavaScript
document.body.style.color = "red";
// Prefer CSS: body { color: red; }

⚡ Quick Reference

GoalCode / note
Read text colordocument.fgColor
Set name (MDN)document.fgColor = "white"
Set hexdocument.fgColor = "#f8fafc"
MDN pair with backgroundbgColor = "darkblue"
Modern replacementdocument.body.style.color
MDN statusDeprecated

🔍 At a Glance

Four facts about document.fgColor.

Type
string

Color value

Targets
foreground

Text color

Status
deprecated

Legacy API

Use CSS
color

Modern fix

📋 fgColor vs body.style.color

document.fgColorbody.style.color
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: fgColor. Open try-it labs to see text color changes where the property still applies.

📚 Getting Started

Set and read the deprecated document foreground color.

Example 1 — MDN: White Text on Dark Blue

The classic MDN pairing of fgColor with bgColor.

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

How It Works

Legacy pages often set both text and background together for readable contrast.

Example 2 — Read document.fgColor

Log the current foreground color (browser-dependent).

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

How It Works

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

📈 Hex, Contrast & Modern CSS

Precise colors and the recommended replacement.

Example 3 — Set a Hex Foreground

Use a hexadecimal color for precise brand text.

JavaScript
document.fgColor = "#0f172a";
console.log(document.fgColor);
Try It Yourself

How It Works

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

Example 4 — Keep Text Readable

Educational pattern: never set light text without a dark background (and vice versa).

JavaScript
// Legacy demo only — prefer CSS in real apps
document.bgColor = "#0f172a";
document.fgColor = "#f8fafc";
console.log("fg:", document.fgColor, "bg:", document.bgColor);
Try It Yourself

How It Works

In modern CSS, use design tokens and check contrast ratios instead of Document color properties.

Example 5 — Modern Replacement with CSS

MDN’s recommended approach—style text with CSS color.

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

How It Works

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

🚀 Common Use Cases

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

🧠 How Document Foreground Color Worked

1

Legacy script runs

document.fgColor = "white" (often with bgColor).

Legacy
2

Browser paints text color

Document-level foreground applied as the default text color.

Render
3

CSS may override

Modern stylesheets often take precedence over legacy hooks.

Cascade
4

Prefer CSS color

Define text colors in stylesheets or body.style.color for runtime changes.

📝 Notes

  • fgColor is deprecated in the HTML specification (MDN).
  • Firefox default is #000000 (black) per MDN.
  • Sibling legacy properties: bgColor, linkColor, vlinkColor, alinkColor.
  • MDN replacement: CSS color via document.body.style.color.
  • Related: bgColor, alinkColor, featurePolicy.

Legacy Browser Support

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

Deprecated · Legacy

Document.fgColor

Legacy document foreground (text) color getter/setter — use CSS color in new code.

Legacy Compatibility only
Google Chrome Compatibility support · prefer CSS
Legacy support
Mozilla Firefox Compatibility support · default #000000
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.fgColor Avoid in new code

Bottom line: Recognize document.fgColor in old scripts. For new text colors, use CSS color on body or a theme root — never document.fgColor.

Conclusion

Document.fgColor is a deprecated way to get or set a document’s default text color. Learn it to maintain old code—then migrate to CSS color (and pair backgrounds with background-color, not bgColor, in new work).

Continue with firstElementChild, bgColor, alinkColor, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use CSS color for text styling
  • Prefer design tokens / CSS variables for themes
  • Check contrast when pairing text and backgrounds
  • Treat any document.fgColor = as tech debt
  • Migrate siblings (bgColor, link colors) together

❌ Don’t

  • Set text color with document.fgColor in new features
  • Assume every browser still honors the property
  • Use light text on light backgrounds
  • Mix legacy Document colors with modern CSS without testing
  • Rely on Firefox defaults as a cross-browser contract

Key Takeaways

Knowledge Unlocked

Five things to remember about document.fgColor

Deprecated text color API — prefer CSS color.

5
Core concepts
⚠️02

Status

deprecated

MDN
🎨03

Type

string color

API
🚫04

Default

#000000

Firefox
05

Replace

CSS color

Modern

❓ Frequently Asked Questions

It gets or sets the foreground color — the text color — of the current document.
Yes. MDN marks Document.fgColor deprecated in the HTML specification. Prefer the CSS color property in new code.
A string color name (such as white or red) or a hexadecimal color (such as #ff0000).
MDN notes the default fgColor in Mozilla Firefox is black (#000000 in hexadecimal).
Use CSS color, for example document.body.style.color = "red", or preferably a stylesheet rule such as body { color: ... }.
No. Style text with CSS or design-system tokens. Learn fgColor only to read or migrate old scripts.
Did you know?

The same era that gave us document.fgColor also exposed <body text="..."> and bgcolor HTML attributes. All of those document-level color knobs were replaced by the CSS cascade—one of the biggest clarity wins in web styling history.

Next: firstElementChild

Learn the document’s first child Element—usually the root <html>.

firstElementChild →

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