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>
자세한 설명이 필요하신가요?

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

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

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

GitHub에서 샘플 수정하기