docs.rodeo

MDN Web Docs mirror

@font-face

{{CSSRef}} 

The @font-face CSS at-rule specifies a custom font with which to display text; the font can be loaded from either a remote server or a locally-installed font on the user’s own computer.

Syntax

@font-face {
  font-family: "Trickster";
  src:
    local("Trickster"),
    url("trickster-COLRv1.otf") format("opentype") tech(color-COLRv1),
    url("trickster-outline.otf") format("opentype"),
    url("trickster-outline.woff") format("woff");
}

Descriptors

Description

It’s common to use both url() and local() together, so that the user’s installed copy of the font is used if available, falling back to downloading a copy of the font if it’s not found on the user’s device.

If the local() function is provided, specifying a font name to look for on the user’s device, and if the {{Glossary("user agent")}}  finds a match, that local font is used. Otherwise, the font resource specified using the url() function is downloaded and used.

Browsers attempt to load resources in their list declaration order, so usually local() should be written before url(). Both functions are optional, so a rule block containing only one or more local() without url() is possible. If more specific fonts with format() or tech() values are desired, these should be listed before versions that don’t have these values, as the less specific variant would otherwise be tried and used first.

By allowing authors to provide their own fonts, @font-face makes it possible to design content without being limited to the so-called “web-safe” fonts (that is, the fonts that are so common that they’re considered to be universally available). The ability to specify the name of a locally-installed font to look for and use makes it possible to customize the font beyond the basics while making it possible to do so without relying on an internet connection.

[!NOTE] Fallback strategies for loading fonts on older browsers are described in the src descriptor page.

The @font-face at-rule may be used not only at the top level of a CSS, but also inside any CSS conditional-group at-rule.

Font MIME Types

Format MIME type
TrueType font/ttf
OpenType font/otf
Web Open Font Format font/woff
Web Open Font Format 2 font/woff2

Notes

Formal syntax

{{csssyntax}} 

Examples

Specifying a downloadable font

This example specifies a downloadable font to use, applying it to the entire body of the document:

<body>
  This is Bitstream Vera Serif Bold.
</body>
@font-face {
  font-family: "Bitstream Vera Serif Bold";
  src: url("https://mdn.github.io/shared-assets/fonts/VeraSeBd.ttf");
}

body {
  font-family: "Bitstream Vera Serif Bold", serif;
}

{{EmbedLiveSample("web-font-example", "", "100px")}} 

Specifying local font alternatives

In this example, the user’s local copy of “Helvetica Neue Bold” is used; if the user does not have that font installed (both the full font name and the Postscript name are tried), then the downloadable font named “MgOpenModernaBold.ttf” is used instead:

@font-face {
  font-family: "MyHelvetica";
  src:
    local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"),
    url("MgOpenModernaBold.ttf");
  font-weight: bold;
}

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN