JavaScript String fontsize() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

String.prototype.fontsize(size) returns an HTML string that wraps your text in a <font size="..."> tag. It is a deprecated HTML wrapper (legacy only) and does not create a live DOM node.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call string.fontsize. Start with this line.

JavaScript
str.fontsize(size)
CallWhat it does
str.fontsize(size)Primary signature.
string.fontsize(...)Call with the arguments your case needs.

Built into JavaScript. Call fontsize(...) on a string (or String instance) — no import required.

Library declarations
JavaScript
str.fontsize(size)

Parameters

ParameterDescription
sizeRequired. Attribute value (coerced to string; not validated)

Return Value

Return ValueDescription
*HTML string with <font size>

JavaScript Examples

Example 1 — Absolute Size (1–7)

Wrap text with a classic HTML font size and log the HTML string.

JavaScript
const text = "Hello, world";

console.log(text.fontsize(7));
// '<font size="7">Hello, world</font>'

console.log(text.fontsize(1));
// '<font size="1">Hello, world</font>'

console.log(text);  // "Hello, world" (unchanged)
Try It Yourself

How It Works

  1. Start with text. text holds "Hello, world".
  2. Pick a size. 7 is the largest classic value; 1 is the smallest.
  3. Get a string. The return value is markup text, not a painted element.
  4. Original stays put. Strings are immutable — text is unchanged.

Example 2 — Relative Size (+n / -n)

Pass a relative size string. Historically these were relative to default size 3.

JavaScript
console.log("Title".fontsize("+2"));
// '<font size="+2">Title</font>'

console.log("Note".fontsize("-1"));
// '<font size="-1">Note</font>'

// No validation — any value is pasted into the attribute:
console.log("X".fontsize("huge"));
// '<font size="huge">X</font>'
Try It Yourself

How It Works

  1. Relative strings. "+2" and "-1" are written into the size attribute as-is.
  2. Old browser meaning. Relative sizes were relative to the default size 3.
  3. No checks. Invalid values like "huge" still produce a string — they are not rejected.
  4. Quotes escape. A " inside size becomes &quot; in the attribute.

Example 3 — Modern CSS font-size

Style a real element instead of building obsolete <font> markup.

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

console.log(el.style.fontSize);  // "2em"
console.log(el.outerHTML);
// '<p style="font-size: 2em;">Hello, world</p>'

// rem / px work too:
el.style.fontSize = "1.25rem";
Try It Yourself

How It Works

  1. Create a real node. createElement("p") gives you a live element.
  2. Set safe text. textContent avoids HTML injection from string-built tags.
  3. Apply CSS. style.fontSize accepts modern units (em, rem, px, …).
  4. Prefer classes in apps. A type scale / CSS class is easier to maintain than inline styles.

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. MDN recommends CSS font-size instead of building <font> tags.

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 <font> element from HTML wrapper methods.

Frequently Asked Questions

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.

Next: italics()

See another deprecated HTML wrapper — and why CSS font-style replaced it.

italics() 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.

9 people found this page helpful