GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

`@font-face` CSS at-rule

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.

The **`@font-face`** [CSS](/docs/Web/CSS) [at-rule](/docs/Web/CSS/Guides/Syntax/At-rules) 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

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

### Descriptors

- ascent-override
- : Defines the ascent metric for the font.
- descent-override
- : Defines the descent metric for the font.
- font-display
- : Determines how a font face is displayed based on whether and when it is downloaded and ready to use.
- font-family
- : Specifies a name that will be used as the font face value for font properties. A `font-family` name is required for the `@font-face` rule to be valid.
- font-stretch
- : A font-stretch value. Accepts two values to specify a range that is supported by a font face, for example `font-stretch: 50% 200%;`
- font-style
- : A font-style value. Accepts two values to specify a range that is supported by a font face, for example `font-style: oblique 20deg 50deg;`
- font-weight
- : A font-weight value. Accepts two values to specify a range that is supported by a font face, for example `font-weight: 100 400;`
- font-feature-settings
- : Allows control over advanced typographic features in OpenType fonts.
- font-variation-settings
- : Allows 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.
- font-width
- : Allows authors to specify a normal, condensed, or expanded face for the specified fonts.
- line-gap-override
- : Defines the line gap metric for the font.
- size-adjust
- : Defines 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.
- src
- : Specifies references to font resources including hints about the font format and technology. A `src` is required for the `@font-face` rule to be valid.
- unicode-range
- : The range of Unicode code points to be used from the font.

## 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 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 [`src` descriptor page](/docs/Web/CSS/Reference/At-rules/@font-face/src#specifying_fallbacks_for_older_browsers).

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](/docs/Web/CSS/Guides/Conditional_rules#at-rules_and_descriptors).

### 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), unless [HTTP access controls](/docs/Web/HTTP/Guides/CORS) are used to relax this restriction.
- `@font-face` cannot be declared within a CSS selector. For example, the following will not work:

```css example-bad
.className {
@font-face {
font-family: "MyHelvetica";
src:
local("Helvetica Neue Bold"), local("HelveticaNeue-Bold"),
url("MgOpenModernaBold.woff2");
font-weight: bold;
}
}
```

## Formal syntax

## Examples

### Specifying a downloadable font

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

```html live-sample___web-font-example
<body>
This is Bitstream Vera Serif Bold.
</body>
```

```css live-sample___web-font-example
@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;
}
```

### 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.woff2" is used instead:

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

## Specifications

## Browser compatibility

## See also

- [About WOFF](/docs/Web/CSS/Guides/Fonts/WOFF)
- [Transfonter webfont generator](https://transfonter.org/)
- [Beautiful fonts with @font-face](https://hacks.mozilla.org/2009/06/beautiful-fonts-with-font-face/)
- [Font Library](https://fontlibrary.org/)

source: MDN Web Docs mirror · page

Cite this

"`@font-face` CSS at-rule". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@font-face

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·