files/en-us/web/css/reference/at-rules/@font-face/index.md
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.
@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.woff2") format("woff2");
}
font-family name is required for the @font-face rule to be valid.font-stretch: 50% 200%;font-style: oblique 20deg 50deg;font-weight: 100 400;src is required for the @font-face rule to be valid.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.
For web delivery, it's generally best to serve fonts in WOFF2 format, because it compresses fonts more efficiently than older formats like WOFF or OpenType, reducing file size and improving load times. WOFF2 is also well supported in modern browsers, making it a safe default choice for most websites.
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
srcdescriptor 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.
| Format | MIME type |
|---|---|
| TrueType | font/ttf |
| OpenType | font/otf |
| Web Open Font Format | font/woff |
| Web Open Font Format 2 | font/woff2 |
Web fonts are subject to the same domain restriction (font files must be on the same domain as the page using them), unless HTTP access controls are used to relax this restriction.
@font-face cannot be declared within a CSS selector. For example, the following will not work:
.className {
@font-face {
font-family: "MyHelvetica";
src:
local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"),
url("MgOpenModernaBold.woff2");
font-weight: bold;
}
}
{{csssyntax}}
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/FiraSans-Regular.woff2");
}
body {
font-family: "Bitstream Vera Serif Bold", serif;
}
{{EmbedLiveSample("web-font-example", "", "100px")}}
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.woff2" is used instead:
@font-face {
font-family: "MyHelvetica";
src:
local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"),
url("MgOpenModernaBold.woff2");
font-weight: bold;
}
{{Specifications}}
{{Compat}}