Font

This page lists the CSS properties to customize an element's font.

Basic Concepts

Font Display Timeline

Font display timeline begins when the browser attempts to use a given font. There are three periods:

  • font block period if the font is not loaded, temporarily render using an invisible text. (If the font succesfully loads, then use it normally.)
  • font swap period if the font is not loaded, temporarily render using a fallback font. (If the font succesfully loads, then use it normally.)
  • font failure period if the font is not loaded, treat it as a failed load, and render using a fallback font.

Font At-Rules

@font-face

Defines a custom font.

Required properties:

  • font-familyName of the font
  • srcSource to the font data. Use local(...) to refer to a font name on the client's computer, or use url(...) to load from a URL.

Optional properties:

  • font-displaySets how a font is displayed in relation to its availability (is loading or has done loading). Specify one of: auto (let the browser decide); block (short block period, infinite swap period); swap (no block period, infinite swap period); fallback (super short block period, short swap period); optional (super short block period, no swap period).
  • unicode-rangeSets the specific range of characters to be used from the font. Specify (in hexadecimal): a single codepoint (e.g. U+26), a codepoint range (e.g. U+0025-00FF), or a wildcard range (e.g. U+4??).
  • font-variantSee below.
  • font-weightSee below.
  • font-stretchSee below.
  • font-feature-settingsSee below.
  • font-variation-settingsSee below.
@font-face {
  font-family: examplefont;
  src: local(Example Font),
    url('examplefont.woff') format('woff'),
    url('examplefont.otf') format('opentype');
}

Font Properties

font

Shorthand for the following properties. font-size and font-family are required.

  • font-style
  • font-variant
  • font-weight
  • font-stretch
  • font-size
  • line-height
  • font-family
| LIVE PREVIEW

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

font-style

Selects a normal/italicized font from a font family.

Specify: normal, italic, oblique, or oblique <angle> (e.g. oblique 14deg).

According to the spec, “Italic forms are generally cursive in nature while oblique faces are typically sloped versions of the regular face” (CSS-Tricks).

Normal vs italic vs oblique version of Palatino Linotype font.
Normal vs italic vs oblique version of Palatino Linotype font. (Source: Microsoft Docs)

font-variant

Controls the usage of alternate glyphs (e.g. ligatures, traditional/simplified Chinese characters). Shorthand for the following properties:

  • font-variant-caps
  • font-variant-east-asian
  • font-variant-ligatures
  • font-variant-numeric
p {
  /* Examples */
  font-variant: normal;
  font-variant: small-caps slashed-zero;
  font-variant: common-ligatures tabular-nums;
}

font-weight

Sets the boldness of the font. Specify:

  • any number between 1 and 1000E.g. 500
  • normalAlias of 400
  • boldAlias 700
  • lighterOne relative weight lighter than the parent
  • bolderOne relative weight bolder than the parent

font-stretch

Selects a normal, condensed, or expanded version of a font. Specify:

  • normal
  • a percentagebetween 50% and 200%
  • a "condensed" familysemi-condensed, condensed, extra-condensed, or ultra-condensed
  • an "expanded" familysemi-expanded, expanded, extra-expanded, or ultra-expanded

font-size

Sets the size of the font. Specify:

  • a lengthE.g. 16pt or 16px
  • a percentageE.g. 75% (relative to its parent)
  • an absolute-size keywordxx-small, x-small, small, medium, large, x-large, xx-large, or xxx-large (based on the user's default font size; the final value is calculated by the browser)
  • an relative-size keywordlarger, smaller (relative to its parent)

line-height

Sets the height of the line box (distance between the lines of the text). Specify:

  • normaldepends on the browser
  • a number(e.g. 1.5) multiple of the element's font size
  • a percentage(e.g. 150%) relative to the element's font size
  • a length(e.g. 20px)

font-family

Sets a prioritized list of font family names (and/or generic family names) to use.

A font family name would be something like Arial or Times New Roman, whereas a generic family name is one of: serif, sans-serif, cursive, fantasy, or monospace.

font-kerning

Enables or disables kerning. Specify auto, normal, or none.

Kerning disabled vs enabled.
Kerning disabled vs enabled. (Source: MDN)

Font Settings Properties

font-feature-settings

Controls advanced typographic features in OpenType fonts.

p {
  font-feature-settings: 'smcp';
  font-feature-settings: 'smcp' on;
  font-feature-settings: 'swsh' 2;
  font-feature-settings: 'smcp', 'swsh' 2;
}

font-variation-settings

Sets low-level properties of a variable font.

p {
  font-variation-settings: 'wght' 850;
  font-variation-settings: 'wght' 850, 'slant' 0;
}

Font Variant Properties

font-variant-caps

Controls the use of alternate glyphs for capital letters.

Common values are normal and small-caps.

| LIVE PREVIEW
font-variant-caps Preview
normal Lorem ipsum
small-caps Lorem ipsum
all-small-caps Lorem ipsum
petite-caps Lorem ipsum
all-petite-caps Lorem ipsum
unicase Lorem ipsum
titling-caps Lorem ipsum

font-variant-east-asian

Controls the use of alternate glyphs for East Asian scripts (e.g. half-width or full-width, traditional or simplified). Specify:

  • normalDon't use alternate glyphs
  • rubyUse of special glyphs for rendering ruby characters (usually slightly bolder for increased contrast because ruby characters are usually rendered small)
  • a variant valuejis78, jis83, jis90, jis04, simplified, or traditional. The resulting kanji might slightly be altered: e.g. 麹町 (normal) vs 麹町 (jis78); 大学 (normal) vs 大学 (traditional).
  • a width valueproportional-width ("half-width") or full-width

font-variant-ligatures

Controls which ligatures are used. Common values are normal and none (ligature availability depends on the font). See here for other font-variant-ligatures values.

| LIVE PREVIEW
font-variant-ligatures Preview (note the "ffi" and "ffl")
normal Difficult waffles
none Difficult waffles

font-variant-numeric

Controls the usage of alternate glyphs for numbers, fractions, and ordinal markers. (Alternate glyphs availability depends on the font.)

| LIVE PREVIEW
font-variant-numeric Preview
normal 0.31415926 1st 2nd 3rd 1/2 3/4
ordinal 0.31415926 1st 2nd 3rd 1/2 3/4
slashed-zero 0.31415926 1st 2nd 3rd 1/2 3/4
numeric-figure-values
lining-nums 0.31415926 1st 2nd 3rd 1/2 3/4
oldstyle-nums 0.31415926 1st 2nd 3rd 1/2 3/4
numeric-spacing-values
proportional-nums 0.31415926 1st 2nd 3rd 1/2 3/4
tabular-nums 0.31415926 1st 2nd 3rd 1/2 3/4
numeric-fraction-values
diagonal-fractions 0.31415926 1st 2nd 3rd 1/2 3/4
stacked-fractions 0.31415926 1st 2nd 3rd 1/2 3/4
*) This example uses the Source Sans Pro OpenType font, developed by Adobe and used here under the terms of the SIL Open Font License, Version 1.1: http://scripts.sil.org/cms/scripts/page.php?item_id=OFL_web

Assets

References