Images are displayed using the
<img> tag. There is a few different image types that can be used, the most common is PNG, JPEG, GIF, SVG and WebP.
img tag has an attribute called
src and this is used to set the image location.
There is also an
alt attribute that is very important to add. This helps screen readers and search enigne bots to understand what the images is.
This tag is used as a wrapper around the
img tag to display a caption with the image.
The srcset attribute allows you to set responsive images that the browser can use depending on the pixel density or window width, according to your preferences. This way, it can only download the resources it needs to render the page, without downloading a bigger image if it's on a mobile device, for example.
sizes attributes look complicated, but they're not too hard to understand if you format them as shown above, with a different part of the attribute value on each line. Each value contains a comma-separated list, and each part of those lists is made up of three sub-parts. Let's run through the contents of each now:
srcset defines the set of images we will allow the browser to choose between, and what size each image is. Before each comma, we write:
- An image filename (elva-fairy-480w.jpg)
- A space
- The image's inherent width in pixels (480w) — note that this uses the w unit, not px as you might expect. This is the image's real size, which can be found by inspecting the image file on your computer (for example, on a Mac you can select the image in Finder and press Cmd + I to bring up the info screen).
sizes defines a set of media conditions (e.g. screen widths) and indicates what image size would be best to choose, when certain media conditions are true — these are the hints we talked about earlier. In this case, before each comma we write:
- A media condition ((max-width:480px)) — you'll learn more about these in the CSS topic, but for now let's just say that a media condition describes a possible state that the screen can be in. In this case, we are saying "when the viewport width is 480 pixels or less".
- A space
- The width of the slot the image will fill when the media condition is true (440px)
So, with these attributes in place, the browser will:
- Look at its device width
- Work out which media condition in the sizes list is the first one to be true
- Look at the slot size given to that media query
- Load the image referenced in the srcset list that most closely matches the chosen slot size
And that's it! At this point, if a supporting browser with a viewport width of 480px loads the page, the (max-width: 480px) media condition will be true, and so the browser chooses the 440px slot. The elva-fairy-480w.jpg will be loaded, as its inherent width (480w) is the closest to 440px. The 800px picture is 128KB on disk, whereas the 480px version is only 63KB — a saving of 65KB. Now, imagine if this was a page that had many pictures on it. Using this technique could save mobile users a lot of bandwidth.
picture does a similar job to srcset, the differences are subtle.
You use picture when instead of just serving a smaller version of a file, you completely want to change it. Or serve a different image format. A good usecase is when serve a WebP image, which is a format still not widely supported. In the picture tag you specify a list of images, and they will be used in order so if you look at the example, browsers that support WebP will use the first image, and fallback to JPG if not.
The source tag defines one (or more) formats for the images. The img tag is the fallback in case the browser is very old and does not support the picture tag.
You can also set media queries inside the source tag.