- Usage
- Specify a fallback image
- Scale an image up to a maximum width
- The difference between the responsive and intrinsic layouts
- Set a fixed sized image
- Set the aspect ratio
- Set multiple source files for different screen resolutions
- Maintain the aspect ratio for images with unknown dimensions
- Accessibility considerations for images
- Attributes
- Styling
- Validation
amp-img
Description
Replaces the HTML5 img tag.
Supported Layouts
Usage
The AMP HTML replacement for the normal HTML img tag. With amp-img, AMP
provides a powerful replacement.
AMP may choose to delay or prioritize resource loading based on the viewport
position, system resources, connection bandwidth, or other factors. The
amp-img components allows the runtime to effectively manage image resources
this way.
amp-img components, like all externally fetched AMP resources, must be given
an explicit size (as in width / height) in advance, so that the aspect ratio
can be known without fetching the image. Actual layout behavior is determined by
the layout attribute.
Learn more about layouts in the AMP HTML Layout System spec and Supported Layouts.
In the following example, we display an image that responds to the size of the
viewport by setting layout=responsive. The image stretches and shrinks
according to the aspect ratio specified by the width and height.
<amp-img alt="A view of the sea" src="/static/inline-examples/images/sea.jpg" width="900" height="675" layout="responsive" > </amp-img>
Learn about responsive AMP pages in the Create Responsive AMP Pages guide.
If the resource requested by the amp-img component fails to load, the space
will be blank unless a
fallback
child is provided. A fallback is only executed on the initial layout and
subsequent src changes after the fact (through resize + srcset for example)
will not have a fallback for performance implications.
Specify a fallback image
In the following example, if the browser cannot load the image, it'll display the fallback image instead (here we're using an inline SVG as fallback):
<amp-img alt="Misty road" width="550" height="368" src="image-does-not-exist.jpg" > <amp-img alt="Misty road" fallback width="550" height="368" src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSAiaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmlld0JveD0iMSAxIDggNCI+PGltYWdlIGZpbHRlcj0idXJsKCNibHVyKSIgd2lkdGg9IjEwIiBoZWlnaHQ9IjYiIHhsaW5rOmhyZWY9ImRhdGE6aW1hZ2UvcG5nO2Jhc2U2NCxpVkJPUncwS0dnb0FBQUFOU1VoRVVnQUFBQW9BQUFBR0NBWUFBQUQ2OEEvR0FBQUFBWE5TUjBJQXJzNGM2UUFBQUVSbFdFbG1UVTBBS2dBQUFBZ0FBWWRwQUFRQUFBQUJBQUFBR2dBQUFBQUFBNkFCQUFNQUFBQUJBQUVBQUtBQ0FBUUFBQUFCQUFBQUNxQURBQVFBQUFBQkFBQUFCZ0FBQUFEK2lGWDBBQUFBcjBsRVFWUUlIVDJQU1FxRlFBeEVYN2VpNG9BYkVRV3Y0TVlMZUhLdjQwSVV3UW5uL25ZdmZxQ29JcWxRaVVpU1JQbStqNVFTei9PNDd4dkhjUmlHZ2I3dnNTeUw2N3FRNy91YW9XNHN5L0kzYXYwOGp6SHlsZFNHZmQrcDY5cXcxbTNia21VWlFnalQweXlpS0ZKNlE4ZVdaVWxSRklSaGFLS2JwdUU4VCtaNVJyaXVxejZna2VjNVZWVXhqaVBUTkpremp1TmcyemJzN3htQ0lDQk5VK0k0cHVzNmxGS3M2NHB0MjBicnhCODZ4VmdkUHdJV2NRQUFBQUJKUlU1RXJrSmdnZz09Ii8+PGZpbHRlciBpZD0iYmx1ciI+PGZlR2F1c3NpYW5CbHVyIHN0ZERldmlhdGlvbj0iLjUiIC8+PC9maWx0ZXI+PC9zdmc+" > </amp-img> </amp-img>
A placeholder background color or other visual can be set using CSS selector and style on the element itself.
Additional image features like captions can be implemented with standard HTML
(for example, figure and figcaption).
Learn more about using amp-img from these resources:
Scale an image up to a maximum width
If you want your image to scale as the window is resized but up to a maximum width (so the image doesn't stretch beyond its width):
- Set
layout=responsivefor<amp-img>. - On the container of the image, specify the
max-width:<max width to display image>CSS attribute. Why on the container? Anamp-imgelement withlayout=responsiveis a block-level element, whereas,<img>is inline. Alternatively, you could setdisplay: inline-blockin your CSS for theamp-imgelement.
The difference between the responsive and intrinsic layouts
Both the responsive and intrinsic layouts create an image that will scale
automatically. The main difference is that the intrinsic layout uses an SVG
image as its scaling element. This will make it behave in the same way as a
standard HTML image while retaining the benefit of the browser knowing the image
size on initial layout. The intrinsic layout will have an intrinsic size and
will inflate a floated div until it reaches either the image size defined by
the width and height attributes passed to the amp-img (not by the natural size
of the image) or a CSS constraint such as max-width. The responsive layout
will render 0x0 in a floated div because it takes its size from the parent,
which has no natural size when floated.
Set a fixed sized image
If you want your image to display at a fixed size:
- Set
layout=fixedfor<amp-img>. - Specify the
widthandheight.
Learn about the
inferred layout
if you don't specify the layout attribute.
Set the aspect ratio
For responsive images, the width and height do not need to match the exact
width and height of the amp-img; those values just need to result in the same
aspect-ratio.
For example, instead of specifying width="900" and height="675", you can
just specify width="1.33" and height="1".
<amp-img alt="A view of the sea" src="/static/inline-examples/images/sea.jpg" width="1.33" height="1" layout="responsive" > </amp-img>
Set multiple source files for different screen resolutions
The srcset attribute should be used to provide different
resolutions of the same image, that all have the same aspect ratio. The browser
will automatically choose the most appropriate file from srcset based on the
screen resolution and width of the user's device.
In contrast, the media
attribute shows or hides AMP components, and should be used when designing
responsive layouts. The appropriate way to display images with differing aspect
ratios is to use multiple <amp-img> components, each with a media attribute
that matches the screen widths in which to show each instance.
See the guide on creating responsive AMP pages for more details.
Maintain the aspect ratio for images with unknown dimensions
The AMP layout system requires the aspect ratio of an image in advance before
fetching the image; however, in some cases you might not know the image's
dimensions. To display images with unknown dimensions and maintain the aspect
ratios, combine AMP's
fill
layout with the
object-fit CSS
property. For more information, see the
How to support images with unknown dimensions
example.