Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Add responsive images to a webpage

Terjemahan ini belum lengkap. Sila bantu terjemahkan artikel ini daripada Bahasa Inggeris.

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 <img> element, so the browser can pull the right image for the occasion.

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.

Remember the <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, srcset and sizes sizes (in addition to alt and 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">

srcset and sizes each contain comma-separated lists.

For srcset: Between the commas, write

  1. an image filename (chalet-256.jpg)
  2. a space
  3. the image's inherent width in pixels (256w)

For sizes: Between the commas, write

  1. a media condition ((max-width:500px))
  2. a space
  3. the width of the slot the image will fill when the media condition is true (100vw)
  • For the slot width, you may provide an absolute length (px, em) or a relative length (vw, that is, percent of viewport width). You may also provide a combination using calc.
  • The last slot width is the default (for when no media condition is true). Therefore, the last slot width has no media condition.
  • max-width simply 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?

Because the browser's preloader starts downloading images before the main parser has a chance to interpret CSS and JavaScript. You want that, because images are heavy and circumventing the preloader may add another 20% on to your page load time.

Advanced scenarios

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

<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">
  • Note that 1x is implied.

  • x means, "this many device pixels for one CSS pixel"

Art direction

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> element. <picture> is a wrapper containing several <source> elements, followed by the all-important <img> element:

<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 <img>, <source> can take srcset and sizes. In a responsive images scenario, do not use <source> and src together.
  • <source> may also take a media attribute containing a CSS media query. Use media only 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 sizes.
  • In all cases, you must provide an <img> element, with src and 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 media attribute, unless you also need art direction.
  • In a <source> element, you can only refer to images of the type declared in type.
  • As before, you're welcome to use comma-separated lists with srcset and sizes, as needed.

Learn more

Tag Dokumen dan Penyumbang

 Penyumbang untuk halaman ini: QA8500
 Terakhir dikemaskini oleh: QA8500,