@font-face
Baseline Widely available *
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
* Some parts of this feature may have varying levels of support.
The@font-faceCSSat-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.
In this article
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
ascent-overrideDefines the ascent metric for the font.
descent-overrideDefines the descent metric for the font.
font-displayDetermines how a font face is displayed based on whether and when it is downloaded and ready to use.
font-familySpecifies a name that will be used as the font face value for font properties. A
font-familyname is required for the@font-facerule to be valid.font-stretchA
font-stretchvalue. Accepts two values to specify a range that is supported by a font face, for examplefont-stretch: 50% 200%;font-styleA
font-stylevalue. Accepts two values to specify a range that is supported by a font face, for examplefont-style: oblique 20deg 50deg;font-weightA
font-weightvalue. Accepts two values to specify a range that is supported by a font face, for examplefont-weight: 100 400;font-feature-settingsAllows control over advanced typographic features in OpenType fonts.
font-variation-settingsAllows low-level control over OpenType or TrueType font variations, by specifying the four-letter axis names of the features to vary, along with their variation values.
line-gap-overrideDefines the line gap metric for the font.
size-adjustDefines a multiplier for glyph outlines and metrics associated with this font. This makes it easier to harmonize the designs of various fonts when rendered at the same font size.
srcSpecifies references to font resources including hints about the font format and technology. A
srcis required for the@font-facerule to be valid.unicode-rangeThe range of Unicode code points to be used from the font.
Description
It's common to use bothurl() andlocal() 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 thelocal() function is provided, specifying a font name to look for on the user's device, and if theuser agent finds a match, that local font is used. Otherwise, the font resource specified using theurl() function is downloaded and used.
Browsers attempt to load resources in their list declaration order, so usuallylocal() should be written beforeurl(). Both functions are optional, so a rule block containing only one or morelocal() withouturl() is possible.If more specific fonts withformat() ortech() values are desired, these should be listedbefore 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 thesrc descriptor page.
The@font-face at-rule may be used not only at the top level of a CSS, but also inside anyCSS 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
Web fonts are subject to the same domain restriction (font files must be on the same domain as the page using them), unlessHTTP access controls are used to relax this restriction.
@font-facecannot be declared within a CSS selector. For example, the following will not work:css.className { @font-face { font-family: "MyHelvetica"; src: local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"), url("MgOpenModernaBold.ttf"); font-weight: bold; }}
Formal syntax
@font-face =
@font-face {<declaration-list> }
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;}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
| Specification |
|---|
| CSS Fonts Module Level 4> # font-face-rule> |