@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: "Open Sans";
  src: url("/fonts/OpenSans-Regular-webfont.woff2") format("woff2"),
       url("/fonts/OpenSans-Regular-webfont.woff") format("woff");
}
					

Descriptors

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.

font-stretch
A 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
A 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
A 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
A font-variant 值。
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.

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() 函数:

src: url(ideal-sans-serif.woff) format("woff"),
     url(basic-sans-serif.ttf) format("truetype");
						

The available types are: "woff" , "woff2" , "truetype" , "opentype" , "embedded-opentype" ,和 "svg" .

unicode-range

The range of Unicode code points to be used from the font.

描述

local() function is provided, specifying a font name to look for on the user's computer, and the 用户代理 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 "web-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.

@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 MIME Types

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.ttf);
        font-weight: bold;
      }
    }
    							

形式句法

@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>+

范例

Specifying a downloadable font

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>
					

The output of this example code looks like so:

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

规范

规范 状态 注释
WOFF File Format 2.0
The definition of 'WOFF2 font format' in that specification.
推荐 Font format specification with new compression algorithm
WOFF File Format 1.0
The definition of 'WOFF font format' in that specification.
推荐 Font format specification
CSS Fonts Module Level 3
The definition of '@font-face' in that specification.
候选推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
@font-face Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 4 Opera 完整支持 10 Safari 完整支持 3.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 2 Samsung Internet Android 完整支持 1.0
SVG fonts 弃用 非标 Chrome 不支持 1 — 38 Edge 不支持 No Firefox 不支持 No IE 不支持 No Opera 不支持 15 — 25 Safari 完整支持 3.2 WebView Android 不支持 ≤37 — 38 Chrome Android 不支持 18 — 38 Firefox Android 不支持 No Opera Android 不支持 14 — 25 Safari iOS 完整支持 3 Samsung Internet Android 不支持 1.0 — 3.0
WOFF Chrome 完整支持 6 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 9 Opera 完整支持 11.1 Safari 完整支持 5.1 WebView Android 完整支持 4.4 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 11.1 Safari iOS 完整支持 5 Samsung Internet Android 完整支持 1.0
WOFF 2 Chrome 完整支持 36 Edge 完整支持 14 Firefox 完整支持 39
完整支持 39
完整支持 35 Disabled
Disabled ). To change preferences in Firefox, visit
IE 不支持 No Opera 完整支持 23 Safari 完整支持 10 注意事项
完整支持 10 注意事项
注意事项 Supported only on macOS 10.12 (Sierra) and later.
WebView Android 完整支持 37 Chrome Android 完整支持 36 Firefox Android 完整支持 39
完整支持 39
完整支持 35 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 24 Safari iOS 完整支持 10 Samsung Internet Android 完整支持 3.0
font-display Chrome 完整支持 72 Edge 完整支持 79 Firefox 完整支持 58 IE 不支持 No Opera 完整支持 60 Safari 完整支持 11.1 WebView Android 完整支持 72 Chrome Android 完整支持 72 Firefox Android 完整支持 58 Opera Android 完整支持 51 Safari iOS 完整支持 11.3 Samsung Internet Android 完整支持 11.0
font-family Chrome 完整支持 4 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 6 Opera 完整支持 10 Safari 完整支持 3.1 WebView Android 完整支持 2.2 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 3.1 Samsung Internet Android 完整支持 1.0
font-feature-settings Chrome 不支持 No Edge 不支持 No Firefox 完整支持 34 注意事项
完整支持 34 注意事项
注意事项 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.
完整支持 15 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 From Firefox 4 to Firefox 14 (inclusive), Firefox supported an older, slightly different syntax. See OpenType Font Feature support in Firefox 4 .
IE 不支持 No Opera 不支持 No Safari 不支持 No WebView Android 不支持 No Chrome Android 不支持 No Firefox Android 完整支持 34 注意事项
完整支持 34 注意事项
注意事项 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.
完整支持 15 Prefixed 注意事项
Prefixed Implemented with the vendor prefix: -moz-
注意事项 From Firefox 4 to Firefox 14 (inclusive), Firefox supported an older, slightly different syntax. See OpenType Font Feature support in Firefox 4 .
Opera Android 不支持 No Safari iOS 不支持 No Samsung Internet Android 不支持 No
font-stretch Chrome 完整支持 62 Edge 完整支持 17 Firefox 完整支持 62 IE 不支持 No Opera 完整支持 49 Safari 完整支持 10.1 WebView Android 完整支持 62 Chrome Android 完整支持 62 Firefox Android 完整支持 62 Opera Android 完整支持 41 Safari iOS 完整支持 10.3 Samsung Internet Android 完整支持 6.0
font-style Chrome 完整支持 4 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 4 Opera 完整支持 10 Safari 完整支持 3.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 2 Samsung Internet Android 完整支持 1.0
font-variant Chrome 完整支持 4 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 4 Opera 完整支持 10 Safari 完整支持 3.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 2 Samsung Internet Android 完整支持 1.0
font-variation-settings Chrome 完整支持 62 Edge 完整支持 79 Firefox 完整支持 62
完整支持 62
不支持 60 — 62 Disabled
Disabled ). To change preferences in Firefox, visit
IE 不支持 No Opera 完整支持 49 Safari 不支持 No WebView Android 完整支持 62 Chrome Android 完整支持 62 Firefox Android 完整支持 62
完整支持 62
不支持 60 — 62 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android 完整支持 46 Safari iOS 不支持 No Samsung Internet Android 完整支持 8.0
font-weight Chrome 完整支持 4 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 4 Opera 完整支持 10 Safari 完整支持 3.1 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 2 Samsung Internet Android 完整支持 1.0
src Chrome 完整支持 4 Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 6 Opera 完整支持 10 Safari 完整支持 3.1 WebView Android 完整支持 2.2 Chrome Android 完整支持 18 Firefox Android 完整支持 4 Opera Android 完整支持 10.1 Safari iOS 完整支持 3.1 Samsung Internet Android 完整支持 1.0
unicode-range Chrome 完整支持 1 Edge 完整支持 12 Firefox 完整支持 36 IE 完整支持 9 Opera 完整支持 15 Safari 完整支持 3.2 WebView Android 完整支持 ≤37 Chrome Android 完整支持 18 Firefox Android 完整支持 36 Opera Android 完整支持 14 Safari iOS 完整支持 3 Samsung Internet Android 完整支持 1.0

图例

完整支持

完整支持

不支持

不支持

非标。预期跨浏览器支持较差。

非标。预期跨浏览器支持较差。

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改: