大网数据广告活动
Web 标准与前端开发

FontFace: FontFace() constructor

· 52CE 我爱测

The **`FontFace()`** constructor creates a new FontFace object.

CSS Font Loading API

The FontFace() constructor creates a new FontFace object.

Syntax

new FontFace(family, source)
new FontFace(family, source, descriptors)

Parameters

  • family

    • : Specifies a font family name that can be used to match against this font face when styling elements.

      Takes the same type of values as the @font-face/font-family descriptor of @font-face. This value may also be read and set using the FontFace.family property.

  • source

    • : The font source. This can be either:
  • descriptors

    • : A set of optional descriptors passed as an object. It can contain any of the descriptors available for @font-face:
      • ascentOverride
        • : With an allowable value for @font-face/ascent-override.
      • descentOverride
        • : With an allowable value for @font-face/descent-override.
      • display
        • : With an allowable value for @font-face/font-display.
      • featureSettings
        • : With an allowable value for font-feature-settings.
      • lineGapOverride
        • : With an allowable value for @font-face/line-gap-override.
      • sizeAdjust
        • : With an allowable value for @font-face/size-adjust.
      • stretch
        • : With an allowable value for @font-face/font-stretch.
      • style
        • : With an allowable value for @font-face/font-style.
      • unicodeRange
        • : With an allowable value for @font-face/unicode-range.
      • variationSettings
        • : With an allowable value for @font-face/font-variation-settings.
      • weight
        • : With an allowable value for @font-face/font-weight.

Exceptions

  • SyntaxError DOMException
    • : Thrown when a descriptor string does not match the grammar of the corresponding @font-face descriptor, or the specified binary source cannot be loaded. This error results in FontFace.status being set to error.

Examples

async function loadFonts() {
  const font = new FontFace("my-font", 'url("my-font.woff")', {
    style: "normal",
    weight: "400",
    stretch: "condensed",
  });
  // wait for font to be loaded
  await font.load();
  // add font to document
  document.fonts.add(font);
  // enable font with CSS class
  document.body.classList.add("fonts-loaded");
}

Specifications

Browser compatibility

See also

  • @font-face

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。