Home JavaScript Document Properties fgColor JavaScript Document fgColor 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.
Call What it does document.fgColorPrimary signature.
Built into the browser. Read or assign document.fgColor — no import required.
Library declarations Parameters Parameter Description Writable? Required. Getter and setter
Return Value Return Value Description * The current value of document.fgColor.
JavaScript Examples 1. Read value 2. Set value 3. CSS color Example 1 — Read fgColor Log the current foreground text-color string.
console.log(document.fgColor);
console.log(typeof document.fgColor); #000000
string
/* exact string varies by browser */ How It Works Read the property. Returns a string color (name or hex).Check typeof. Always a string in supporting browsers.Defaults differ. Firefox’s default is black #000000 (MDN).Deprecated. Prefer CSS / computed style in new work.Example 2 — Set fgColor (Legacy) MDN-style pair: white text on a dark blue background.
document.fgColor = "white";
document.bgColor = "darkblue";
console.log(document.fgColor);
console.log(document.bgColor); white
darkblue
/* browser may normalize the strings */ How It Works Assign fgColor. Sets document text color (MDN).Assign bgColor. Matching background for the classic sample.Re-read both. Confirms stored values (engines may normalize).Do not ship this. Use CSS for production styling.Example 3 — Prefer CSS color Modern replacement recommended by MDN.
document.body.style.color = "crimson";
console.log(document.body.style.color);
console.log(getComputedStyle(document.body).color); crimson
rgb(220, 20, 60)
/* computed form varies by browser */ How It Works Set body.style.color. Uses the CSSOM, not Document color props.Read the inline style. Shows the value you assigned.Use getComputedStyle. Sees the resolved color the browser paints.Prefer a stylesheet in real apps: body { color: … }.Compatibility
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.
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 Frequently Asked Questions What does document.fgColor do? It gets or sets the foreground color — the text color — of the current document (MDN).
Is fgColor deprecated? Yes. MDN marks Document.fgColor as Deprecated. Document color attributes are superseded by CSS. Prefer the CSS color property.
What values does it accept? A string color name (such as white or red) or a hexadecimal color (such as #ff0000).
What is the default in Firefox? MDN notes the default fgColor in Mozilla Firefox is black (#000000 in hexadecimal).
What is the modern replacement? Use CSS color — for example body { color: red; } or document.body.style.color = "red". You can also inspect with getComputedStyle.
Should I use fgColor in new projects? No. Style text with CSS or design-system tokens. Learn fgColor only to read or migrate old scripts.
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.
11 people found this page helpful
Helpful Share Copy link Suggestion