CSS Reference ยท Typography
The font-size property
Reference for the CSS font-size property: set text size with px, rem, em, percentages, keywords and clamp(), with accessibility advice and live examples.
The font-size property sets the size of text. It also sets the meaning of the em unit for the element: 1em equals the element's own font size, so many other measurements (padding, margins, widths) can scale with the text.
Most browsers default to a font size of 16px for body text, but users can change that in their settings. Good CSS respects that choice by using relative units such as rem.
CSS
html { font-size: 100%; } h1 { font-size: 2.5rem; } small { font-size: 0.875em; }
Try it
Syntax
CSS
font-size: <absolute-size> | <relative-size> | <length-percentage [0,โ]> | math; /* <absolute-size> = xx-small | x-small | small | medium | large | x-large | xx-large | xxx-large */ /* <relative-size> = larger | smaller */
- Initial value:
medium - Applies to: all elements and text, plus
::first-letter,::first-lineand::placeholder - Inherited: yes (the computed size is inherited)
- Animatable: yes, as a length
- Computed value: an absolute length
Values
| Value | Description |
|---|---|
<length> |
px, rem, em, vw, ch and so on. em here means the parent's font size. |
<percentage> |
A percentage of the parent's font size. |
xx-small to xxx-large |
Keyword sizes from a table that scales with the user's default size. medium is the default. |
larger, smaller |
One step above or below the parent's size. |
math |
A size suited to nested math content. Rarely used. |
Usage notes
- Prefer
remfor text. Aremvalue follows the root font size, so a user who raised their default text size gets bigger text. Fixedpxsizes still scale with browser zoom, but ignore a changed default size. emcompounds. Becauseemand%refer to the parent, nested elements multiply: a list inside a list at0.9emshrinks twice. Useremto avoid this.- Do not shrink the root.
html { font-size: 62.5% }is a common trick, but it makes every unset size small.100%is safer. - Fluid sizes.
clamp(min, preferred, max)gives text that grows with the viewport but stays in a safe range. Add arempart to the preferred value (for example1rem + 1vw) so zoom still works. - Readable minimums. Body text below about
16pxhurts readability, particularly on touch screens. - Mobile text inflation. Some mobile browsers boost small text in wide layouts; use viewport meta and fluid layouts rather than fighting it.
- Browser minimums. Browsers may enforce a minimum size in settings, which overrides very small values.
Browser support
Supported in every browser.
Related
fontโ the shorthandfont-familyandfont-size-adjustline-heightremandemunits- Lesson: Fluid Typography, CSS Units