JavaScript String fontcolor() Method

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

What You’ll Learn

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

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.

This page is part of JavaScript String Methods. Related topics include fixed() and bold().

Understanding the fontcolor() Method

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 &quot;.
  • No color validation — invalid values are still pasted into the attribute.
  • Prefer CSS color for real UI.

📝 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:

SituationReturns
Color name"Hi".fontcolor("red")'<font color="red">Hi</font>'
Hex RGB triplet (no #)"Hi".fontcolor("FA8072")'<font color="FA8072">Hi</font>'
Quotes in color" escaped as &quot; inside the attribute
Empty receiver"".fontcolor("navy")'<font color="navy"></font>'
Missing / non-string colorCoerced to string (e.g. no arg → color="undefined")
Valid modern HTML?No<font> was removed from the HTML specification

Common patterns

JavaScript
"Hello, world".fontcolor("red");
// 'Hello, world'

"Title".fontcolor("ff00dd");
// 'Title'

"Hi".fontcolor('red"blue');
// 'Hi'

// Prefer this in new code:
const el = document.getElementById("yourElemId");
el.style.color = "red";

⚡ Quick Reference

GoalCode
Legacy HTML stringstr.fontcolor("red")
Hex color (legacy)str.fontcolor("FA8072")
Escape quotes in colorAutomatic ("&quot;)
Modern text colorel.style.color = "red"
Use in new apps?No — deprecated

🔍 At a Glance

Four facts to remember about String.fontcolor().

Returns
string

HTML markup text

Status
deprecated

Compatibility only

Tag
<font color>

Removed from HTML

Replace with
color

CSS, not tags

📋 fontcolor() vs CSS color

str.fontcolor(color)el.style.color
ResultHTML stringStyled live element
Valid HTML today?No (<font> removed)Yes
Recommended?NoYes
Best forReading legacy codeNew UI / apps

Examples Gallery

Examples follow MDN String.fontcolor() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

See the exact HTML string fontcolor() builds.

Example 1 — Basic fontcolor()

MDN-style call that wraps text with a named color.

JavaScript
const contentString = "Hello, world";
contentString.fontcolor("red");
// 'Hello, world'
Try It Yourself

How It Works

The method returns markup text only. Nothing is added to the document until you assign it somewhere (for example innerHTML).

Example 2 — Hex RGB Triplet

MDN note: use rrggbb without a leading # for hex colors.

JavaScript
// Salmon: red=FA, green=80, blue=72
"Hello".fontcolor("FA8072");
// 'Hello'

"Hello".fontcolor("ff00dd");
// 'Hello'
Try It Yourself

How It Works

The method does not normalize or validate colors. In modern CSS you would usually write #FA8072 or rgb(...) on style.color.

📈 Practical Patterns

Escaping, legacy injection, and the modern CSS replacement.

Example 3 — Quotes in the color

Double quotes inside color are escaped for safer HTML attributes.

JavaScript
"Hi".fontcolor('red"blue');
// 'Hi'

"".fontcolor("navy");
// ''

"A".fontcolor(123);
// 'A'
Try It Yourself

How It Works

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
Try It Yourself

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";
Try It Yourself

How It Works

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.

🚀 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 &quot; 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.

📝 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.

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.

Legacy Compatibility only
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

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.

Continue with fontsize(), at(), or the String methods hub.

💡 Best Practices

✅ Do

  • Use CSS color (or design tokens) for text color
  • Prefer CSS variables for themeable palettes
  • Prefer textContent over string-built HTML
  • Replace fontcolor() when you touch legacy files
  • Learn wrappers only to recognize deprecated APIs

❌ Don’t

  • Use fontcolor() in new production code
  • Assume <font> is valid modern HTML
  • Inject untrusted fontcolor() output via innerHTML
  • Expect fontcolor() to return a live element
  • Mix HTML wrappers with modern component frameworks

Key Takeaways

Knowledge Unlocked

Five things to remember about String.fontcolor()

Deprecated HTML wrapper — prefer CSS color.

5
Core concepts
⚠️ 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 &quot; 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.

More String Methods

Return to the hub for slice, trim, replace, and more.

String methods hub →

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.

8 people found this page helpful