From da78a9e329e272dedb2400b79a3bdeebff387d47 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:17 -0500 Subject: initial commit --- .../add_responsive_image_to_a_webpage/index.html | 170 +++++++++++++++++++++ files/ms/learn/html/howto/index.html | 153 +++++++++++++++++++ 2 files changed, 323 insertions(+) create mode 100644 files/ms/learn/html/howto/add_responsive_image_to_a_webpage/index.html create mode 100644 files/ms/learn/html/howto/index.html (limited to 'files/ms/learn/html/howto') diff --git a/files/ms/learn/html/howto/add_responsive_image_to_a_webpage/index.html b/files/ms/learn/html/howto/add_responsive_image_to_a_webpage/index.html new file mode 100644 index 0000000000..0589cda2d8 --- /dev/null +++ b/files/ms/learn/html/howto/add_responsive_image_to_a_webpage/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

+ + diff --git a/files/ms/learn/html/howto/index.html b/files/ms/learn/html/howto/index.html new file mode 100644 index 0000000000..7ce5cf622f --- /dev/null +++ b/files/ms/learn/html/howto/index.html @@ -0,0 +1,153 @@ +--- +title: Learn HTML to solve problems +slug: Learn/HTML/Howto +tags: + - CodingScripting + - HTML + - NeedsTranslation + - TopicStub +translation_of: Learn/HTML/Howto +--- +

Once you've covered the basics, there isn't one right path to learn {{Glossary("HTML")}}. You can pick up whatever you like at your own pace. HTML is simply a set of {{glossary("tag","tags")}} you can use to set up your document structure and add extra functionality to your document. The following articles explain thoroughly, with full working examples, how to use HTML for the most common, frequent Web development tasks. If you need a quick explanation of a tag, please head over to our HTML reference.

+ +

Common use cases

+ +

HTML covers a lot of very common use cases in Web design. It's highly likely you'll come across these scenarios:

+ +
+
+

Basic structure

+ +

The most basic application of HTML is document structure. If you're new to HTML you should start with this.

+ + + +

Basic text-level semantics

+ +

HTML specializes in providing semantic information for a document, so HTML answers many questions you might have about how to get your message across best in your document.

+ + +
+ +
+ + +

One of the main reasons for HTML is make navigation easy with {{Glossary("hyperlink", "hyperlinks")}}, which can be used in many different ways:

+ + + +

Images & multimedia

+ + + +

Scripting & styling

+ +

HTML only sets up document structure. To solve presentation issues, use {{glossary("CSS")}}, or use scripting to make your page interactive.

+ + + +

Embedded content

+ + +
+
+ +

Uncommon or advanced problems

+ +

Beyond the basics, HTML is very rich and offers advanced features for solving complex problems. These articles help you tackle the less common use cases you may face:

+ +
+
+

Forms

+ +

Forms are a complex HTML structure made to send data from a webpage to a web server. We encourage you to go over our full dedicated guide. Here is where you should start:

+ + + +

Tabular information

+ +

Some information, called tabular data, needs to be organized into tables with columns and rows. It's one of the most complex HTML structures, and mastering it is not easy:

+ + + +

Data representation

+ + + +

Interactivity

+ + +
+ + +
+ +

     

-- cgit v1.2.3-54-g00ecf