@font-face

@font-face

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.

Syntax

Descriptors

font-family
Specifies a name that will be used as the font face value for font properties.
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.
font-variant
A font-variant value.
font-stretch
A font-stretch value.
font-weight
A font-weight value.
font-style
A font-style value.
unicode-range
The range of Unicode code points to be used from the font.

Formal syntax

@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-stretch: <font-stretch>; ] ||
  [ font-weight: <weight>; ] ||
  [ font-style: <style>; ]
}

where
<family-name> = <string> | <custom-ident>+
<feature-tag-value> = <string> [ <integer> | on | off ]?

Examples

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

View the live example

<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;
}

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 are used to relax this restriction.
  • Because there are no defined MIME types for TrueType, OpenType, and Web Open File Format (WOFF) fonts, the MIME type of the file specified is not considered.
  • @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; 
      } 
    }

Specifications

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

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support 4.0 (Yes) 3.5 (1.9.1) 4.0 10.0 3.1
WOFF 6.0 (Yes) 3.5 (1.9.1) 9.0 11.10 5.1
WOFF2 38 (Yes) 39 (39)[1] No support 24 No support
SVG Fonts[2] (Yes) No support No support[3] No support (Yes) (Yes)
unicode-range (Yes) (Yes)

36 (36)

9.0 (Yes) (Yes)
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mini Opera Mobile Safari Mobile
Basic support (Yes) (Yes) 1.0 (1.9.1) ? No support 10.0 (Yes)
WOFF 4.4 (Yes) 5.0 (5.0) 10.0 No support 11.0 5.0
WOFF2 No support (Yes) 39.0 (39.0)[1] No support ? ? No support
SVG Fonts[2] ? No support No support[3] ? No support 10.0 (Yes)
unicode-range ? (Yes) 36.0 (36.0) ? No support ? (Yes)

[1] WOFF2 supports from Gecko 35 to Gecko 38 included is only activated by default on Aurora and Nightly versions. To use it on Beta and Release versions, the user needs to set the gfx.downloadable_fonts.woff2.enabled preference to true.

[2] Support for SVG Fonts was dropped in favor of allowing SVG glyphs within OpenType font packages.

[3] For Gecko there was bug 119490 filed for implementing SVG Fonts, though as SVG Fonts got superseded by SVG glyphs within OpenType font packages, the bug got closed.

Notes

  • Support of the Embedded OpenType font format is not included in the compatibility table because it is a proprietary feature. Prior to IE 9.0, IE supported only this format.
  • TrueType and OpenType are not included because they are superseded by WOFF.
  • The same origin policy is not implemented in WebKit based browsers like Safari and Mobile Safari. This was also true for Blink based browsers, until September, 2014 - Chrome 37 and Opera 24.

See also

© 2005–2017 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

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部