amp-font

Edit on Github
Open in Playground

Introduction

Learn how to use the amp-font component to trigger and monitor the loading of custom fonts on AMP pages.

Setup

Import the amp-font component.

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

Define custom CSS rules to style your document depending on whether a font was loaded successfully or not. Find out about all the supported css classes here

<style amp-custom>
  @font-face {
    font-family: 'Font Does Not exist';
    font-style: normal;
    font-weight: 400;
    src: url(fonts/FontDoesNotExist.ttf) format('truetype');
  }
  @font-face {
    font-family: 'Another Font That Does Not exist';
    font-style: normal;
    font-weight: 400;
    src: url(fonts/FontDoesNotExist.ttf) format('truetype');
  }
  @font-face {
    font-family: 'Comic AMP';
    font-style: normal;
    font-weight: 400;
    src: local('Comic AMP'), url(/fonts/ComicAMP.ttf) format('truetype');
  }

  .unavailable-font-loaded .unavailable-font {
    font-family: 'Font Does Not exist', serif, sans-serif;
  }
  .comicamp-loaded .comicamp {
    font-family: 'Comic AMP', serif, sans-serif;
  }
  .another-unavailable-font-loaded .another-unavailable-font {
    font-family: 'Another Font That Does Not exist', serif, sans-serif;
  }
  .unavailable-font-loading, .comicamp-loading, .another-unavailable-font-loading {
    color: blue;
  }
  .unavailable-font-loading .unavailable-font,
    .comicamp-loading .comicamp,
      .another-unavailable-font-loading .another-unavailable-font {
        color: aqua;
  }
  .unavailable-font-missing .unavailable-font,
    .comicamp-missing .comicamp,
      .another-unavailable-font-missing .another-unavailable-font  {
        color: red;
  }
</style>

Sample for a non existing font

Embed amp-font inside the body or the head of the document. Use the attribute timeout to specify the time in milliseconds to wait until the font becomes available. This attribute is optional and it's default value is 3000. Use on-error-remove-class, on-error-add-class, on-load-remove-class, on-load-add-class to specify the CSS classes to be used if the font loads correctly or not. For a detailed description, please refer to the documentation

Example

<amp-font layout="nodisplay"
  font-family="Font Does Not exist"
  timeout="2000"
  on-error-remove-class="unavailable-font-loading"
  on-error-add-class="unavailable-font-missing"
  on-load-remove-class="unavailable-font-loading"
  on-load-add-class="unavailable-font-loaded">
</amp-font>

Here we declare...

Example

  <amp-font layout="nodisplay"
    font-family="Comic AMP"
    timeout="2000"
    on-error-remove-class="comicamp-loading"
    on-error-add-class="comicamp-missing"
    on-load-remove-class="comicamp-loading"
    on-load-add-class="comicamp-loaded">
  </amp-font>

and use an existing font.

Example

This text is displayed in red if the chosen font is not supported.

<p class="comicamp">
  This text is displayed in red if the chosen font is not supported.
</p>

This time the font is not available.

Example

This text is displayed in red if the chosen font is not supported.

<p class="unavailable-font">
  This text is displayed in red if the chosen font is not supported.
</p>

Using only cached fonts

Set timeout="0" to load the font only if already in cache.

Example

<amp-font layout="nodisplay"
  font-family="Another Font That Does Not exist"
  timeout="0"
  on-error-remove-class="another-unavailable-font-loading"
  on-error-add-class="another-unavailable-font-missing"
  on-load-remove-class="another-unavailable-font-loading"
  on-load-add-class="another-unavailable-font-loaded">
</amp-font>