liveBook · Manning

@font-face

As explained in Web Design Playground, Second Edition

The CSS @font-face at-rule defines a font resource and the information a browser needs to load it.

The CSS @font-face at-rule lets a page use web fonts: font files hosted online and loaded by the browser. It reduces uncertainty caused by relying on fonts installed on each visitor’s computer. section 18.2

A basic @font-face rule identifies the font and where the browser can find it:

  • font-family gives the downloaded font a name, such as 'Bree Serif'. section 18.4
  • src lists font files with their URLs and formats; multiple entries can provide WOFF2 and WOFF alternatives. section 18.4
  • A normal selector then uses the declared name, such as ul { font-family: 'Bree Serif'; }. section 18.4
@font-face {
    font-family: 'Bree Serif';     #1
    src: url('/fonts/breeserif.woff2') format('woff2'),
         url('/fonts/breeserif.woff') format('woff');
}
ul {
    font-family: 'Bree Serif';     #1
}

See a complete rule that declares Bree Serif, supplies WOFF2 and WOFF files, and applies the font to a list. section 18.4

Hosting a font yourself

Place the font files on the same server as the page, then point src at their paths. Self-hosting avoids dependence on a remote server but adds complexity: verify that the font license permits web use and obtain a suitable web font format. Modern browsers support WOFF2; WOFF can serve as an additional format. section 18.4

How Web Design Playground, Second Edition covers it

How deep it goes
Explains the rule’s purpose, descriptors, hosting choices, licensing concern, and font formats through short working examples.
Examples
Uses Bree Serif with WOFF2 and WOFF files, and practices a Google Fonts Merriweather setup with a generic fallback.
What it assumes
Builds from basic font-stack usage and introduces web-font hosting, licensing, file paths, and formats.
What you can do after
Apply it via CSS by declaring a hosted font, supplying file formats, and assigning the font to page elements.
How it connects
Connects web fonts with font stacks, CSS selectors, remote font hosting, self-hosting, font licensing, WOFF2, and WOFF.

Also explained in

Compare how 4 books explain @font-face →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.