From ed0709eda64929079081b4806130028f771a8814 Mon Sep 17 00:00:00 2001 From: Florian Merz Date: Thu, 11 Feb 2021 14:48:32 +0100 Subject: unslug ms: move --- .../responsive_images/index.html | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 files/ms/learn/html/multimedia_and_embedding/responsive_images/index.html (limited to 'files/ms/learn/html/multimedia_and_embedding/responsive_images/index.html') diff --git a/files/ms/learn/html/multimedia_and_embedding/responsive_images/index.html b/files/ms/learn/html/multimedia_and_embedding/responsive_images/index.html new file mode 100644 index 0000000000..0589cda2d8 --- /dev/null +++ b/files/ms/learn/html/multimedia_and_embedding/responsive_images/index.html @@ -0,0 +1,170 @@ +--- +title: Add responsive images to a webpage +slug: Learn/HTML/Howto/Add_responsive_image_to_a_webpage +translation_of: Learn/HTML/Multimedia_and_embedding/Responsive_images +--- +
+

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 {{htmlelement("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 {{htmlelement("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, {{htmlattrxref("srcset", "img")}} and {{htmlattrxref("sizes", "img")}} sizes (in addition to {{htmlattrxref("alt", "img")}} and {{htmlattrxref("src", "img")}}), 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. +
  3. a space
  4. +
  5. the image's inherent width in pixels (256w)
  6. +
+ +

For sizes: Between the commas, write

+ +
    +
  1. a media condition ((max-width:500px))
  2. +
  3. a space
  4. +
  5. the width of the slot the image will fill when the media condition is true (100vw)
  6. +
+ +
+ +
+ +
+

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">
+
+ +
+ +
+ +

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 {{htmlelement("picture")}} element. <picture> is a wrapper containing several {{htmlelement("source")}} elements, followed by the all-important {{htmlelement("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>
+
+ + + +

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

Learn more

+ + -- cgit v1.2.3-54-g00ecf