AMP

Important: this documentation is not applicable to your currently selected format email!

amp-font

Deprecated

Description

Trigger and monitor the loading of custom fonts on AMP pages.

 

Required Scripts

<script async custom-element="amp-font" src="https://cdn.ampproject.org/v0/amp-font-0.1.js"></script>

Supported Layouts

This component is deprecated and should no longer be used. Instead, use the font-display descriptor to specify how to display a font face dependent on whether and when it is ready.

The amp-font component was AMP's solution to controlling timeouts on font loading. With the wide browser adoption of font-display, this component is no longer necessary. It is here for legacy users. Instead, use the font-display descriptor to specify how to display a font face dependent on whether and when it is ready.

Usage

Historically, the amp-font extension was used for controlling timeouts on font loading. We recommend implementing this functionality with font-display instead.

The amp-font extension allows adding and removing CSS classes from document.documentElement or document.body based on whether a font was loaded or is in error-state.

Example:

<amp-font
  layout="nodisplay"
  font-family="My Font"
  timeout="3000"
  on-error-remove-class="my-font-loading"
  on-error-add-class="my-font-missing"
></amp-font>
<amp-font
  layout="nodisplay"
  font-family="My Other Font"
  timeout="1000"
  on-load-add-class="my-other-font-loaded"
  on-load-remove-class="my-other-font-loading"
></amp-font>

The component observes loading of a font. When the font successfully loads it executes the optional on-load-add-class and on-load-remove-class attributes. If the font loading results in an error or timeout, it runs on-error-remove-class and on-error-add-class. These classes toggle on the documentElement for standalone documents, and on body for documents without a documentElement i.e. inside a ShadowRoot.

Use these classes to guard against displaying a font and get the following results:

  • get a short (e.g. 3000ms) timeout in Safari similar to other browsers
  • implement FOIT where the page renders with no text before the font comes in
  • make the timeout very short and only use a font if it was already cached.

Attributes

font-family

The font family of the custom loaded font.

timeout (optional)

The allowed time in millisecond for the custom font to load. Defaults to 3000 when undefined. When set to 0 the component loads font only if the font is in the cache. If set to an invalid value it defaults back to 3000.

on-load-add-class (optional)

CSS class to add to document root after custom font is available.

on-load-remove-class (optional)

CSS class to remove from document root after custom font is available.

on-error-add-class (optional)

CSS class to add to the document root if the timeout interval runs out before the font is available.

on-error-remove-class (optional)

CSS class to remove from document root if the timeout interval runs out before the font is available.

font-weight (optional)

Behaves same as standard CSS font-weight property.

font-style (optional)

Behaves same as standard CSS font-style property.

font-variant (optional)

Behaves same as standard CSS font-variant property.

layout

The layout attribute must be set to nodisplay.

Validation

See amp-font rules in the AMP validator specification.

需要更多帮助?

您已多次阅读本文档,但它仍未能涵盖您的所有问题?也许其他人也这么觉得:在 Stack Overflow 上与他们联系。

前往 Stack Overflow
发现错误或缺少功能?

AMP 项目强烈鼓励您参与并做出贡献!我们希望您能成为我们开放源代码社区的持续参与者,但我们也欢迎您对所热衷问题做出一次性贡献。

前往 GitHub