AMP
  • websites

How to support Images with unknown Dimensions

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:

<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.

<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:

<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:

  <div class="fixed-container">
<amp-img layout="fill" src="https://unsplash.it/300/200?image=1074"></amp-img>
  </div>
  <div class="fixed-container">
<amp-img layout="fill" src="https://unsplash.it/200/300?image=1074"></amp-img>
  </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.

<div class="fixed-container">
  <amp-img class="contain" layout="fill" src="https://unsplash.it/400/300"></amp-img>
</div>
<div class="fixed-container">
  <amp-img class="contain" layout="fill" src="https://unsplash.it/300/300"></amp-img>
</div>
<div class="fixed-container">
  <amp-img class="contain" layout="fill" src="https://unsplash.it/200/300"></amp-img>
</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:

<div class="fixed-container">
  <amp-img class="cover" layout="fill" src="https://unsplash.it/400/300"></amp-img>
</div>
<div class="fixed-container">
  <amp-img class="cover" layout="fill" src="https://unsplash.it/300/300"></amp-img>
</div>
<div class="fixed-container">
  <amp-img class="cover" layout="fill" src="https://unsplash.it/200/300"></amp-img>
</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:

<div class="fixed-height-container">
  <amp-img class="contain" layout="fill" src="https://unsplash.it/400/300"></amp-img>
</div>
<div class="fixed-height-container">
  <amp-img class="contain" layout="fill" src="https://unsplash.it/300/300"></amp-img>
</div>
<div class="fixed-height-container">
  <amp-img class="contain" layout="fill" src="https://unsplash.it/200/300"></amp-img>
</div>
<div class="fixed-height-container">
  <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.

<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>
Perlu penjelasan lebih lanjut?

Jika penjelasan pada halaman ini tidak menjawab semua pertanyaan Anda, silakan hubungi pengguna AMP lainnya untuk mendiskusikan contoh penggunaan Anda yang spesifik.

Kunjungi Stack Overflow
Fitur yang tidak dijelaskan?

Proyek AMP sangat menganjurkan partisipasi dan kontribusi Anda! Kami berharap Anda akan terus ambil bagian dalam komunitas sumber terbuka kami, tetapi kami juga menerima kontribusi satu kali untuk topik tertentu yang Anda minati.

Edit sampel di GitHub