CSS typography

Quick reference for CSS font families, font-size, line-height, the font shorthand, and the limits of font loading on this site.

リファレンスページの本文は現在英語です。

This page is a typography quick reference: font-family, font-size, weight, style, line-height, and the font shorthand. It does not repeat the box model, Flexbox, or Grid pages, and it does not replace the CSS tutorial.

The font shorthand is easy to overuse. A valid font value must include font-size and font-family. Any optional longhand you omit — font-style, font-variant, font-weight, font-stretch, and line-height — is reset to its initial value. A rule that only intended to change the size can therefore drop a bold or italic setting declared earlier.

Font loading is described only in general terms. Examples use generic families such as sans-serif, serif, monospace, and system-ui. They do not use url() font files, @import, or third-party font services, and this page does not fetch typefaces over the network.

Examples are complete HTML and CSS fragments parsed with PostCSS. They were not rendered in a browser, and font fallback was not measured here.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Font family, size, and the font shorthand

font-family is a comma-separated stack. The browser uses the first installed or generic family it can satisfy. Quote family names that contain spaces or keywords. End the stack with a generic family so a missing file still yields a typographic role, not an inherited surprise.

On these pages the reasonable font-loading range is a generic stack, or a self-hosted @font-face that you already control. @font-face descriptors such as font-display (auto, block, swap, fallback, optional) affect blank text versus fallback text during load. That behaviour is a compatibility topic; it is not demonstrated with a network request here.

Syntax or propertyInitial / defaultInheritedApplies toNotes
font-familydepends on the user agentyesall elementsA stack of family names and generics: serif, sans-serif, monospace, cursive, fantasy, and the newer system-ui. Common values: sans-serif, serif, "Source Serif", system-ui, sans-serif. system-ui is widely available in current browsers; quote multi-word names.
font-sizemediumyesall elementsCommon values: rem, em, px, keywords (small, medium, large), or percentages relative to the parent font-size. rem scales from the root; em scales from the parent. Computed sizes are inherited, so nested em can compound.
font-weightnormal (400)yesall elementsCommon values: normal, bold, or 100–900 (and relative bolder / lighter). A family may not have every weight; the user agent synthesises or substitutes. Numeric weights are the usual choice in component CSS.
font-stylenormalyesall elementsCommon values: normal, italic, oblique. italic uses an italic face when one exists; otherwise the engine may synthesise a slant.
font-variantnormalyesall elementsIn the font shorthand, only the CSS 2.1 values normal and small-caps apply. OpenType variants use the longhands such as font-variant-ligatures, not the shorthand.
fontsee longhandsyesall elementsShorthand for style, variant, weight, stretch, size / line-height, and family. font-size and font-family are required or the declaration is invalid. Omitted optionals reset to their initial values. Do not use it as a shortcut for font-size alone.

The font shorthand resets omitted longhands

font-style: italic;
font-weight: 700;
font: 1rem/1.4 serif;

A later font shorthand that sets only size, line-height, and family resets style and weight to normal.

  • The example uses generic families only. No font file is requested.
html
<p class="lead">Italic bold lead, then a shorthand.</p>
<p class="lead short">The shorthand resets style and weight.</p>
<style>
.lead {
  font-style: italic;
  font-weight: 700;
  font-size: 1.25rem;
  line-height: 1.5;
  font-family: sans-serif;
}
.lead.short { font: 1rem/1.4 serif; }
</style>

What this markup means

The first paragraph keeps italic, weight 700, 1.25rem, and a unitless 1.5 line-height. The second paragraph also has those longhands, but font: 1rem/1.4 serif then sets size, line-height, and family and resets font-style and font-weight to their initial normal values. The rule is valid because it includes both a size and a family.

Related: Text, CSS tutorial: selectors

MDN documentation → · CSS specification →

Unitless line-height and inheritance

The initial line-height is normal, which is a font-dependent used value and is not a number you should treat as a fixed ratio. Prefer a unitless number such as 1.5 when you want descendants to scale with their own font-size.

A unitless line-height is inherited as that number. Each descendant multiplies its own font-size by the number. A length (24px) or a percentage (150%) is computed against the element that declared it, and that computed length is what descendants inherit. A child with a larger font-size then keeps the parent’s line box height, which often looks tight.

Syntax or propertyInitial / defaultInheritedApplies toNotes
line-heightnormalyesall elementsCommon values: normal, a unitless number (1.4, 1.5), a length, or a percentage of the element’s own font-size. Unitless numbers inherit as factors. Lengths and percentages inherit as computed lengths. The font shorthand can also set size/line-height together, and omitting line-height there resets it to normal.

Unitless line-height inherits as a factor

line-height: 1.5;
line-height: 24px;

A unitless 1.5 scales with a child font-size. A 24px line-height is inherited as 24px and does not scale.

  • The used line boxes were not measured in a browser. The distinction follows the inheritance rules for the two value types.
html
<div class="factor">
  <p class="child">Unitless 1.5 on the parent, larger type on the child.</p>
</div>
<div class="fixed">
  <p class="child">24px line-height on the parent, larger type on the child.</p>
</div>
<style>
.factor, .fixed { font-family: sans-serif; font-size: 16px; }
.factor { line-height: 1.5; }
.fixed { line-height: 24px; }
.child { font-size: 32px; margin: 0; }
</style>

What this markup means

Both parents start at 16px. The first inherits line-height: 1.5 as a factor, so the 32px child has a used line-height of 48px. The second inherits a computed 24px line-height, so the same 32px child still has a 24px line-height. That is why typography systems usually set unitless line-height on a parent rather than a pixel value.

Related: Text, Sizing and overflow

MDN documentation → · CSS specification →