@font-face

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2016.

摘要

The @font-face CSS at-rule (en-US) 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 (en-US).

實驗性質: 這是一個實驗中的功能 (en-US)
此功能在某些瀏覽器尚在開發中,請參考兼容表格以得到不同瀏覽器用的前輟。

語法

@font-face {
  [font-family: <family-name>;]?
  [src: [ <uri> [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>];
}

參數值

<family-name>

Specifies a font name that will be used as font face value for font properties.

<uri>

URL for the remote font file location, or the name of a font on the user's computer in the form local("Font Name").

<font-variant>

A font-variant (en-US) value.

<font-stretch>

A font-stretch (en-US) value.

<weight>

A font weight (en-US) value.

<style>

A font style (en-US) value.

You can specify a font on the user's local computer by name using the local() syntax. If that font isn't found, other sources will be tried until one is found.

範例

這個範例指定一個可供下載的字型,並套用至 document 的整個 body。

html
<!doctype html>
<html lang="en-US">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>Web Font Sample</title>
    <style media="screen, print">
      @font-face {
        font-family: "Bitstream Vera Serif Bold";
        src: url("https://mdn.github.io/css-examples/web-fonts/VeraSeBd.ttf");
      }

      body {
        font-family: "Bitstream Vera Serif Bold", serif;
      }
    </style>
  </head>
  <body>
    This is Bitstream Vera Serif Bold.
  </body>
</html>

這個範例會套用使用者本地的 "Helvetica Neue Bold" 字型,只有當使用者未安裝該字型(兩種名稱都試過了),才會下載 "MgOpenModernaBold.ttf" 字型:

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

注意事項

  • In Gecko, 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 (en-US) 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.

  • When Gecko displays a page that uses web fonts, it initially displays text using the best CSS fallback font available on the user's computer while it waits for the web font to finish downloading. As each web font finishes downloading, Gecko updates the text that uses that font. This allows the user to read the text on the page more quickly.

規格文件

Specification
CSS Fonts Module Level 4
# font-face-rule

瀏覽器相容性

BCD tables only load in the browser

詳見