Do you build things with AMP? Fill out the new AMP Developer Survey!
AMP
  • websites

amp-font

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>
:root {
  --color-error: #B00020;
  --color-primary: #005AF0;
}
@font-face {
  font-family: 'UnavailableFont';
  font-style: normal;
  font-weight: 400;
  src: url(fonts/UnavailableFont.ttf) format('truetype');
}
@font-face {
  font-family: 'Comic AMP';
  font-style: normal;
  font-weight: 400;
  src: local('Comic AMP'), url(/static/samples/fonts/ComicAMP.ttf) format('truetype');
}
@font-face {
  font-family: 'Comic AMP 2';
  font-style: normal;
  font-weight: 400;
  src: local('Comic AMP'), url(/static/samples/fonts/ComicAMP2.ttf) format('truetype');
}

.unavailable-font-loaded .unavailable-font {
  font-family: 'UnavailableFont';
}
.comicamp-loaded .comicamp {
  font-family: 'Comic AMP';
}
.comicamp2-loaded .comicamp2 {
  font-family: 'Comic AMP 2';
}
.comicamp-loading .comicamp,
.comicamp2-loading .comicamp2,
.unavailable-font-loading .unavailable-font {
  color: var(--color-primary);
}
.comicamp-missing .comicamp,
.comicamp2-missing .comicamp2,
.unavailable-font-missing .unavailable-font {
  color: var(--color-error);
}
</style>

Existing font

The amp-font components needs to be embedded inside the body or the head. 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. This sample demonstrates that amp-font will add the class comicamp-loaded to the root element if the font is downloaded.

This text is displayed in red if the font `Comic AMP` is not available.

<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>
<p class="comicamp">
  This text is displayed in red if the font `Comic AMP` is not available.
</p>

Unavailable font

amp-font will add a class unvailable-font-missing to the root element in this sample.

This text is displayed in red if the font `UnavailableFont` is not available.

<amp-font layout="nodisplay"
  font-family="UnavailableFont"
  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>
<p class="unavailable-font">
  This text is displayed in red if the font `UnavailableFont` is not available.
</p>

Using only cached fonts

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

This text is displayed in red if the font `Comic AMP 2` is not cached.

<amp-font layout="nodisplay"
  font-family="Comic AMP 2"
  timeout="0"
  on-error-remove-class="comicamp2-loading"
  on-error-add-class="comicamp2-missing"
  on-load-remove-class="comicamp2-loading"
  on-load-add-class="comicamp2-loaded">
</amp-font>
<p class="comicamp2">
  This text is displayed in red if the font `Comic AMP 2` is not cached.
</p>
자세한 설명이 필요하신가요?

이 페이지의 설명만으로 궁금한 점이 모두 해결되지 않는다면 다른 AMP 사용자에게 문의하여 구체적인 활용 사례를 논의해 보세요.

Stack Overflow로 이동
설명이 부족한 기능을 발견하셨나요?

AMP 프로젝트는 여러분의 참여와 기여를 적극 환영합니다! 오픈 소스 커뮤니티를 통해 지속적으로 활동해 주셔도 좋지만 관심 있는 주제에 한 번만 기여하셔도 큰 도움이 됩니다.

GitHub에서 샘플 수정하기