The @font-face
CSS at-rule allows authors to specify online fonts to display text on their web pages. By allowing authors to provide their own fonts, @font-face
eliminates the need to depend on the limited number of fonts users have installed on their computers. 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.font-style
font-style
value.font-weight
font-weight
value.font-variant
font-variant
value.font-feature-settings
font-variation-settings
src
unicode-range
@font-face { [ font-family: <family-name>; ] || [ src: [ <url> [ format(<string>#) ]? | <font-face-name> ]#; ] || [ unicode-range: <urange>#; ] || [ font-variant: <font-variant>; ] || [ font-feature-settings: normal | <feature-tag-value>#; ] || [ font-variation-settings: normal | [ <string> <number>] # || [ font-stretch: <font-stretch>; ] || [ font-weight: <weight>; ] || [ font-style: <style>; ] }where
<family-name> = <string> | <custom-ident>+
<feature-tag-value> = <string> [ <integer> | on | off ]?
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; }
.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. | Candidate 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 |
Feature | 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 351 |
No | 23 | 102 |
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 |
343 |
No | No | 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 |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | Opera Android | iOS Safari | Samsung Internet |
---|---|---|---|---|---|---|---|
Basic support | Yes | Yes | 12 | 4 | 10 | Yes | ? |
WOFF | 4.4 | Yes | Yes | 4 | 11 | 5 | ? |
WOFF 2 | 36 | 36 | 14 |
39 351 |
23 | 10 | ? |
SVG fonts | No | No | No | No | No | Yes | ? |
font-display |
60 | 60 | No | 58 | 47 | Yes | ? |
font-family |
2.2 | ? | 12 | 4 | 12 | 3.1 | ? |
font-feature-settings |
No | No | No |
343 |
No | No | ? |
font-style |
Yes | ? | ? | 4 | 10 | Yes | ? |
font-weight |
Yes | Yes | 12 | 4 | 10 | Yes | ? |
src |
2.2 | ? | 12 | 4 | 12 | 3.1 | ? |
unicode-range |
? | ? | Yes | 36 | ? | Yes | ? |
1. From version 35: this feature is behind the gfx.downloadable_fonts.woff2.enabled
preference (needs to be set to true
). To change preferences in Firefox, visit about:config.
2. Supported only on macOS 10.12 (Sierra) and later.
3. The ISO/IEC CD 14496-22 3rd edition suggests using the ssty
feature to provide glyph variants more suitable for use in scripts (for example primes used as superscripts). Starting with Firefox 29, this is done automatically by the MathML rendering engine. The ISO/IEC CD 14496-22 3rd edition also suggests applying the dtls
feature to letters when placing mathematical accents to get dotless forms (for example dotless i, j with a hat). Starting with Firefox 35, this is done automatically by the MathML rendering engine. You can override the default values determined by the MathML rendering engine with CSS.
4. From Firefox 4 to Firefox 14 (inclusive), Firefox supported an older, slightly different syntax. See OpenType Font Feature support in Firefox 4.
© 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