src
attributesrcset
attribute must not be differentsrcset
attribute must not have different aspect ratios<source>
elements shouldn’t be the samesizes
attribute has to match the width of the image<img>
elements are not allowed<source>
and <img>
tags are allowed inside of <picture>
<img>
element must not be omitted inside of <picture>
src
attribute has no effect on a <source>
element<source>
element must not appear after an <img>
element<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 2x">
<img src="200x100.jpg" srcset="200x100.jpg 200w, 400x200.jpg 400w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg, 400x200.jpg 1x">
<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 1x">
<img src="200x100.jpg" srcset="200x100.jpg 200w, 400x200.jpg 200w">
The syntax of the descriptors is defined in the spec.
<img src="200x100.jpg" srcset="200x100.jpg 1x">
<img src="200x100.jpg" srcset="200x100.jpg 200w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg 1a">
<img src="200x100.jpg" srcset="200x100.jpg 100h">
<img src="200x100.jpg" srcset="200x100.jpg 200w 100h">
<img src="200x100.jpg" srcset="200x100.jpg 1x 200w">
<img src="200x100.jpg" srcset="200x100.jpg 200.0w">
<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 2x">
<img src="200x100.jpg" srcset="200x100.jpg 200w, 400x200.jpg 400w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 400w">
<img src="200x100.jpg" srcset="200x100.jpg, 400x200.jpg 400w">
<img src="200x100.jpg" srcset="200x100.jpg 1x">
<img src="200x100.jpg" srcset="200x100.jpg 200w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg 200w">
<img src="200x100.jpg" srcset="200x100.jpg 200w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg 100w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 2x">
<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 3x">
<img src="200x100.jpg" srcset="200x100.jpg 1x">
<img src="200x100.jpg" sizes="200px" srcset="200x100.jpg 200w">
<img src="200x100.jpg" sizes="100vw" srcset="200x100.jpg 1x">
src
attributeOlder browsers or other user agents which don’t understand the srcset
attribute should get a fallback image.
<img src="200x100.jpg" srcset="200x100.jpg 1x, 400x200.jpg 2x">
<img srcset="200x100.jpg 1x, 400x200.jpg 2x">
srcset
attribute must not be differentThe srcset
attribute isn’t for art direction, so the images must therefore only differ in dimensions not in the image contents.
<img src="cat-200x100.jpg" srcset="cat-200x100.jpg 1x, cat-400x200.jpg 2x">
<img src="cat-200x100.jpg" srcset="cat-200x100.jpg 1x, dog-400x200.jpg 2x">
srcset
attribute must not have different aspect ratiosThe srcset
attribute isn’t for art direction, so the images must therefore only differ in dimensions not in the ratio.
<img src="200x100.jpg" srcset="200x100.jpg 200w, 400x200.jpg 400w" sizes="200px">
<img src="200x100.jpg" srcset="200x100.jpg 200w, 400x100.jpg 400w" sizes="200px">
Loading a large image and display it much smaller should be avoided to save bandwidth. Loading a small image and display it much larger should be avoided to prevent pixelated artifacts.
<img style="width: 200px" src="200x100.jpg">
<img
style="width: 100%"
src="200x100.jpg"
srcset="200x100.jpg 200w, 400x200.jpg 400w, 800x400.jpg 800w, 1600x800.jpg 1600w, 2000x1000.jpg 2000w"
sizes="100vw"
>
<img style="width: 100%" src="200x100.jpg">
<img
style="width: 100%"
src="200x100.jpg"
srcset="200x100.jpg 200w, 4000x2000.jpg 4000w"
sizes="100vw"
>
<source>
elements shouldn’t be the sameThe <source>
element should only be used for art direction and format-based selection. For providing multiple resolutions of the same image use the srcset
attribute instead. More information on CSS-Tricks.
<picture> <!-- different content -->
<source srcset="cat-200x100.jpg" media="(max-width: 600px)">
<img src="dog-200x100.jpg">
</picture>
<picture> <!-- different aspect ratio -->
<source srcset="cat-100x100.jpg" media="(max-width: 600px)">
<img src="cat-200x100.jpg">
</picture>
<picture> <!-- different image type -->
<source srcset="cat-200x100.webp" type="image/webp">
<img src="cat-200x100.jpg">
</picture>
<picture> <!-- same contents, aspect ratio and format -->
<source media="(max-width: 900px)" srcset="cat-400x200.jpg">
<img src="cat-200x100.jpg">
</picture>
<picture> <!-- exact same image but different URL -->
<source media="(max-width: 900px)" srcset="cat-200x100.jpg?foo">
<img src="cat-200x100.jpg">
</picture>
sizes
attribute has to match the width of the imageThe sizes
attribute is a hint for browsers which should tell them how large the image will be displayed. If it doesn’t match the real size, browsers cannot select the correct image source.
<img
style="width: 200px"
sizes="200px"
srcset="200x100.jpg 200w, 400x200.jpg 400w"
src="200x100.jpg"
>
<style>
.wrong-sizes-demo-1 { width: 200px }
@media (min-width: 600px) {
.wrong-sizes-demo-1 { width: 400px }
}
</style>
<picture>
<source
media="(min-width: 600px)"
sizes="400px"
srcset="400x400.jpg 400w, 800x800.jpg 800w"
>
<img
class="wrong-sizes-demo-1"
sizes="200px"
srcset="200x100.jpg 200w, 400x200.jpg 400w"
src="200x100.jpg"
>
</picture>
<img
style="width: 100%"
sizes="200px"
srcset="200x100.jpg 200w, 400x200.jpg 400w"
src="200x100.jpg"
>
<picture>
<source
media="(min-width: 600px)"
sizes="200px"
srcset="200x100.jpg 200w, 400x200.jpg 400w"
>
<img
style="width: 200px"
sizes="400px"
srcset="400x400.jpg 400w, 800x800.jpg 800w"
src="400x400.jpg"
>
</picture>
<img>
elements are not allowedOnly one <img>
element is allowed inside of <picture>
.
<picture>
<img src="200x100.jpg">
</picture>
<picture>
<img src="200x100.jpg">
<img src="200x100.jpg">
</picture>
<source>
and <img>
tags are allowed inside of <picture>
You should not use <picture>
as a wrapper to put additional elements inside it.
<picture>
<img src="200x100.jpg">
</picture>
<picture>
<img src="200x100.jpg">
<span>Image Caption</span>
</picture>
<img>
element must not be omitted inside of <picture>
The <picture>
element needs an <img>
element in order to display something.
<picture>
<img src="200x100.jpg">
</picture>
<picture>
<source srcset="200x100.jpg">
</picture>
src
attribute has no effect on a <source>
elementThe <source>
element only supports the srcset
attribute.
<picture>
<source srcset="200x200.jpg">
<img src="200x100.jpg">
</picture>
<picture>
<source src="200x200.jpg">
<img src="200x100.jpg">
</picture>
<source>
element must not appear after an <img>
elementThe <img>
element has to be the last element inside of <picture>
.
<picture>
<source srcset="200x200.jpg">
<img src="200x100.jpg">
</picture>
<picture>
<img src="200x200.jpg">
<source srcset="200x100.jpg">
</picture>