How to support Images with unknown Dimensions?

Edit on Github
Open in Playground

Introduction

This sample demonstrates how it is possible to embed images with unknown dimensions into an AMP page while maintaining the correct aspect ratio.

Background

To avoid page jumps while loading an AMP page, the AMP runtime's static layout system requires that the height of all elements is known in advance. This is why the amp-img extension requires the width and height of an image to be specified:

Example

<amp-img width="300"
  height="200"
  src="https://unsplash.it/300/200?image=1074"></amp-img>

The amp-img extension supports different layouts. The responsive layout requires width and height to be able to determine an image's aspect ratio.

Example

<amp-img layout="responsive"
  width="300"
  height="200"
  src="https://unsplash.it/300/200?image=1074"></amp-img>

The fixed-height layout only requires a height, but stretches the image to fill the available width:

Example

<amp-img layout="fixed-height"
  height="200"
  src="https://unsplash.it/300/200?image=1074"></amp-img>

The fill layout is the only layout supported by amp-img that doesn't require any image dimensions. However, images will be stretched in both dimensions to fill the container:

Example

<div class="flex">
  <div class="fixed-container m1">
    <amp-img layout="fill"
      src="https://unsplash.it/300/200?image=1074"></amp-img>
  </div>
  <div class="fixed-container m1">
    <amp-img layout="fill"
      src="https://unsplash.it/200/300?image=1074"></amp-img>
  </div>
</div>

The question is: how can images with unknown dimensions be embeded inside an AMP pages while maintaining their correct aspect ratio?

Object-Fit to the Rescue

We can solve this problem by combining AMP's fill layout with the object-fit CSS property. The object-fit property helps us ensure that the image maintains its aspect ratio while being resized via the fill layout.

First we need to define a container to constrain the maximum size of the image, e.g.:

 .fixed-container {
   position: relative;
   width: 200px;
   height: 200px;
 }

Then we can use the object-fit property to define how the img inside the amp-img extension will resize to maintain its aspect ratio:

  amp-img.contain img {
    object-fit: contain;
  }

object-fit: contain increases or decreases the size of the image to fill the container whilst preserving the image's aspect-ratio.

Example

<div class="flex">
  <div class="fixed-container m1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/400/300"></amp-img>
  </div>
  <div class="fixed-container m1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/300/300"></amp-img>
  </div>
  <div class="fixed-container m1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/200/300"></amp-img>
  </div>
</div>

With object-fit: cover the image will fill the height and width of its container, but maintains the aspect ratio by cropping the image:

Example

<div class="flex">
  <div class="fixed-container m1">
    <amp-img class="cover"
      layout="fill"
      src="https://unsplash.it/400/300"></amp-img>
  </div>
  <div class="fixed-container m1">
    <amp-img class="cover"
      layout="fill"
      src="https://unsplash.it/300/300"></amp-img>
  </div>
  <div class="fixed-container m1">
    <amp-img class="cover"
      layout="fill"
      src="https://unsplash.it/200/300"></amp-img>
  </div>
</div>

Fixed-Height Layout with correct Aspect Ratios

This approach works also with responsive container dimensions, for example, a dynamic width:

   .fixed-height-container {
     position: relative;
     width: 100%;
     height: 300px;
   }

The result is a fixed-height layout that maintains the correct image aspect ratio:

Example

  <div class="fixed-height-container my1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/400/300"></amp-img>
  </div>
  <div class="fixed-height-container my1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/300/300"></amp-img>
  </div>
  <div class="fixed-height-container my1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/200/300"></amp-img>
  </div>
  <div class="fixed-height-container my1">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/1920/480"></amp-img>
  </div>

Image Carousels

This approach also works very well for image carousels by combining it with an amp-carousel using a fixed-height layout.

Example

<amp-carousel height="300"
  layout="fixed-height"
  type="slides">
  <div class="fixed-height-container">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/500/400"></amp-img>
  </div>
  <div class="fixed-height-container">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/500/500"></amp-img>
  </div>
  <div class="fixed-height-container">
    <amp-img class="contain"
      layout="fill"
      src="https://unsplash.it/300/500"></amp-img>
  </div>
</amp-carousel>