String.prototype.fontcolor() is a deprecated HTML wrapper method (same API as MDN String.prototype.fontcolor()). It returns a string that wraps your text in a <font color="..."> tag. Learn color names vs hex triplets, quote escaping, why that element is obsolete, and how to replace it with CSS color — with five examples and try-it labs.
01
Kind
Instance method
02
Returns
HTML string
03
Status
Deprecated
04
Param
color
05
Mutates?
No
06
Prefer
CSS color
Fundamentals
Introduction
Early JavaScript included helpers that wrapped text in HTML presentation tags — fontcolor(), fontsize(), bold(), fixed(), and others. They made it easy to inject markup with innerHTML.
Today those wrappers are deprecated. fontcolor() still works in browsers for compatibility, but the <font> element itself was removed from HTML. Text color belongs in CSS.
💡
Learn it, don’t ship it
Study fontcolor() so you can recognize legacy code. For new pages, style text with CSS color (or a design token / utility class) instead of building <font> strings.
Calling str.fontcolor(color) does not create a live DOM node. It concatenates an HTML string: an opening <font color="..."> tag, the original text, and a closing </font>.
It is an instance method on strings (auto-boxed if needed).
It returns a new string — the original is unchanged.
Double quotes inside color become ".
No color validation — invalid values are still pasted into the attribute.
Prefer CSS color for real UI.
Foundation
📝 Syntax
General form of String.prototype.fontcolor:
JavaScript
str.fontcolor(color)
Parameters
color — a string (or value coerced to string) used as the color attribute. Typically a CSS color name ("red") or a hex RGB triplet without # ("FA8072").
Return value
Always an HTML string wrapped in <font color="...">. Special cases:
Escaping keeps the attribute delimiter intact when the returned string is parsed as HTML. It does not make the <font> element valid in modern HTML.
Example 4 — Injecting with innerHTML (Legacy)
How older demos used the returned string — still produces obsolete markup.
JavaScript
const contentString = "Hello, world";
const html = contentString.fontcolor("red");
// document.body.innerHTML = html; // legacy pattern — avoid in new apps
console.log(html);
// Warning: is not a valid element in modern HTML
<font color="red">Hello, world</font>
(invalid modern markup — <font> was removed from HTML)
How It Works
Even if a browser still paints the text red, the element is obsolete. Use CSS color instead.
Example 5 — Modern Replacement with CSS
MDN’s recommended approach — control color with the color property.
JavaScript
const el = document.createElement("p");
el.textContent = "Hello, world";
el.style.color = "red";
// document.body.appendChild(el);
console.log(el.style.color);
console.log(el.outerHTML);
// Hex works the modern way too:
el.style.color = "#FA8072";
You get a real element, valid markup, and full CSS color support (#hex, rgb(), hsl(), named colors, and more). Prefer a CSS class in production so styles stay maintainable.
Applications
🚀 Common Use Cases
Reading legacy tutorials — recognize HTML wrapper methods in old samples.
Migrating old scripts — replace fontcolor() with CSS color.
Teaching string immutability — show that methods return new strings.
Not for new apps — do not use fontcolor() to theme UI text.
Design systems — use color tokens / CSS variables instead.
Safer markup — prefer textContent + CSS over innerHTML strings.
🧠 How fontcolor() Builds Markup
1
Start with text + color
You call str.fontcolor(color) on a string receiver.
Input
2
Escape quotes in color
Double quotes become " in the attribute.
Escape
3
Concatenate HTML text
Build <font color="..."> + text + </font>.
Result
4
⚠️
Prefer CSS instead
Set color on a real element for valid styling.
Important
📝 Notes
fontcolor() is deprecated — standardized only for compatibility.
The <font> element was removed from the HTML specification.
It returns a string, not a DOM node.
It does not validate the color value.
Hex triplets traditionally omit # in this legacy API.
Other HTML wrappers (fontsize(), fixed(), bold(), …) share the same fate.
Avoid assigning untrusted strings to innerHTML.
Compatibility
Browser & Runtime Support
String.prototype.fontcolor() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.
✓ Deprecated · Legacy
String.fontcolor()
Available in modern browsers for old code, but MDN recommends CSS color instead.
LegacyCompatibility only
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
fontcolor()Avoid in new code
Bottom line: Recognize fontcolor() in legacy samples. For new UI, control text color with CSS color — never rely on the obsolete element from HTML wrapper methods.
Wrap Up
Conclusion
String.prototype.fontcolor() builds an obsolete <font color="...">...</font> HTML string. It is useful for understanding history and migrating old code — not for writing new features.
Mix HTML wrappers with modern component frameworks
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.fontcolor()
Deprecated HTML wrapper — prefer CSS color.
5
Core concepts
📝01
Returns
HTML string
API
⚠️02
Status
deprecated
Legacy
❌03
Markup
invalid
HTML
✓04
Replace
CSS color
Modern
⚡05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.fontcolor(color) returns an HTML string that wraps the text in a <font color="..."> tag — for example "Hello".fontcolor("red") returns '<font color="red">Hello</font>'.
Yes. MDN marks all HTML wrapper methods as deprecated. The <font> element itself was removed from the HTML specification. Prefer CSS color instead.
A CSS color name (like "red") or a hexadecimal RGB triplet without # (like "FA8072" for salmon). The method does not validate the value — it just puts it in the attribute.
No. Strings are immutable. fontcolor() returns a new string containing HTML markup. The original text stays the same.
Double quotes in color are escaped as " so the generated attribute stays well-formed as HTML text.
Set CSS color on an element, for example element.style.color = "red", or use a CSS class / custom property. Do not build <font> tags with string methods.
Did you know?
The old <font> tag could set color, size, and face in HTML. All three presentational jobs moved to CSS (color, font-size, font-family) — which is why fontcolor(), fontsize(), and similar string helpers exist only for backward compatibility today.