The picture element is a markup pattern that allows developers to declare multiple sources for an image.
This markup is a container used to specify multiple <source> elements for a specific <img> contained in it.
1 | <picture> |
Use the media attribute
The media attribute lets you specify a media query that the user agent will evaluates to select a <source> element. If the media query evaluates to false, the <source> element is skipped.
1 | <picture> |
The srcset and sizes attributes extend the img and source elements to provide a list of available image sources and their sizes. Browsers can then use this information to pick the best image source.
1 | <img |
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-fair-320w.jpg)
A space
The image’s inherit width in pixels(480w) - that this issue the
wunit, notpxas you might expect.
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.
Before each comma, we write:
A media condition(max-width: 480px) – when the viewport width is 480 px or less
A space
The width of the slot the image will fill when the media condition is true(440px)
Resolution switching
1 | <img |
