JavaScript String fontsize() Method

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

What You’ll Learn

String.prototype.fontsize() is a deprecated HTML wrapper method (same API as MDN String.prototype.fontsize()). It returns a string that wraps your text in a <font size="..."> tag. Learn the old 1–7 size scale, relative +n/-n values, quote escaping, why that element is obsolete, and how to replace it with CSS font-size — with five examples and try-it labs.

01

Kind

Instance method

02

Returns

HTML string

03

Status

Deprecated

04

Param

size

05

Mutates?

No

06

Prefer

CSS font-size

Introduction

Early JavaScript included helpers that wrapped text in HTML presentation tags — fontsize(), fontcolor(), big(), small(), and others. They made it easy to inject markup with innerHTML.

Today those wrappers are deprecated. fontsize() still works in browsers for compatibility, but the <font> element itself was removed from HTML. Font size belongs in CSS — with units like rem, em, or px, not a 1–7 HTML attribute scale.

💡
Learn it, don’t ship it

Study fontsize() so you can recognize legacy code. For new pages, style text with CSS font-size (or a type-scale class) instead of building <font> strings.

This page is part of JavaScript String Methods. Related topics include fontcolor() and big().

Understanding the fontsize() Method

Calling str.fontsize(size) does not create a live DOM node. It concatenates an HTML string: an opening <font size="..."> 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 size become &quot;.
  • No size validation — values outside 1–7 are still pasted into the attribute.
  • Prefer CSS font-size for real UI.

📝 Syntax

General form of String.prototype.fontsize:

JavaScript
str.fontsize(size)

Parameters

  • size — historically an integer from 1 to 7, or a string like "+3" / "-2" to adjust relative to the default size 3. Coerced to string for the attribute.

Return value

Always an HTML string wrapped in <font size="...">. Special cases:

SituationReturns
Absolute size (1–7)"Hi".fontsize(7)'<font size="7">Hi</font>'
Relative size string"Hi".fontsize("+3")'<font size="+3">Hi</font>' (relative to default 3)
Quotes in size" escaped as &quot; inside the attribute
Empty receiver"".fontsize(3)'<font size="3"></font>'
Odd / unvalidated valuesStill concatenated (e.g. 7.111, -1) — no clamping
Valid modern HTML?No<font> was removed from the HTML specification

Common patterns

JavaScript
"Hello, world".fontsize(7);
// 'Hello, world'

"Title".fontsize("+2");
// 'Title'

"Hi".fontsize('3"4');
// 'Hi'

// Prefer this in new code:
const el = document.getElementById("yourElemId");
el.style.fontSize = "2em";

⚡ Quick Reference

GoalCode
Legacy HTML stringstr.fontsize(7)
Relative legacy sizestr.fontsize("+3")
Escape quotes in sizeAutomatic ("&quot;)
Modern font sizeel.style.fontSize = "2em"
Use in new apps?No — deprecated

🔍 At a Glance

Four facts to remember about String.fontsize().

Returns
string

HTML markup text

Status
deprecated

Compatibility only

Tag
<font size>

Removed from HTML

Replace with
font-size

CSS, not tags

📋 fontsize() vs CSS font-size

str.fontsize(size)el.style.fontSize
ResultHTML stringStyled live element
Valid HTML today?No (<font> removed)Yes
Size modelLegacy 1–7 / +nrem, em, px, keywords…
Recommended?NoYes

Examples Gallery

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

📚 Getting Started

See the exact HTML string fontsize() builds.

Example 1 — Basic fontsize()

MDN-style call that wraps text with an absolute size.

JavaScript
const contentString = "Hello, world";
contentString.fontsize(7);
// '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 — Relative Sizes (+n / -n)

MDN note: strings like "+3" or "-2" adjust relative to the default size 3.

JavaScript
"Hello".fontsize("+3");
// 'Hello'

"Hello".fontsize("-2");
// 'Hello'

"Hello".fontsize(3);   // default-ish baseline in the old scale
// 'Hello'
Try It Yourself

How It Works

In modern CSS you would use relative units like em / rem or calc() instead of this HTML size scale.

📈 Practical Patterns

Escaping, legacy injection, and the modern CSS replacement.

Example 3 — Quotes and Unvalidated Sizes

Double quotes are escaped; odd values are still written into the attribute.

JavaScript
"Hi".fontsize('3"4');
// 'Hi'

"".fontsize(3);
// ''

"A".fontsize(7.111);
// 'A'
Try It Yourself

How It Works

Escaping keeps the attribute delimiter intact. The method still does not make <font> valid modern HTML, and it does not clamp sizes to 1–7.

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.fontsize(7);
// 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 enlarges the text, the element is obsolete. Use CSS font-size instead.

Example 5 — Modern Replacement with CSS

MDN’s recommended approach — control size with the font-size property.

JavaScript
const el = document.createElement("p");
el.textContent = "Hello, world";
el.style.fontSize = "2em";

// document.body.appendChild(el);
console.log(el.style.fontSize);
console.log(el.outerHTML);

// Other modern options:
// el.style.fontSize = "1.25rem";
// el.style.fontSize = "xx-large";
Try It Yourself

How It Works

You get a real element, valid markup, and full CSS sizing power. Prefer a CSS class or design-token type scale in production.

🚀 Common Use Cases

  • Reading legacy tutorials — recognize HTML wrapper methods in old samples.
  • Migrating old scripts — replace fontsize() with CSS font-size.
  • Teaching string immutability — show that methods return new strings.
  • Not for new apps — do not use fontsize() to scale UI text.
  • Design systems — use type-scale tokens / utility classes instead.
  • Safer markup — prefer textContent + CSS over innerHTML strings.

🧠 How fontsize() Builds Markup

1

Start with text + size

You call str.fontsize(size) on a string receiver.

Input
2

Escape quotes in size

Double quotes become &quot; in the attribute.

Escape
3

Concatenate HTML text

Build <font size="..."> + text + </font>.

Result
4

Prefer CSS instead

Set font-size on a real element for valid styling.

📝 Notes

  • fontsize() 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 or clamp the size value.
  • Legacy docs describe sizes 17 and relative +n/-n vs default 3.
  • Other HTML wrappers (fontcolor(), big(), fixed(), …) share the same fate.
  • Avoid assigning untrusted strings to innerHTML.

Browser & Runtime Support

String.prototype.fontsize() is still widely implemented for compatibility, but it is deprecated. Do not use it in new projects.

Deprecated · Legacy

String.fontsize()

Available in modern browsers for old code, but MDN recommends CSS font-size 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
fontsize() Avoid in new code

Bottom line: Recognize fontsize() in legacy samples. For new UI, control text size with CSS font-size — never rely on the obsolete element from HTML wrapper methods.

Conclusion

String.prototype.fontsize() builds an obsolete <font size="...">...</font> HTML string. It is useful for understanding history and migrating old code — not for writing new features.

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

💡 Best Practices

✅ Do

  • Use CSS font-size with rem/em for scalable type
  • Prefer semantic headings when size means hierarchy
  • Prefer textContent over string-built HTML
  • Replace fontsize() when you touch legacy files
  • Learn wrappers only to recognize deprecated APIs

❌ Don’t

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

Key Takeaways

Knowledge Unlocked

Five things to remember about String.fontsize()

Deprecated HTML wrapper — prefer CSS font-size.

5
Core concepts
⚠️ 02

Status

deprecated

Legacy
03

Markup

invalid

HTML
04

Replace

font-size

Modern
05

Mutates

no

Immutable

❓ Frequently Asked Questions

String.prototype.fontsize(size) returns an HTML string that wraps the text in a <font size="..."> tag — for example "Hello".fontsize(7) returns '<font size="7">Hello</font>'.
Yes. MDN marks all HTML wrapper methods as deprecated. The <font> element itself was removed from the HTML specification. Prefer CSS font-size instead.
Historically an integer from 1 to 7, or a relative string like "+3" or "-2" (relative to the default size 3). The method does not validate the value — it just puts it in the attribute.
No. Strings are immutable. fontsize() returns a new string containing HTML markup. The original text stays the same.
Double quotes in size are escaped as &quot; so the generated attribute stays well-formed as HTML text.
Set CSS font-size on an element, for example element.style.fontSize = "2em" or "1.25rem", or use a CSS class / type scale. Do not build <font> tags with string methods.
Did you know?

The old HTML size attribute used a tiny 1–7 ladder (with 3 as the default). CSS replaced that with continuous units and keywords — so fontsize(7) is a historical curiosity, not a modern type system.

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