amp-fit-text

Edit on Github
Open in Playground

Introduction

amp-fit-text enables you to manage the size and fit of text within a given area.

Setup

Include the amp-fit-text component.

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

Basic Usage

In each of these examples, the <amp-fit-text> element is nested within in a 200x200 grey div block to demonstrate differences in attributes and layout values.

When amp-fit-text is used with a given height, width, and layout="responsive" attribute, the text will scale responsively in the ratio provided but not exceed the size of its parent.

Aspect ratio: 1.5:1 (3:2)

Example

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div class="fixedblock">
  <amp-fit-text width="300"
    height="200"
    layout="responsive">Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</amp-fit-text>
</div>

Aspect ratio: 1:1

Example

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div class="fixedblock">
  <amp-fit-text width="200"
    height="200"
    layout="responsive">Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</amp-fit-text>
</div>

The min-font-size attribute dictates a minimum size for the text. In this case, we've dictated a minimum of 30, which will cause it to exceed the size of its fixed block parent and be truncated to fit as appropriate.

Example

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div class="fixedblock">
  <amp-fit-text width="300"
    height="200"
    layout="responsive"
    min-font-size="30">Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</amp-fit-text>
</div>

The max-font-size attribute dictates a maximum size for the text. In this case, we've dictated a maximum of 10, ensuring it won't fill the size of its parent element.

Example

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div class="fixedblock">
  <amp-fit-text width="300"
    height="200"
    layout="responsive"
    max-font-size="10">Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</amp-fit-text>
</div>

When amp-fit-text is used with a given height and layout="fixed-height" attribute, it will fit to the size of its given height (200), and adjust the width as needed to stay within the bounds of its parent container.

Example

Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.
<div class="fixedblock">
  <amp-fit-text height="200"
    layout="fixed-height">Lorem ipsum dolor sit amet, has nisl nihil convenire et, vim at aeque inermis reprehendunt.</amp-fit-text>
</div>