font-size

The font-size CSS property sets the size of the font. Changing the font size also updates the sizes of the font size-relative <length> units, such as em, ex, and so forth.

Syntax

/* <absolute-size> values */
font-size: xx-small;
font-size: x-small;
font-size: small;
font-size: medium;
font-size: large;
font-size: x-large;
font-size: xx-large;
font-size: xxx-large;

/* <relative-size> values */
font-size: smaller;
font-size: larger;

/* <length> values */
font-size: 12px;
font-size: 0.8em;

/* <percentage> values */
font-size: 80%;

/* Global values */
font-size: inherit;
font-size: initial;
font-size: unset;

The font-size property is specified in one of the following ways:

  • As one of the absolute-size or relative-size keywords
  • As a <length> or a <percentage>, relative to the parent element's font size

Values

xx-small, x-small, small, medium, large, x-large, xx-large, xxx-large
Absolute-size keywords, based on the user's default font size (which is medium).
larger, smaller
Relative-size keywords. The font will be larger or smaller relative to the parent element's font size, roughly by the ratio used to separate the absolute-size keywords above.
<length>

A positive <length> value. For most font-relative units (such as em and ex), the font size is relative to the parent element's font size.

For font-relative units that are root-based (such as rem), the font size is relative to the size of the font used by the <html> (root) element.

<percentage>

A positive <percentage> value, relative to the parent element's font size.

Note: To maximize accessibility, it is generally best to use values that are relative to the user's default font size.

Description

There are several ways to specify the font size, including with keywords or numerical values for pixels or ems. Choose the appropriate method based on the needs for the particular web page.

Keywords

Keywords are a good way to set the size of fonts on the web. By setting a keyword font size on the <body> element, you can set relative font-sizing everywhere else on the page, giving you the ability to easily scale the font up or down on the entire page accordingly.

Pixels

Setting the font size in pixel values (px) is a good choice when you need pixel accuracy. A px value is static. This is an OS-independent and cross-browser way of literally telling the browsers to render the letters at exactly the number of pixels in height that you specified. The results may vary slightly across browsers, as they may use different algorithms to achieve a similar effect.

Font sizing settings can also be used in combination. For example, if a parent element is set to 16px and its child element is set to larger, the child element displays larger than the parent element in the page.

Note: Defining font sizes in px is not accessible, because the user cannot change the font size in some browsers. For example, users with limited vision may wish to set the font size much larger than the size chosen by a web designer. Avoid using them for font sizes if you wish to create an inclusive design.

Ems

Another way of setting the font size is with em values. The size of an em value is dynamic. When defining the font-size property, an em is equal to the font size of the element on which the em is used. If you haven't set the font size anywhere on the page, then it is the browser default, which is often 16px. So, by default 1em = 16px, and 2em = 32px. If you set a font-size of 20px on the body element, then 1em = 20px and 2em = 40px. Note that the value 2 is essentially a multiplier of the current em size.

In order to calculate the em equivalent for any pixel value required, you can use this formula:

em = desired element pixel value / parent element font-size in pixels

For example, suppose the font-size of the body of the page is set to 16px. If the font-size you want is 12px, then you should specify 0.75em (because 12/16 = 0.75). Similarly, if you want a font size of 10px, then specify 0.625em (10/16 = 0.625); for 22px, specify 1.375em (22/16).

The em is a very useful unit in CSS, since it automatically adapts its length relative to the font that the reader chooses to use.

em and ex units on the font-size property are relative to the parent element's font size (unlike all other properties, where they're relative to the font size on the element). This means em units and percentages do the same thing for font-size.

One important fact to keep in mind: em values compound. Take the following HTML and CSS:

html {
  font-size: 62.5%; /* font-size 1em = 10px on default browser settings */
}
span {
  font-size: 1.6em;
}
<div>
<span>Outer <span>inner</span> outer</span>
</div>

The result is:

Assuming that the browser's default font-size is 16px, the words โ€œouterโ€ would be rendered at 16px, but the word โ€œinnerโ€ would be rendered at 25.6px. This is because the inner <span>'s font-size is 1.6em which is relative to its parent's font-size, which is in turn relative to its parent's font-size. This is often called compounding.

Rems

rem values were invented in order to sidestep the compounding problem. rem values are relative to the root html element, not the parent element. In other words, it lets you specify a font size in a relative fashion without being affected by the size of the parent, thereby eliminating compounding.

The CSS below is nearly identical to the previous example. The only exception is that the unit has been changed to rem.

html {
  font-size: 62.5%; /* font-size 1em = 10px on default browser settings */
}
span {
  font-size: 1.6rem;
}

Then we apply this CSS to the same HTML, which looks like this:

<span>Outer <span>inner</span> outer</span>

In this example, the words โ€œouter inner outerโ€ are all displayed at 16px (assuming that the browser's font-size has been left at the default value of 10px).

Formal definition

Initial valuemedium
Applies toall elements. It also applies to ::first-letter and ::first-line.
Inheritedyes
Percentagesrefer to the parent element's font size
Computed valueas specified, but with relative lengths converted into absolute lengths
Animation typea length

Formal syntax

<absolute-size> | <relative-size> | <length-percentage>

where
<absolute-size> = xx-small | x-small | small | medium | large | x-large | xx-large | xxx-large
<relative-size> = larger | smaller
<length-percentage> = <length> | <percentage>

Examples

Setting font sizes

CSS

.small {
  font-size: xx-small;
}
.larger {
  font-size: larger;
}
.point {
  font-size: 24pt;
}
.percent {
  font-size: 200%;
}

HTML

<h1 class="small">Small H1</h1>
<h1 class="larger">Larger H1</h1>
<h1 class="point">24 point H1</h1>
<h1 class="percent">200% H1</h1>

Result

Specifications

Specification Status Comment
CSS Fonts Module Level 4
The definition of 'font-size' in that specification.
Working Draft Adds xxx-large keyword.
CSS Fonts Module Level 3
The definition of 'font-size' in that specification.
Candidate Recommendation No change.
CSS Level 2 (Revision 1)
The definition of 'font-size' in that specification.
Recommendation No change.
CSS Level 1
The definition of 'font-size' in that specification.
Recommendation Initial definition.

Browser compatibility

DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
font-sizeChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 5.5Opera Full support 7Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Firefox Android Full support 4Opera Android Full support 10.1Safari iOS Full support 1Samsung Internet Android Full support 1.0
rem valuesChrome Full support 31Edge Full support 12Firefox Full support 31
Notes
Full support 31
Notes
Notes Before Firefox 57, animations using em units are not affected by changes to the font-size of the animated element's parent (bug 1254424).
Notes Before Firefox 57, some language settings' inherited font-size is smaller than expected (bug 1391341).
IE Full support 11
Full support 11
No support 9 — 10
Opera Full support 28Safari Full support 7WebView Android Full support 4.1Chrome Android Full support 42Firefox Android Full support 31Opera Android Full support 28Safari iOS Full support 7Samsung Internet Android Full support 4.0
xxx-large keywordChrome Full support 79Edge Full support 79Firefox Full support 70IE No support NoOpera No support NoSafari No support NoWebView Android Full support 79Chrome Android Full support 79Firefox Android No support NoOpera Android No support NoSafari iOS No support NoSamsung Internet Android No support No

Legend

Full support
Full support
No support
No support
See implementation notes.
See implementation notes.