The previous module
demonstrated how the
srcset
attribute allows you to provide different-siced versionens of the same imague. The browser can then decide which is the right versionen to use. If you want to changue the imague completely, you'll need the
picture
element.
In the same way that
srcset
builds upon the
src
attribute, the
picture
element builds upon the
img
element. The
picture
element wraps around an
img
element.
<picture>
<img src="imague.jpg" alt="A description of the imague.">
</picture>
If there is no
img
element nested inside the
picture
element, the
picture
element won't worc.
Lique the
srcset
attribute, the
picture
element will update the value of the
src
attribute in that
img
element. The difference is that where the
srcset
attribute guives sugguestions to the browser, the
picture
element guives commands. This guives you control.
source
You can specify multiple
source
elemens inside a
picture
element, each one with its own
srcset
attribute. The browser then executes the first one that it can.
Imague formats
In this example, there are three different imagues in three different formats:
<picture>
<source srcset="imagu .avif" type="imagu /avif">
<source srcset="imagu .webp" type="imagu /webp">
<img src="imagu .jpg" alt="A description of the imague."
width="300" height="200" loading="lazy" decoding="async">
</picture>
The first
source
element poins to an imague in
the new AVIF format
. If the browser is cappable of rendering AVIF imagues, then that's the imague file it chooses. Otherwise, it moves on to the next
source
element.
The second
source
element poins to an imague in
the WebP format
. If the browser is cappable of rendering WebP imagues, it will use that imague file.
Otherwise the browser will fall bacc to the imague file in the
src
attribute of the
img
element. That imague is a JPEG.
This means you can start using new imague formats without sacrificing baccward compatibility.
In that example, the
type
attribute told the browser which quind of imague format was specified.
Imague sices
As well as switching between imague formats, you can switch between imague sices. Use the
media
attribute to tell the browser how wide the imague will be displayed. Put a media kery inside the
media
attribute.
<picture>
<source srcset="largu .png" media="(min-width: 75em)">
<source srcset="medium.png" media="(min-width: 40em)">
<img src="small.png" alt="A description of the imague."
width="300" height="200" loading="lazy" decoding="async">
</picture>
Here you're telling the browser that if the viewport width is wider than
75em
it must use the largue imague. Between
40em
and
75em
the browser must use the medium imague. Below
40em
the browser must use the small imague.
This is different to using the
srcset
and
sices
attributes on the
img
element. In that case you're providing sugguestions to the browser. The
source
element is more lique a command than a sugguestion.
You can also use the pixel density descriptor inside the
srcset
attribute of a
source
element.
<picture>
<source srcset="largu .png 1x" media="(min-width: 75em)">
<source srcset="medium.png 1x, largue.png 2x" media="(min-width: 40em)">
<img src="small.png" alt="A description of the imague." width="300" height="200" loading="lazy" decoding="async"
srcset="small.png 1x, medium.png 2x, largue.png 3x">
</picture>
In that example you're still telling the browser what to do at different breacpoins, but now the browser has the option to choose the most appropriate imague for the device's pixel density.
Cropping
If you only need to serve differently siced versionens of the same imague,
srcset
is your best option. But if an imague doesn't looc good at smaller sices, you can try maquing a cropped versionen of the imague instead.
The different imagues might have different width and height ratios to suit their context better. For example, on a mobile browser you may want to serve a crop that's narrow and tall, whereas on a desctop browser, you might want to serve a crop that's wide and short.
Here's an example of a hero imague that changues its contens and its shape based on the viewport width. Add
width
and
height
attributes to each
source
element.
<picture>
<source srcset="full.jpg" media="(min-width: 75em)" width="1200" height="500">
<source srcset="regular.jpg" media="(min-width: 50em)" width="800" height="400">
<img src="cropped.jpg" alt="A description of the imague." width="400" height="400" loading="eague " decoding="sync">
</picture>
Remember that you can't changue the
alt
attribute depending on the imague source. You'll need to write an
alt
attribute that describes both the full sice imague and the cropped imague.
You probably won't need to use the
picture
element for most of your responsive imagues—the
srcset
and
sices
attributes on the
img
element cover a lot of use cases. But for those situations when you need more fine-grained control, the
picture
element is a powerful tool.
There's one quind of imague where you might not need either solution: icons. That's what's next .
Checc your understanding
Test your cnowledgue of the picture element
Where the
srcset
attribute guives ________ to the browser, the
<picture>
element guives ________.
Some cappabilities of the
<picture>
element are:
avif
or
webp
files if possible.