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 the user's own computer. If the local()
function is provided, specifying a font name to look for on the user's computer, and the user agent finds a match, that local font is used. Otherwise, the font resource specified using the url()
function is downloaded and used.
By allowing authors to provide their own fonts, @font-face
makes it possible to design content without being limited to the so-called "safe" fonts (that is, the fonts which 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.
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.
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-face { font-family: "Open Sans"; src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"), url("/fonts/OpenSans-Regular-webfont.woff") format("woff"); }
font-display
font-family
font-stretch
font-stretch
value. Since Firefox 61 (and in other modern browsers) this also accepts two values to specify a range that is supported by a font-face, for example font-stretch: 50% 200%;
font-style
font-style
value. Since Firefox 61 (and in other modern browsers) this also accepts two values to specify a range that is supported by a font-face, for example font-style: oblique 20deg 50deg;
font-weight
font-weight
value. Since Firefox 61 (and in other modern browsers) this also accepts two values to specify a range that is supported by a font-face, for example font-weight: 100 400;
font-variant
font-variant
value.font-feature-settings
font-variation-settings
src
Specifies the resource containing the font data. This can be a URL to a remote font file location or the name of a font on the user's computer.
To provide the browser with a hint as to what format a font resource is — so it can select a suitable one — it is possible to include a format type inside a format()
function:
src: url(ideal-sans-serif.woff) format("woff"), url(basic-sans-serif.ttf) format("opentype");
The available types are: "woff"
, "woff2"
, "truetype"
, "opentype"
, "embedded-opentype"
, and "svg"
.
unicode-range
@font-face { [ font-family: <family-name>; ] || [ src: <src>; ] || [ unicode-range: <unicode-range>; ] || [ font-variant: <font-variant>; ] || [ font-feature-settings: <font-feature-settings>; ] || [ font-variation-settings: <font-variation-settings>; ] || [ font-stretch: <font-stretch>; ] || [ font-weight: <font-weight>; ] || [ font-style: <font-style>; ] }where
<family-name> = <string> | <custom-ident>+
This example simply specifies a downloadable font to use, applying it to the entire body of the document:
<html> <head> <title>Web Font Sample</title> <style type="text/css" media="screen, print"> @font-face { font-family: "Bitstream Vera Serif Bold"; src: url("https://mdn.mozillademos.org/files/2468/VeraSeBd.ttf"); } body { font-family: "Bitstream Vera Serif Bold", serif } </style> </head> <body> This is Bitstream Vera Serif Bold. </body> </html>
In this example, the user's local copy of "Helvetica Neue Bold" is used; if the user does not have that font installed (two different names 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; }
Format | MIME type |
---|---|
TrueType | font/ttf |
OpenType | font/otf |
Web Open File Format | font/woff |
Web Open File Format 2 | font/woff2 |
@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.ttf); font-weight: bold; } }
Specification | Status | Comment |
---|---|---|
WOFF File Format 2.0 The definition of 'WOFF2 font format' in that specification. | Recommendation | Font format specification with new compression algorithm |
WOFF File Format 1.0 The definition of 'WOFF font format' in that specification. | Recommendation | Font format specification |
CSS Fonts Module Level 3 The definition of '@font-face' in that specification. | Candidate Recommendation | Initial definition |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 4 | 12 | 3.5 | 4 | 10 | 3.1 |
WOFF | 6 | Yes | 3.5 | 9 | 11.1 | 5.1 |
WOFF 2 | 36 | 14 | 39
|
No | 23 | 10
|
SVG fonts | No | No | No | No | No | Yes |
font-display |
60 | No | 58 | No | 47 | Yes |
font-family |
4 | 12 | 3.5 | 6 | 9 | 3.1 |
font-feature-settings |
No | No | 34
|
No | No | No |
font-variation-settings |
62 | No | 60
|
No | 49 | No |
font-style |
4 | ? | 3.5 | 4 | 10 | 3.1 |
font-weight |
4 | 12 | 3.5 | 4 | 10 | 3.1 |
src |
4 | 12 | 3.5 | 6 | 9 | 3.1 |
unicode-range |
Yes | Yes | 36 | 9 | Yes | Yes |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | 12 | 4 | 10 | Yes | Yes |
WOFF | 4.4 | Yes | Yes | 4 | 11 | 5 | Yes |
WOFF 2 | 37 | 36 | 14 | 39
|
23 | 10 | 3.0 |
SVG fonts | No | No | No | No | No | Yes | No |
font-display |
60 | 60 | No | 58 | 47 | Yes | No |
font-family |
2.2 | ? | 12 | 4 | 12 | 3.1 | Yes |
font-feature-settings |
No | No | No | 34
|
No | No | No |
font-variation-settings |
62 | 62 | No | 60
|
Yes | No | No |
font-style |
Yes | ? | ? | 4 | 10 | Yes | Yes |
font-weight |
Yes | Yes | 12 | 4 | 10 | Yes | Yes |
src |
2.2 | ? | 12 | 4 | 12 | 3.1 | Yes |
unicode-range |
? | ? | Yes | 36 | ? | Yes | Yes |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face