This translation is incomplete. Please help translate this article from English
Learn about HTML features you can use to adapt your site's images to various screen sizes and display resolutions.
|Prerequisites:||You should already know how to create a basic HTML document and how to add static images to a webpage.|
|Objective:||Learn how to feed multiple source files to your
Note: Vector images are the ultimate responsive images because the files are small while the images are scalable to any size without loss of definition. Use vector images whenever you can; they're even more effective than the techniques described here. This article demonstrates how to use multiple versions of a bitmap image, in different sizes, to provide the best possible output given the current screen size and resolution.
Note also that while this article discusses how to implement responsive images in HTML, sometimes it makes more sense to use CSS.
Why responsive images?
Here's the problem we're solving:
On a webpage, you have a box that must be filled by an image. More precisely, the box must be filled by pixels, so many wide by so many tall. Just how many pixels wide and tall depends on your visitor's device.
You also have an image file, a set number of pixels wide and a set number of pixels tall. The image naturally should display in a box the same number of pixels wide and tall as the image. If the box is significantly too big, the image doesn't have enough pixels and it's going to look grainy. If the box is significantly too small, you're wasting bandwidth and slowing down your page by loading a larger image than you need.
Responsive images solves this problem by letting you offer the browser several image files, all showing the same thing but containing different numbers of pixels. That way the browser can load an image that will look sharp enough but won't needlessly slow down the page.
So how do you do it?
In this section, we'll solve what is, by far, the most common problem: displaying identical image content, just larger or smaller depending on the device. In the next section, we'll look at some less common scenarios.
<img> element? It lets you point the browser to a single source file:
<img src="chalet.jpg" alt="Quaint wooden cottage in the Alps">
We can use two new attributes,
sizes (in addition to
src), to provide several additional source images and enough information to help the browser pick the right one. It looks like this when you're done:
<img src="chalet.jpg" alt="Quaint wooden cottage in the Alps" srcset=" chalet-256.jpg 256w, chalet-512.jpg 512w, chalet-1024.jpg 1024w" sizes=" (max-width: 500px) 100vw, (max-width: 900px) 40vw, 400px">
sizes each contain comma-separated lists.
srcset: Between the commas, write
- an image filename (
- a space
- the image's inherent width in pixels (
sizes: Between the commas, write
- a media condition (
- a space
- the width of the slot the image will fill when the media condition is true (
- For the slot width, you may provide an absolute length (
em) or a relative length (
vw, that is, percent of viewport width). You may also provide a combination using
- The last slot width is the default (for when no media condition is true). Therefore, the last slot width has no media condition.
max-widthsimply means, "if the user's screen is no wider than X pixels".
- The browser ignores everything after the first matching condition. Pay attention to the order of the media conditions.
Why can't the browser just look at the CSS to find out the width of the image slot?
Images at different resolutions but one real-world size
If you're supporting multiple display densities, but everyone sees your image at the same real-world size, you should use
srcset with x-descriptors and without
<img src="chalet.jpg" alt="Quaint wooden cottage in the Alps" srcset="chalet.jpg, chalet-1-5x.jpg 1.5x, chalet-2x.jpg 2x, chalet-3x.jpg 3x">
xmeans, "this many device pixels for one CSS pixel"
Art direction means cropping an image either 1) to make the main subject easier to see when the image is small or 2) to make a landscape image suitable for a portrait slot (and vice versa). (However, it must be the same image content in all cases. To, say, show phone users a jet and desktop users a ladybug is a misuse of the responsive image mechanism.)
Art direction is a more complex problem, and needs a more complex solution: the
<picture> is a wrapper containing several
<source> elements, followed by the all-important
<picture> <source media="(min-width: 1000px)" srcset="chalet-desktop.jpg"> <source media="(min-width: 700px)" srcset="chalet-tablet.jpg"> <img src="chalet-phone.jpg" alt="Quaint wooden cottage in the Alps"> </picture>
- Just like
sizes. In a responsive images scenario, do not use
<source>may also take a
mediaattribute containing a CSS media query. Use
mediaonly in an art direction scenario, because it leaves the browser no choice of which image to use. When you do use
media, don't offer media conditions within
- In all cases, you must provide an
alt, right before
</picture>, otherwise no images will appear.
Use modern image formats boldly
There are several exciting new image formats (think WebP and JPEG-2000) that can maintain a low file size and high quality at the same time. However, browser support is spotty.
<picture> lets us continue catering to older browsers. Supply MIME types inside
type attributes so the browser can immediately reject unsupported file types:
<picture> <source type="image/svg+xml" srcset="pyramid.svg"> <source type="image/webp" srcset="pyramid.webp"> <img src="pyramid.png" alt="regular pyramid built from four equilateral triangles"> </picture>
- Do not use the
mediaattribute, unless you also need art direction.
- In a
<source>element, you can only refer to images of the type declared in
- As before, you're welcome to use comma-separated lists with
sizes, as needed.