CSS font-size Property

The font-size property sets the size of text by controlling the font's em square.

Font sizes can be specified using absolute lengths, relative units, percentages, or predefined keywords. Relative units such as rem and em are particularly useful for scalable typography, while functions such as clamp() allow text sizes to adapt to different screen widths.

font-size Syntax

The font-size property accepts a non-negative length, a non-negative percentage, an absolute-size keyword, or a relative-size keyword.

Basic Syntax

selector {
  font-size: 16px;
}

Relative Length

selector {
  font-size: 1.25rem;
}

Percentage

selector {
  font-size: 125%;
}

Keyword

selector {
  font-size: large;
}

Responsive Size

selector {
  font-size: clamp(1rem, 2vw + 0.5rem, 2rem);
}

CSS-Wide Keywords

.example {
  font-size: inherit;
}
.example {
  font-size: initial;
}
.example {
  font-size: unset;
}
.example {
  font-size: revert;
}
.example {
  font-size: revert-layer;
}

Negative values are invalid. A value of 0 is allowed, although it makes the font's em square zero-sized and generally prevents the text from being visibly rendered.

Property Values

The font-size property supports several types of values.

Value Type Example Description
Absolute length 20px Specifies a size using a fixed CSS length unit.
Relative length 1.25rem Specifies a size relative to another measurement.
Percentage 125% Specifies a size relative to the parent element's computed font size.
Absolute-size keyword large Selects a size from the browser's predefined font-size scale.
Relative-size keyword larger Adjusts the size relative to the parent's font size.
CSS calculation clamp(1rem, 2vw, 2rem) Calculates a font size using supported CSS math functions.

Property Characteristics

Characteristic Value
Initial value medium
Inherited Yes
Applies to All elements and text
Computed value An absolute length
Animation type By computed value type
Percentages Relative to the parent element's font size

The initial value medium commonly corresponds to 16px under default browser settings, but users can change their preferred font size.

Absolute Lengths

Absolute CSS length units specify a font size without referencing the parent element's font size.

Pixels

p {
  font-size: 18px;
}

The px unit represents CSS pixels. CSS pixels are reference units and do not necessarily correspond to individual physical screen pixels.

Points

p {
  font-size: 12pt;
}

Points are commonly associated with print typography. One CSS point equals 1/72 of a CSS inch.

Other Absolute Length Units

Unit Name CSS Relationship
px Pixel 1/96 of a CSS inch
pt Point 1/72 of a CSS inch
pc Pica 12 points
in Inch 96 CSS pixels
cm Centimeter 96/2.54 CSS pixels
mm Millimeter 1/10 of a centimeter
Q Quarter-millimeter 1/4 of a millimeter

Example

.small-text {
  font-size: 14px;
}
.body-text {
  font-size: 18px;
}
.large-text {
  font-size: 28px;
}

Absolute lengths can be useful when a specific CSS size is required. For general website typography, relative units often provide greater flexibility when users change their default text size.

Relative Lengths

Relative length units calculate font size using another measurement, such as the parent font size, root font size, or viewport dimensions.

Common Relative Units

Unit Relative To
em The parent element's computed font size when used in font-size.
rem The root element's font size.
ex The font's x-height, or an approximation.
ch The advance width of the 0 glyph, or an approximation.
vw 1% of the viewport width.
vh 1% of the viewport height.
vmin 1% of the smaller viewport dimension.
vmax 1% of the larger viewport dimension.

Using em

.container {
  font-size: 20px;
}
.container p {
  font-size: 1.2em;
}

The paragraph's computed font size is 24px because 1.2em is 120% of the parent's 20px font size.

Using rem

html {
  font-size: 16px;
}
h2 {
  font-size: 2rem;
}

The heading's computed font size is 32px.

Using Viewport Units

.display-title {
  font-size: 5vw;
}

The font size changes as the viewport width changes. A value based only on viewport units can become too small or too large, so it is generally better to establish limits with clamp().

Percentages

Percentage values set the font size relative to the parent element's computed font size.

Basic Percentage

.container {
  font-size: 20px;
}
.container p {
  font-size: 150%;
}

The paragraph's computed font size is 30px.

Smaller Text

.small-text {
  font-size: 80%;
}

The element uses 80% of its parent's font size.

Larger Text

.large-text {
  font-size: 125%;
}

The element uses 125% of its parent's font size.

Percentage Calculations

Parent Font Size Percentage Result
20px 75% 15px
20px 100% 20px
20px 125% 25px
20px 150% 30px

Important Consideration

Percentage font sizes can compound through nested elements when each level applies another percentage. Use them carefully in deeply nested components.

Absolute-Size Keywords

CSS provides predefined font-size keywords that form a scale from very small to very large.

Available Keywords

Keyword Relative Size
xx-small Very small
x-small Extra small
small Small
medium Default size
large Large
x-large Extra large
xx-large Very large
xxx-large Extremely large

Example

.small-heading {
  font-size: small;
}
.normal-text {
  font-size: medium;
}
.large-heading {
  font-size: xx-large;
}

Using the math Keyword

math {
  font-size: math;
}

The math keyword is a specialized value used for mathematical content. It scales the font size according to the element's mathematical layout depth.

Important Consideration

Absolute-size keywords are not guaranteed to correspond to the same pixel sizes for every user. Browser settings and font-size preferences can influence the resulting values.

Relative-Size Keywords

The larger and smaller keywords adjust font size relative to the parent element.

Larger Text

.larger-text {
  font-size: larger;
}

Smaller Text

.smaller-text {
  font-size: smaller;
}

Parent and Child Example

.container {
  font-size: 20px;
}
.container .larger {
  font-size: larger;
}
.container .smaller {
  font-size: smaller;
}

The browser determines the resulting sizes using the relative-size scale defined by CSS.

Important Consideration

The keywords do not specify exact multiplication factors. Use em, rem, or percentages when a particular proportional relationship is required.

em vs. rem

The em and rem units both support scalable typography, but they use different reference sizes.

Using em

.parent {
  font-size: 20px;
}
.parent .child {
  font-size: 1.5em;
}

The child uses a computed font size of 30px.

Using rem

html {
  font-size: 16px;
}
.parent {
  font-size: 20px;
}
.parent .child {
  font-size: 1.5rem;
}

The child uses a computed font size of 24px, regardless of the parent's 20px font size.

Comparison

Unit Reference Typical Use
em Parent font size Component-relative text scaling.
rem Root font size Consistent typography across a page.

Nested em Values

.level-one {
  font-size: 1.2em;
}
.level-two {
  font-size: 1.2em;
}

When nested, each em value is calculated using the computed font size of its parent. This can cause font sizes to increase or decrease progressively.

Important Consideration

For the root element itself, rem values used in font-size are resolved using the initial font size rather than creating a circular dependency.

Inheritance and Nested Elements

The font-size property is inherited. Elements without their own font-size declaration generally use the computed font size of their parent.

Inherited Font Size

.container {
  font-size: 18px;
}
<div class="container">
  <p>This paragraph inherits the font size.</p>
</div>

Override the Inherited Size

.container {
  font-size: 18px;
}
.container .small-text {
  font-size: 14px;
}

Explicit Inheritance

.child {
  font-size: inherit;
}

Nested Relative Sizes

.parent {
  font-size: 20px;
}
.child {
  font-size: 1.2em;
}
.grandchild {
  font-size: 1.2em;
}

When the elements are nested in that order, the child computes to 24px and the grandchild computes to 28.8px.

Important Consideration

Inherited font sizes affect relative units used in descendant elements. Check nested components when unexpected text sizes appear.

Responsive Font Sizes

Responsive typography adjusts text sizes to suit different viewport widths and layouts.

Using Media Queries

.page-title {
  font-size: 2rem;
}
@media (min-width: 768px) {
  .page-title {
    font-size: 3rem;
  }
}

The heading uses a smaller size on narrow screens and a larger size when the viewport is at least 768px wide.

Using clamp()

.page-title {
  font-size: clamp(2rem, 4vw, 4rem);
}

The browser selects a value between 2rem and 4rem, using 4vw as the preferred value.

Combining Relative and Viewport Units

.responsive-heading {
  font-size: clamp(1.75rem, 1rem + 2vw, 3.5rem);
}

Combining rem and vw allows the preferred size to respond to both the root font size and the viewport width.

Using calc()

.responsive-text {
  font-size: calc(1rem + 0.5vw);
}

Important Consideration

A font size based only on viewport units may not respond adequately to user text-size preferences. Combine viewport units with relative font units and test text resizing and zoom behavior.

Font Size and Line Height

The font-size property determines the size of the font, while line-height controls the height of each line box.

Font Size with Line Height

p {
  font-size: 18px;
  line-height: 1.6;
}

The used line height is 28.8px because the unitless multiplier is applied to the element's font size.

Larger Heading

h1 {
  font-size: 3rem;
  line-height: 1.2;
}

Using a Fixed Line Height

p {
  font-size: 18px;
  line-height: 28px;
}

A fixed line height does not automatically scale when the font size changes.

Unitless Line Height

body {
  font-size: 1rem;
  line-height: 1.5;
}

A unitless line height is often useful for inherited typography because descendants calculate their line height using their own font sizes.

Important Consideration

Increasing font size without allowing sufficient line height can make multiline text difficult to read. Test headings, paragraphs, and narrow layouts together.

Font Size and Accessibility

Text should remain readable when users zoom the page, increase their preferred font size, or use smaller screens.

Use Relative Font Sizes

body {
  font-size: 1rem;
}
h1 {
  font-size: 2.5rem;
}

Relative units can help typography respond to changes in the root font size.

Avoid Unnecessarily Small Text

.supporting-text {
  font-size: 0.875rem;
}

Smaller text may be appropriate for secondary information, but it must remain readable at typical viewing distances and under user-selected text scaling.

Allow Text to Wrap

.content {
  max-width: 65ch;
  overflow-wrap: break-word;
}

Flexible containers and appropriate wrapping help prevent text from being clipped when its size increases.

Avoid Fixed-Height Text Containers

.message {
  min-height: 80px;
  padding: 1rem;
}

Using min-height instead of a fixed height allows the container to grow when text wraps onto additional lines.

Important Consideration

WCAG 2.2 includes requirements for text resizing and reflow. Test pages at increased zoom and text sizes to ensure content remains readable and functional without unnecessary loss of information.

font-size Example

This example demonstrates several font-size values, including pixels, rem, em, percentages, keywords, and responsive sizing.

HTML Example

<div class="font-size-demo">
  <p class="demo-label">16px</p>
  <p class="size-pixels">CSS controls text size.</p>
  <p class="demo-label">1.5rem</p>
  <p class="size-rem">CSS controls text size.</p>
  <p class="demo-label">1.25em</p>
  <p class="size-em">CSS controls text size.</p>
  <p class="demo-label">150%</p>
  <p class="size-percent">CSS controls text size.</p>
  <p class="demo-label">large</p>
  <p class="size-keyword">CSS controls text size.</p>
  <p class="demo-label">clamp()</p>
  <p class="size-responsive">Responsive Text</p>
</div>

CSS Example

.font-size-demo {
  font-size: 16px;
}
.font-size-demo p {
  margin: 0 0 12px;
  line-height: 1.4;
}
.font-size-demo .demo-label {
  font: 600 13px/1.4 Arial, sans-serif;
}
.size-pixels {
  font-size: 16px;
}
.size-rem {
  font-size: 1.5rem;
}
.size-em {
  font-size: 1.25em;
}
.size-percent {
  font-size: 150%;
}
.size-keyword {
  font-size: large;
}
.size-responsive {
  font-size: clamp(1.5rem, 2vw + 1rem, 2.5rem);
}
Browser Result

16px

CSS controls text size.

1.5rem

CSS controls text size.

1.25em

CSS controls text size.

150%

CSS controls text size.

large

CSS controls text size.

clamp()

Responsive Text

The pixel example uses a specified CSS length. The rem example uses the root font size, while the em and percentage examples use the parent font size.

The keyword example follows the browser's font-size scale. The responsive example adjusts its size within the minimum and maximum values defined by clamp().

Common Mistakes

Incorrect font sizes often result from missing units, unexpected inheritance, or misunderstanding relative measurements.

Omitting the Unit

p {
  font-size: 18;
}

This declaration is invalid because nonzero numeric font sizes require an appropriate unit.

Using a Negative Value

p {
  font-size: -16px;
}

Negative font sizes are invalid.

Confusing em and rem

.parent {
  font-size: 24px;
}
.child {
  font-size: 1.5em;
}

The child's font size is 36px, not necessarily 24px. The em unit uses the parent's computed font size.

Unexpected Nested Scaling

.nested {
  font-size: 1.2em;
}

If multiple nested elements match this rule, each level increases the font size relative to its parent.

Using Viewport Units Without Limits

h1 {
  font-size: 6vw;
}

This can produce text that is too small on narrow screens or excessively large on wide screens. Use clamp() or responsive breakpoints when appropriate.

Setting Font Size to Zero

p {
  font-size: 0;
}

Although valid, this can make text effectively invisible. Do not use it to hide meaningful content.

Assuming All Fonts Look the Same Size

Different font families can have different x-heights and proportions, even when they use the same font-size value.

Ignoring Text Resizing

Fixed-height containers, clipped overflow, and rigid layouts can cause problems when users enlarge text.

Important Considerations

Keep the following points in mind when using the font-size property:

  • Purpose: Sets the size of the font's em square.
  • Initial value: medium.
  • Inheritance: The property is inherited.
  • Applies to: All elements and text.
  • Absolute lengths: Units include px, pt, and other CSS length units.
  • Relative lengths: Units include em, rem, and viewport-relative measurements.
  • em: Uses the parent's computed font size when calculating font-size.
  • rem: Uses the root element's font size.
  • Percentages: Are calculated relative to the parent's computed font size.
  • Absolute-size keywords: Provide predefined size steps.
  • Relative-size keywords: Include larger and smaller.
  • math keyword: Provides specialized scaling for mathematical layout.
  • Negative values: Are invalid.
  • Zero: Is valid but generally makes text effectively invisible.
  • Nested elements: Relative font sizes can compound through inheritance.
  • Responsive typography: Media queries and CSS math functions can adjust font sizes.
  • clamp(): Can establish minimum, preferred, and maximum font sizes.
  • Line height: Should be considered alongside font size for readable multiline text.
  • Accessibility: Text should remain readable when resized or zoomed.
  • Font differences: Fonts with the same computed size can appear visually different.
  • Animation: Font sizes can interpolate between compatible computed values.
  • Browser support: The property is supported across all modern browsers.

For general website typography, relative units such as rem provide a consistent foundation. Use em for component-relative scaling and clamp() when fluid typography is appropriate.

Browser Support

The CSS font-size property is Widely available and supported in all modern browsers, including Chrome, Edge, Firefox, and Safari.

Basic values such as pixels, percentages, em, rem, and traditional font-size keywords have long-standing browser support. Newer units, specialized keywords, and CSS math functions may have different compatibility requirements.

Supported Example

p {
  font-size: 1.125rem;
}

Responsive Example

h1 {
  font-size: clamp(2rem, 4vw, 4rem);
}

Fallback Example

h1 {
  font-size: 2rem;
  font-size: clamp(2rem, 4vw, 4rem);
}

Browsers that do not support the clamp() expression retain the earlier valid declaration.

For current compatibility information, visit Can I Use? and search for font-size.

Specifications

The font-size property is defined in the CSS Fonts Module, which describes font sizing, absolute and relative keywords, and font-size computation.

CSS Fonts Module Level 4: font-size Property

CSS length units and their relationships are defined in the CSS Values and Units Module.

CSS Values and Units Module Level 4: Lengths

For additional explanations and examples, see MDN Web Docs.

MDN Web Docs: font-size