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
Fundamentals
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.
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 ".
No size validation — values outside 1–7 are still pasted into the attribute.
Prefer CSS font-size for real UI.
Foundation
📝 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:
Situation
Returns
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 " inside the attribute
Empty receiver
"".fontsize(3) → '<font size="3"></font>'
Odd / unvalidated values
Still 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";
Cheat Sheet
⚡ Quick Reference
Goal
Code
Legacy HTML string
str.fontsize(7)
Relative legacy size
str.fontsize("+3")
Escape quotes in size
Automatic (" → ")
Modern font size
el.style.fontSize = "2em"
Use in new apps?
No — deprecated
Snapshot
🔍 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
Compare
📋 fontsize() vs CSS font-size
str.fontsize(size)
el.style.fontSize
Result
HTML string
Styled live element
Valid HTML today?
No (<font> removed)
Yes
Size model
Legacy 1–7 / +n
rem, em, px, keywords…
Recommended?
No
Yes
Hands-On
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.
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
You get a real element, valid markup, and full CSS sizing power. Prefer a CSS class or design-token type scale in production.
Applications
🚀 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 " 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.
Important
📝 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 1–7 and relative +n/-n vs default 3.
Other HTML wrappers (fontcolor(), big(), fixed(), …) share the same fate.
Avoid assigning untrusted strings to innerHTML.
Compatibility
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.
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
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.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.fontsize()
Deprecated HTML wrapper — prefer CSS font-size.
5
Core concepts
📝01
Returns
HTML string
API
⚠️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 " 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.