JavaScript Document fgColor Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

document.fgColor gets or sets the foreground color — the text color — of the current document (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.fgColor. Start with this line.

JavaScript
document.fgColor
CallWhat it does
document.fgColorPrimary signature.

Built into the browser. Read or assign document.fgColor — no import required.

Library declarations
JavaScript
document.fgColor

Parameters

ParameterDescription
Writable?Required. Getter and setter

Return Value

Return ValueDescription
*The current value of document.fgColor.

JavaScript Examples

Example 1 — Read fgColor

Log the current foreground text-color string.

JavaScript
console.log(document.fgColor);
console.log(typeof document.fgColor);
Try It Yourself

How It Works

  1. Read the property. Returns a string color (name or hex).
  2. Check typeof. Always a string in supporting browsers.
  3. Defaults differ. Firefox’s default is black #000000 (MDN).
  4. Deprecated. Prefer CSS / computed style in new work.

Example 2 — Set fgColor (Legacy)

MDN-style pair: white text on a dark blue background.

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

console.log(document.fgColor);
console.log(document.bgColor);
Try It Yourself

How It Works

  1. Assign fgColor. Sets document text color (MDN).
  2. Assign bgColor. Matching background for the classic sample.
  3. Re-read both. Confirms stored values (engines may normalize).
  4. Do not ship this. Use CSS for production styling.

Example 3 — Prefer CSS color

Modern replacement recommended by MDN.

JavaScript
document.body.style.color = "crimson";
console.log(document.body.style.color);
console.log(getComputedStyle(document.body).color);
Try It Yourself

How It Works

  1. Set body.style.color. Uses the CSSOM, not Document color props.
  2. Read the inline style. Shows the value you assigned.
  3. Use getComputedStyle. Sees the resolved color the browser paints.
  4. Prefer a stylesheet in real apps: body { color: … }.

Browser Support

Document.fgColor is a legacy Document color property. MDN marks it Deprecated. It may still work for compatibility, but CSS color is the supported approach.

✓ Deprecated

Document.fgColor

Still readable in many browsers for old pages. Do not rely on it for new UI — use CSS color.

Legacy Deprecated API
Google Chrome Legacy support
Yes
Microsoft Edge Legacy support
Yes
Mozilla Firefox Legacy · default #000000
Yes
Apple Safari Legacy support
Yes
Opera Legacy support
Yes
Internet Explorer Legacy support
Yes
fgColor Prefer CSS color

Bottom line: Learn it for migration only. See the MDN Document.fgColor reference and use CSS color instead.

Frequently Asked Questions

It gets or sets the foreground color — the text color — of the current document (MDN).
Yes. MDN marks Document.fgColor as Deprecated. Document color attributes are superseded by CSS. Prefer the CSS color property.
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 body { color: red; } or document.body.style.color = "red". You can also inspect with getComputedStyle.
No. Style text with CSS or design-system tokens. Learn fgColor only to read or migrate old scripts.

Next: Document firstElementChild

Learn how to read the first element child of the document element.

firstElementChild tutorial →

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.

11 people found this page helpful