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

amp-img

Introduction

AMP HTML files don't support the normal HTML img tag. With amp-img AMP provides a powerful replacement.

Setup

amp-img is a built-in element and is automatically imported via the AMP runtime.

<script async src="https://cdn.ampproject.org/v0.js"></script>

You can use the amp-bind extension for dynamically changing an image.

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

Basic usage

A simple responsive image - width and height are used to determine the aspect ratio. Use alt to specify text that can act as an alternative for the image.

<amp-img src="https://amp.dev/static/samples/img/amp.jpg"
  width="1080"
  height="610"
  layout="responsive"
  alt="AMP"></amp-img>

Offline fallback

amp-img supports AMP's common attributes, which means you can show a fallback in case the image couldn't be loaded. This is great for adding offline support to your AMPs.

offline
<amp-img src="https://amp.dev/static/samples/img/does-not-exist.jpg"
  width="300"
  height="100"
  layout="responsive">
  <div fallback>offline</div>
</amp-img>

No script

If you want to ensure that users with JavaScript disabled can see your images, you can add an additional img element inside a noscript tag. This also makes it possible for webcrawlers without JavaScript runtime to discover your images.

<amp-img src="https://amp.dev/static/samples/img/amp.jpg"
  alt="AMP"
  width="475"
  height="268"
  layout="responsive">
</amp-img>

Hiding the loading indicator

Use the noloading attribute to hide the loading indicators on page load. This is recommended for logos and small images/icons.

<amp-img src="https://amp.dev/static/samples/img/amp_by_example_logo.svg"
  width="208"
  height="40"
  alt="AMP by Example"
  noloading></amp-img>

Vertically centering an amp-img

Here is a simple approach to vertically center an amp-img using the flex layout (src):

<div class="outer">
  <amp-img class="inner"
    layout="responsive"
    src="https://unsplash.it/300/200"
    width="3"
    height="2"
    alt="a vertically centered amp-img">
  </amp-img>
</div>

...and here is the CSS:

.inner{
  flex-basis: 0;
  -ms-flex-preferred-size: 0;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  flex-grow: 1;
}
.outer {
  height: 320px; /* adjust to your needs */
  display: flex;
  flex-wrap: wrap;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
}
Need further explanation?

If the explanations on this page don't cover all of your questions feel free to reach out to other AMP users to discuss your exact use case.

Go to Stack Overflow
An unexplained feature?

The AMP project strongly encourages your participation and contributions! We hope you'll become an ongoing participant in our open source community but we also welcome one-off contributions for the issues you're particularly passionate about.

Edit sample on GitHub