AD JB Toolbox 01 EN

CSS / Reference / font-size: Understand px, em, rem, and Text Enlargement

font-size sets text size. In this property, em and percentages resolve against the parent’s computed font size, while rem uses the root; px is a CSS-pixel length that still participates in browser zoom.

Syntax and value choices

Property Initial value Inherited Main application conditions
font-size medium Yes All elements; visible effects depend on the text, image, or box conditions below

Initial values are not necessarily the values assigned to HTML elements by the browser’s stylesheet. Inspect the actual result in developer tools’ Computed panel.

html { font-size: 100%; }
body { font-size: 1rem; }
h2 { font-size: 1.5rem; }
Value or category Behavior and choice
px A CSS-pixel length; zoom can still enlarge it.
em / % Resolve against the parent’s font size for this property.
rem Resolve against the root html font size.
larger / smaller Choose a relative keyword size.

For supported values and conditions, see MDN font-size.

Run the example and compare results

Save this complete code as css-reference-10544-example.html in UTF-8 and open it in a browser. Keep its demonstration styles in that file rather than pasting them into a site-wide stylesheet.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>font-size comparison example</title>
<style>
body { margin: 0; color: #20242b; background: #fff; font: 16px/1.7 "Malgun Gothic", sans-serif; }
#demo { padding: 24px; max-width: 820px; margin: auto; }
h1 { font-size: 22px; margin: 0 0 20px; }
.samples { display: flex; flex-wrap: wrap; gap: 24px; }
.label { margin: 0 0 8px; font-weight: 700; }
.samples { font-size: 20px; }
.target { font-size: 1.5em; }
.reference { font-size: 1.5rem; }
</style>
</head>
<body>
<main id="demo">
<h1>font-size comparison</h1>
<section class="samples"><div class="a"><p class="label">Parent 20px × 1.5em</p><div class="target">CSS font size</div></div><div class="b"><p class="label">Root size × 1.5rem</p><div class="reference">CSS font size</div></div></section>
</main>
</body>
</html>

The first sample is 30px because its parent is 20px and its size is 1.5em. The rem sample depends on the root’s size, which must be inspected rather than assumed.

font-size browser comparison showing the specified and reference values in the same example

Nested em multipliers

Each nested em size uses its own parent, so repeated multipliers accumulate. Use rem for a consistent root-based scale or em for proportions within a component. Inspect the used pixel value in Computed to verify the intended reference.

Responsive text and enlargement

Negative font sizes are not valid. Viewport-only text sizing can make text too small on narrow screens; plan minimum and maximum sizes and test zoom. If larger text is clipped, inspect fixed heights, line height, and overflow before reducing the text size.

Compare nested em and rem references

Save this complete document as css-reference-10544-practice.html in UTF-8 and open it in a browser. Test it as a separate file rather than nesting it inside another example.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare nested em and rem references</title>
  <style>
    body { margin: 16px; color: #20242b; font: 16px/1.7 sans-serif; }
    main { max-width: 52rem; margin: auto; }
    h1 { font-size: 1.35rem; }
    .samples { display: flex; flex-wrap: wrap; gap: 24px; }
    .samples > * { max-width: 100%; min-width: 0; }
    :focus-visible { outline: 3px solid #005fcc; outline-offset: 3px; }
html { font-size:16px; } .parent { font-size:20px; padding:8px; background:#e6f2ff; } .em { font-size:1.5em; } .rem { font-size:1.5rem; }
  </style>
</head>
<body>
<main>
  <h1>Compare nested em and rem references</h1>
<p>em</p><div class="parent"><div class="em">Child<div class="em">Grandchild</div></div></div><p>rem</p><div class="parent"><div class="rem">Child<div class="rem">Grandchild</div></div></div>
</main>
</body>
</html>

Inspect Computed: em yields 30px then 45px; both rem descendants yield 24px. Change only html to 20px: the rem descendants become 30px, while the fixed 20px parent leaves em at 30/45px. Restore the root to 16px.

For coordinated font sizing, line heights, and typeface choices, see CSS Fonts, Font Sizes, and Line Height. Related properties to compare alongside these conditions are line-height.

When the result is missing or unexpected

  • Inspect both the parent and root font sizes.
  • Check height and wrapping restrictions if enlargement cuts text off.
More in This Category
CSS / Reference / calc(): Calculate CSS Values and Understand Their Types

CSS / Reference / calc(): Calculate CSS Values and Understand Their Types

Combine percentages and lengths with calc(), inspect the actual parent width, and distinguish invalid expressions from newer typed arithmetic and special numeric results.

CSS / Reference / empty-cells: Show or Hide Empty Table Cells

CSS / Reference / empty-cells: Show or Hide Empty Table Cells

Control empty-cell borders and backgrounds in a separated table while preserving its structure. Compare show, hide, whitespace, and the collapsed-border exception.

CSS / Reference / text-indent: First-Line Indents and Hanging Paragraphs

CSS / Reference / text-indent: First-Line Indents and Hanging Paragraphs

Create first-line and hanging indents, distinguish them from paragraph padding, and check inheritance, writing direction, and narrow-screen clipping.

CSS / Reference / quotes: Choose Nested Quotation Marks

CSS / Reference / quotes: Choose Nested Quotation Marks

Choose generated quotation pairs for nested text and distinguish them from literal punctuation. Compare automatic marks, none, and quotation-depth operations.

CSS / Reference / background-repeat: Tiling, Space, and Round

CSS / Reference / background-repeat: Tiling, Space, and Round

Control horizontal and vertical background tiling and compare repeat, no-repeat, space, and round with the same tile and positioning area.

CSS / Reference / content: Generate Pseudo-Element Text and Decoration

CSS / Reference / content: Generate Pseudo-Element Text and Decoration

Generate strings, attribute labels, counters, and decorative pseudo-elements. Compare none with an empty string and retain important information in real HTML.

CSS / Reference / font-weight: Numeric Weights, Relative Weights, and Font Matching

CSS / Reference / font-weight: Numeric Weights, Relative Weights, and Font Matching

Request numeric or relative font weights and inspect the supplied faces. Compare static and variable-font behavior without assuming every number produces a unique weight.

CSS / Reference / letter-spacing, word-spacing: Compare Character and Word Spacing

CSS / Reference / letter-spacing, word-spacing: Compare Character and Word Spacing

Adjust character and word gaps independently using the same text and a normal baseline. Account for inheritance, language, wrapping, and excessive negative spacing.

CSS / Reference / font-family: Font Lists, Glyph Fallback, and Loading

CSS / Reference / font-family: Font Lists, Glyph Fallback, and Loading

Build a font stack with appropriate generic fallbacks and inspect the fonts actually used. Distinguish naming a family from loading files and covering characters.

CSS / Reference / font-size: Understand px, em, rem, and Text Enlargement

CSS / Reference / font-size: Understand px, em, rem, and Text Enlargement

Choose text-size units by their reference and test nested em scaling. Check the actual root size, browser enlargement, and layout constraints.