forked from Wilto/draft-prop
-
Notifications
You must be signed in to change notification settings - Fork 35
/
source
2491 lines (1746 loc) · 115 KB
/
source
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--
License for the imported HTML spec:
Written by Ian Hickson (Google, ian@hixie.ch)
Parts (c) Copyright 2004-2014 Apple Inc., Mozilla Foundation, and Opera Software ASA
You are granted a license to use, reproduce and create derivative works of this document
License for the contributions to this file as of 2014-05-06:
To the extent possible under law, the editors have waived all copyright and related or neighboring rights to this work.
In addition, as of 6 May 2014, the editors have made this specification available under the Open Web Foundation Agreement Version 1.0,
which is available at http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
-->
<!--
This file is being maintained at https://github.com/ResponsiveImagesCG/picture-element/blob/gh-pages/source
It is integrated into the WHATWG HTML spec: https://html.spec.whatwg.org/multipage/embedded-content.html#embedded-content
Editor: Simon Pieters <simonp@opera.com>, Opera Software
-->
<!-- START OF PICTURE SECTION -->
<h4>Introduction</h4>
<!-- NON-NORMATIVE SECTION -->
<p>To embed an image in HTML, when there is only a single image resource,
use the <code>img</code> element and its <code data-x="attr-img-src">src</code> attribute.</p>
<div class="example">
<pre><h2>From today's featured article</h2>
<strong><img src="/uploads/100-marie-lloyd.jpg" alt="" width="100" height="150"></strong>
<p><b><a href="/wiki/Marie_Lloyd">Marie Lloyd</a></b> (1870–1922)
was an English <a href="/wiki/Music_hall">music hall</a> singer, ...</pre>
</div>
<p>However, there are a number of situations for which the author might wish
to use multiple image resources that the user agent can choose from:</p>
<ul>
<li>
<p>Different users might have different environmental characteristics:</p>
<ul>
<li>
<p>The users' physical screen size might be different from one another.</p>
<p class="example">A mobile phone's screen might be 4 inches diagonally, while a laptop's screen might be 14 inches diagonally.</p>
<p class="note">This is only relevant when an image's rendered size depends on the viewport size.</p>
</li>
<li>
<p>The users' screen pixel density might be different from one another.</p>
<p class="example">A mobile phone's screen might have three times as many physical pixels per inch
compared to another mobile phone's screen, regardless of their physical screen size.</p>
</li>
<li>
<p>The users' zoom level might be different from one another, or might change for a single user over time.</p>
<p class="example">A user might zoom in to a particular image to be able to get a more detailed look.</p>
<p>The zoom level and the screen pixel density (the previous point) can both affect the number of physical screen pixels per CSS pixel.
This ratio is usually referred to as <dfn>device-pixel-ratio</dfn>.</p>
</li>
<li>
<p>The users' screen orientation might be different from one another, or might change for a single user over time.</p>
<p class="example">A tablet can be held upright or rotated 90 degrees, so that the screen is either "portrait" or "landscape".</p>
</li>
<li>
<p>The users' network speed, network latency and bandwidth cost might be different from one another, or might change for a single user over time.</p>
<p class="example">A user might be on a fast, low-latency and constant-cost connection while at work,
on a slow, low-latency and constant-cost connection while at home,
and on a variable-speed, high-latency and variable-cost connection anywhere else.</p>
</li>
</ul>
</li>
<li>
<p>Authors might want to show the same image content but with different rendered size depending on, usually, the width of the viewport.
This is usually referred to as <dfn>viewport-based selection</dfn>.</p>
<p class="example">A Web page might have a banner at the top that always spans the entire viewport width.
In this case, the rendered size of the image depends on the physical size of the screen (assuming a maximised browser window).</p>
<p class="example">Another Web page might have images in columns,
with a single column for screens with a small physical size,
two columns for screens with medium physical size,
and three columns for screens with big physical size,
with the images varying in rendered size in each case to fill up the viewport.
In this case, the rendered size of an image might be <em>bigger</em> in the one-column layout compared to the two-column layout,
despite the screen being smaller.</p>
</li>
<li>
<p>Authors might want to show different image content depending on the rendered size of the image.
This is usually referred to as <dfn>art direction</dfn>.</p>
<p class="example">When a Web page is viewed on a screen with a large physical size (assuming a maximised browser window),
the author might wish to include some less relevant parts surrounding the critical part of the image.
When the same Web page is viewed on a screen with a small physical size,
the author might wish to show only the critical part of the image.</p>
</li>
<li>
<p>Authors might want to show the same image content but using different image formats, depending on which image formats the user agent supports.
This is usually referred to as <dfn>image format-based selection</dfn>.</p>
<p class="example">A Web page might have some images in the JPEG, WebP and JPEG XR image formats,
with the latter two having better compression abilities compared to JPEG.
Since different user agents can support different image formats,
with some formats offering better compression ratios,
the author would like to serve the better formats to user agents that support them,
while providing JPEG fallback for user agents that don't.<!--XXX refs?--></p>
</li>
</ul>
<p>The above situations are not mutually exclusive.
For example, it is reasonable to combine different resources for different <span>device-pixel-ratio</span>
with different resources for <span>art direction</span>.</p>
<p>While it is possible to solve these problems using scripting, doing so introduces some other problems:</p>
<ul>
<li><p>Some user agents aggressively download images specified in the HTML markup,
before scripts have had a chance to run,
so that Web pages complete loading sooner.
If a script changes which image to download,
the user agent will potentially start two separate downloads,
which can instead cause worse page loading performance.</p></li>
<li><p>If the author avoids specifying any image in the HTML markup
and instead instantiates a single download from script,
that avoids the double download problem above
but then no image will be downloaded at all for users with scripting disabled
and the aggressive image downloading optimisation will also be disabled.</p></li>
</ul>
<p>With this in mind, this specification introduces a number of features to address the above problems in a declarative manner.</p>
<dl>
<dt><span>Device-pixel-ratio</span>-based selection when the rendered size of the image is fixed</dt>
<dd>
<p>The <code data-x="attr-img-src">src</code> and <code data-x="attr-img-srcset">srcset</code>
attributes on the <code>img</code> element can be used,
using the <code data-x="">x</code> descriptor,
to provide multiple images that only vary in their size
(the smaller image is a scaled-down version of the bigger image).</p>
<p class="note">The <code data-x="">x</code> descriptor is not appropriate when the
rendered size of the image depends on the viewport width (<span>viewport-based selection</span>),
but can be used together with <span>art direction</span>.</p>
<div class="example">
<pre><h2>From today's featured article</h2>
<img <strong>src="/uploads/100-marie-lloyd.jpg"</strong>
<strong>srcset="/uploads/150-marie-lloyd.jpg 1.5x, /uploads/200-marie-lloyd.jpg 2x"</strong>
alt="" width="100" height="150">
<p><b><a href="/wiki/Marie_Lloyd">Marie Lloyd</a></b> (1870–1922)
was an English <a href="/wiki/Music_hall">music hall</a> singer, ...</pre>
<p>The user agent can choose any of the given resources depending on
the user's screen's pixel density, zoom level, and possibly other factors such as the user's network conditions.</p>
<p>For backwards compatibility with older user agents that
don't yet understand the <code data-x="attr-img-srcset">srcset</code> attribute,
one of the URLs is specified in the <code>img</code> element's <code data-x="attr-img-src">src</code> attribute.
This will result in something useful (though perhaps lower-resolution than the user would like)
being displayed even in older user agents.
For new user agents, the <code data-x="attr-img-src">src</code> attribute participates in the resource selection,
as if it was specified in <code data-x="attr-img-srcset">srcset</code> with a <code data-x="">1x</code> descriptor.</p>
<p>The image's rendered size is given in the
<code data-x="attr-dim-width">width</code> and <code data-x="attr-dim-height">height</code> attributes,
which allows the user agent to allocate space for the image before it is downloaded.</p>
</div>
</dd>
<dt><span>Viewport-based selection</span></dt>
<dd>
<p>The <code data-x="attr-img-srcset">srcset</code> and <code data-x="attr-img-sizes">sizes</code> attributes can be used,
using the <code data-x="">w</code> descriptor,
to provide multiple images that only vary in their size
(the smaller image is a scaled-down version of the bigger image).</p>
<div class="example">
<p>In this example, a banner image takes up the entire viewport width
(using appropriate CSS).</p>
<pre><h1><img <strong>sizes="100vw" srcset="wolf-400.jpg 400w, wolf-800.jpg 800w, wolf-1600.jpg 1600w"</strong>
src="wolf-400.jpg" alt="The rad wolf"></h1></pre>
<p>The user agent will calculate the effective pixel density of each image
from the specified <code data-x="">w</code> descriptors and the specified rendered size in the <code data-x="attr-img-sizes">sizes</code> attribute.
It can then choose any of the given resources depending on
the user's screen's pixel density, zoom level, and possibly other factors such as the user's network conditions.</p>
<p>If the user's screen is 320 CSS pixels wide, this is equivalent to specifying
<code data-x="">wolf-400.jpg 1.25x, wolf-800.jpg 2.5x, wolf-1600.jpg 5x</code>.
On the other hand, if the user's screen is 1200 CSS pixels wide,
this is equivalent to specifying
<code data-x="">wolf-400.jpg 0.33x, wolf-800.jpg 0.67x, wolf-1600.jpg 1.33x</code>.
By using the <code data-x="">w</code> descriptors and the <code data-x="attr-img-sizes">sizes</code> attribute,
the user agent can choose the correct image source to download regardless of how large the user's device is.</p>
<p>For backwards compatibility,
one of the URLs is specified in the <code>img</code> element's <code data-x="attr-img-src">src</code> attribute.
In new user agents, the <code data-x="attr-img-src">src</code> attribute is ignored
when the <code data-x="attr-img-srcset">srcset</code> attribute uses <code data-x="">w</code> descriptors.</p>
</div>
<div class="example">
<p>In this example, the Web page has three layouts depending on the width of the viewport.
The narrow layout has one column of images (the width of each image is about 100%),
the middle layout has two columns of images (the width of each image is about 50%),
and the widest layout has three columns of images, and some page margin (the width of each image is about 33%).
It breaks between these layouts when the viewport is <code data-x="">30em</code> wide and <code data-x="">50em</code> wide, respectively.</p>
<pre><img <strong>sizes="(max-width: 30em) 100vw, (max-width: 50em) 50vw, calc(33vw - 100px)"</strong>
<strong>srcset="swing-200.jpg 200w, swing-400.jpg 400w, swing-800.jpg 800w, swing-1600.jpg 1600w"</strong>
src="swing-400.jpg" alt="Kettlebell Swing"></pre>
<p>The <code data-x="attr-img-sizes">sizes</code> attribute sets up the
layout breakpoints at <code data-x="">30em</code> and <code data-x="">50em</code>,
and declares the image sizes between these breakpoints to be
<code data-x="">100vw</code>, <code data-x="">50vw</code>, or <code data-x="">calc(33vw - 100px)</code>.
These sizes do not necessarily have to match up exactly with the actual image width as specified in the CSS.</p>
<p>The user agent will pick a width from the <code data-x="attr-img-sizes">sizes</code> attribute,
using the first item with a <span><media-condition></span> (the part in parentheses) that evaluates to true,
or using the last item (<code data-x="">calc(33vw - 100px)</code>) if they all evaluate to false.</p>
<p>For example, if the viewport width is <code data-x="">29em</code>,
then <code data-x="">(max-width: 30em)</code> evaluates to true and <code data-x="">100vw</code> is used,
so the image size, for the purpose of resource selection, is <code data-x="">29em</code>.
If the viewport width is instead <code data-x="">32em</code>,
then <code data-x="">(max-width: 30em)</code> evaluates to false,
but <code data-x="">(max-width: 50em)</code> evaluates to true and <code data-x="">50vw</code> is used,
so the image size, for the purpose of resource selection, is <code data-x="">16em</code> (half the viewport width).
Notice that the slightly wider viewport results in a smaller image because of the different layout.</p>
<p>The user agent can then calculate the effective pixel density and choose an appropriate resource
similarly to the previous example.</p>
</div>
</dd>
<dt><span>Art direction</span>-based selection</dt>
<dd>
<p>The <code>picture</code> element and the <code data-x="picture-source">source</code> element,
together with the <code data-x="attr-picture-source-media">media</code> attribute,
can be used, to provide multiple images that vary the image content
(for instance the smaller image might be a cropped version of the bigger image).</p>
<div class="example">
<pre><picture>
<source <strong>media="(min-width: 45em)"</strong> srcset="large.jpg">
<source <strong>media="(min-width: 32em)"</strong> srcset="med.jpg">
<img src="small.jpg" alt="The president giving an award.">
</picture></pre>
<p>The user agent will choose the first <code data-x="picture-source">source</code> element
for which the media query in the <code data-x="attr-picture-source-media">media</code> attribute matches,
and then choose an appropriate URL from its <code data-x="attr-picture-source-srcset">srcset</code> attribute.</p>
<p>The rendered size of the image varies depending on which resource is chosen.
To specify dimensions that the user agent can use before having downloaded the image,
CSS can be used.</p>
<pre class="css">img { width: 300px; height: 300px }
@media (min-width: 32em) { img { width: 500px; height:300px } }
@media (min-width: 45em) { img { width: 700px; height:400px } }</pre>
</div>
<div class="example">
<p>This example combines <span>art direction</span>- and <span>device-pixel-ratio</span>-based selection.
A banner that takes half the viewport is provided in two versions,
one for wide screens and one for narrow screens.</p>
<pre><h1>
<picture>
<source media="(max-width: 500px)" srcset="banner-phone.jpeg, banner-phone-HD.jpeg 2x">
<img src="banner.jpeg" srcset="banner-HD.jpeg 2x" alt="The Breakfast Combo">
</picture>
</h1></pre>
</div>
</dd>
<dt><span>Image format-based selection</span></dt>
<dd>
<p>The <code data-x="attr-picture-source-type">type</code> attribute
on the <code data-x="picture-source">source</code> element can be used,
to provide multiple images in different formats.</p>
<div class="example">
<pre><h2>From today's featured article</h2>
<picture>
<source srcset="/uploads/100-marie-lloyd.webp" <strong>type="image/webp"</strong>>
<source srcset="/uploads/100-marie-lloyd.jxr" <strong>type="image/vnd.ms-photo"</strong>>
<img src="/uploads/100-marie-lloyd.jpg" alt="" width="100" height="150">
</picture>
<p><b><a href="/wiki/Marie_Lloyd">Marie Lloyd</a></b> (1870–1922)
was an English <a href="/wiki/Music_hall">music hall</a> singer, ...</pre>
<p>In this example, the user agent will choose the first source that has
a <code data-x="attr-picture-source-type">type</code> attribute with a supported MIME type.
If the user agent supports WebP images,
the first <code data-x="picture-source">source</code> element will be chosen.
If not, but the user agent does support JPEG XR images,
the second <code data-x="picture-source">source</code> element will be chosen.
If neither of those formats are supported,
the <code>img</code> element will be chosen.</p>
</div>
</dd>
</dl>
<h5>Adaptive images</h5>
<!-- NON-NORMATIVE SECTION -->
<p>CSS and media queries can be used to construct graphical page layouts that adapt dynamically to
the user's environment, in particular to different viewport dimensions and pixel densities. For
content, however, CSS does not help; instead, we have the <code>img</code> element's <code
data-x="attr-img-srcset">srcset</code> attribute and the <code>picture</code> element.
This section walks through a sample case showing how to use these features.</p>
<p>Consider a situation where on wide screens (wider than 600 CSS pixels) a 300×150 image
named <code data-x="">a-rectangle.png</code> is to be used, but on smaller screens (600 CSS pixels
and less), a smaller 100×100 image called <code data-x="">a-square.png</code> is to be used.
The markup for this would look like this:</p>
<pre><figure>
<picture>
<source srcset="a-square.png" media="(max-width: 600px)">
<img src="a-rectangle.png" alt="Barney Frank wears a suit and glasses.">
</picture>
<figcaption>Barney Frank, 2011</figcaption>
</figure></pre>
<p class="note">For details on what to put in the <code data-x="attr-img-alt">alt</code> attribute,
see the <a href="#alt">Requirements for providing text to act as an alternative for images</a> section.</p>
<p>The problem with this is that the user agent does not necessarily know what dimensions to use
for the image when the image is loading. To avoid the layout having to be reflowed multiple times
as the page is loading, CSS and CSS media queries can be used to provide the dimensions:</p>
<pre><figure>
<style scoped>
#a { width: 300px; height: 150px; }
@media (max-width: 600px) { #a { width: 100px; height: 100px; } }
</style>
<picture>
<source srcset="a-square.png" media="(max-width: 600px)">
<img src="a-rectangle.png" alt="Barney Frank wears a suit and glasses." id="a">
</picture>
<figcaption>Barney Frank, 2011</figcaption>
</figure></pre>
<p>Alternatively, the <code data-x="attr-dim-width">width</code> and <code
data-x="attr-dim-height">height</code> attributes can be used to provide the width for legacy user
agents, using CSS just for the user agents that support <code>picture</code>:</p>
<pre><figure>
<style scoped media="(max-width: 600px)">
#a { width: 100px; height: 100px; }
</style>
<picture>
<source srcset="a-square.png" media="(max-width: 600px)">
<img src="a-rectangle.png" width="300" height="150"
alt="Barney Frank wears a suit and glasses." id="a">
</picture>
<figcaption>Barney Frank, 2011</figcaption>
</figure></pre>
<hr>
<p>The <code>img</code> element is used with the <code
data-x="attr-img-src">src</code> attribute, which gives the URL of the image to use for legacy user
agents that do not support the <code>picture</code> element. This leads
to a question of which image to provide in the <code data-x="attr-img-src">src</code>
attribute.</p>
<p>If the author wants the biggest image in legacy user agents, the markup could be as follows:</p>
<pre><picture>
<source srcset="pear-mobile.jpeg" media="(max-width: 720px)">
<source srcset="pear-tablet.jpeg" media="(max-width: 1280px)">
<img src="pear-desktop.jpeg" alt="The pear is juicy.">
</picture></pre>
<p>However, if legacy mobile user agents are more important, one can list all three images in the
<code data-x="picture-source">source</code> elements, overriding the <code
data-x="attr-img-src">src</code> attribute entirely.</p>
<pre><picture>
<source srcset="pear-mobile.jpeg" media="(max-width: 720px)">
<source srcset="pear-tablet.jpeg" media="(max-width: 1280px)">
<source srcset="pear-desktop.jpeg">
<img src="pear-mobile.jpeg" alt="The pear is juicy.">
</picture></pre>
<p>Since at this point the <code data-x="attr-img-src">src</code> attribute is actually being
ignored entirely by <code>picture</code>-supporting user agents, the <code
data-x="attr-img-src">src</code> attribute can default to any image, including one that is neither
the smallest nor biggest:</p>
<pre><picture>
<source srcset="pear-mobile.jpeg" media="(max-width: 720px)">
<source srcset="pear-tablet.jpeg" media="(max-width: 1280px)">
<source srcset="pear-desktop.jpeg">
<img src="pear-tablet.jpeg" alt="The pear is juicy.">
</picture></pre>
<hr>
<p>Above the <code data-x="">max-width</code> media feature is used, giving the maximum
(viewport) dimensions that an image is intended for.
It is also possible to use <code data-x="">min-width</code> instead.</p>
<pre><picture>
<source srcset="pear-desktop.jpeg" media="(min-width: 1281px)">
<source srcset="pear-tablet.jpeg" media="(min-width: 721px)">
<img src="pear-mobile.jpeg" alt="The pear is juicy.">
</picture></pre>
<h4>Dependencies</h4>
<dl>
<dt>Media Queries <ref spec=MQ></dt>
<dd><dfn><media-condition></dfn></dd>
<dt>CSS Values and Units <ref spec=CSSVALUES></dt>
<dd><dfn><length></dfn></dd>
<dt>CSS Syntax <ref spec=CSSSYNTAX></dt>
<dd><dfn>Parse a comma-separated list of component values</dfn></dd>
<dd><dfn>component value</dfn></dd>
<dd><dfn><whitespace-token></dfn></dd>
<dt>Fetch <ref spec=FETCH></dt>
<dd><dfn data-x="concept-request-context">context</dfn></dd>
</dl>
<h4>The <dfn><code>picture</code></dfn> element</h4>
<dl class="element">
<dt><span data-x="element-dfn-categories">Categories</span>:</dt>
<dd><span>Flow content</span>.</dd>
<dd><span>Phrasing content</span>.</dd>
<dd><span>Embedded content</span>.</dd>
<dt><span data-x="element-dfn-contexts">Contexts in which this element can be used</span>:</dt>
<dd>Where <span>embedded content</span> is expected.</dd>
<dt><span data-x="element-dfn-content-model">Content model</span>:</dt>
<dd>Zero or more <code data-x="picture-source">source</code> elements, followed by one <code>img</code> element, optionally intermixed with <span>script-supporting elements</span>.</dd>
<dt><span data-x="element-dfn-attributes">Content attributes</span>:</dt>
<dd><span>Global attributes</span></dd>
<dt><span data-x="element-dfn-dom">DOM interface</span>:</dt><!--TOPIC:DOM APIs-->
<dd>
<pre class="idl">interface <dfn>HTMLPictureElement</dfn> : <span>HTMLElement</span> {};</pre>
</dd>
</dl>
<p>The <code>picture</code> element is a container
which provides multiple sources to its contained <code>img</code> element
to allow authors to declaratively control or give hints to the user agent about which image resource to use,
based on the screen pixel density, viewport size, image format, and other factors. It <span>represents</span> its children.</p>
<p class="note">The <code>picture</code> element is somewhat different
from the similar-looking <code>video</code> and <code>audio</code> elements.
While all of them contain <code data-x="picture-source">source</code> elements,
the <code data-x="picture-source">source</code> element's <code data-x="attr-source-src">src</code> attribute has no meaning
when the element is nested within a <code>picture</code> element,
and the resource selection algorithm is different.
As well, the <code>picture</code> element itself does not display anything;
it merely provides a context for its contained <code>img</code> element
that enables it to choose from multiple <span data-x="URL">URLs</span>.</p>
<h4>The <dfn data-x="picture-source"><code>source</code></dfn> element when used with the <code>picture</code> element</h4>
<dl class="element">
<dt><span data-x="element-dfn-categories">Categories</span>:</dt>
<dd>Same as for the <code>source</code> element.</dd>
<dt><span data-x="element-dfn-contexts">Contexts in which this element can be used</span>:</dt>
<dd>As a child of a <code>picture</code> element, before the <code>img</code> element.</dd>
<dt><span data-x="element-dfn-content-model">Content model</span>:</dt>
<dd>Same as for the <code>source</code> element.</dd>
<dt><span data-x="element-dfn-attributes">Content attributes</span>:</dt>
<dd><span>Global attributes</span></dd>
<dd><code data-x="attr-picture-source-srcset">srcset</code></dd>
<dd><code data-x="attr-picture-source-sizes">sizes</code></dd>
<dd><code data-x="attr-picture-source-media">media</code></dd>
<dd><code data-x="attr-picture-source-type">type</code></dd>
<dt><span data-x="element-dfn-dom">DOM interface</span>:</dt><!--TOPIC:DOM APIs-->
<dd>
<pre class="idl">partial interface <span>HTMLSourceElement</span> {
attribute DOMString <span data-x="dom-picture-source-srcset">srcset</span>;
attribute DOMString <span data-x="dom-picture-source-sizes">sizes</span>;
attribute DOMString <span data-x="dom-picture-source-media">media</span>;
};</pre>
</dd>
</dl>
<p>The authoring requirements in this section only apply if the <code data-x="picture-source">source</code> element has
a parent that is a <code>picture</code> element.</p>
<p>The <code data-x="picture-source">source</code> element allows authors to specify multiple alternative
<span data-x="source set">source sets</span> for <code>img</code> elements.
It does not <span data-x="represents">represent</span> anything on its own.</p>
<p>The <dfn data-x="attr-picture-source-srcset"><code>srcset</code></dfn> attribute must be present,
and must consist of one or more <span data-x="image candidate string">image candidate strings</span>,
each separated from the next by a U+002C COMMA character (,).
If an <span>image candidate string</span> contains no descriptors
and no <span data-x="space character">space characters</span> after the URL,
the following <span>image candidate string</span>, if there is one,
must begin with one or more <span data-x="space character">space characters</span>.</p>
<p>If the <code data-x="attr-picture-source-srcset">srcset</code> attribute has any
<span data-x="image candidate string">image candidate strings</span> using a <i>width descriptor</i>,
the <dfn data-x="attr-picture-source-sizes"><code>sizes</code></dfn> attribute must also be present,
and the value must be a <span>valid source size list</span>.</p>
<p>The <dfn data-x="attr-picture-source-media"><code>media</code></dfn> attributes may also be present.
If present, the value must contain a <span>valid media query list</span>.</p>
<p>The <dfn data-x="attr-picture-source-type"><code>type</code></dfn> attribute may also be present.
If present, the value must be a <span>valid MIME type</span>.
It gives the type of the images in the <span>source set</span>,
to allow the user agent to skip to the next <code data-x="picture-source">source</code> element
if it does not support the given type.</p>
<p class="note">If the <code data-x="attr-picture-source-type">type</code> attribute
is <em>not</em> specified, the user agent will not select a different
<code data-x="picture-source">source</code> element if it finds that it does not support
the image format after fetching it.</p>
<p>When a <code data-x="picture-source">source</code> element has a following sibling
<code data-x="picture-source">source</code> element or <code>img</code> element with a
<code data-x="attr-img-srcset">srcset</code> attribute specified, it must have
at least one of the following:</p>
<ul>
<li><p>A <code data-x="attr-picture-source-media">media</code> attribute specified with a value that,
after <span data-x="strip leading and trailing whitespace">stripping leading and trailing whitespace</span>,
is not the empty string and is not an <span>ASCII case-insensitive</span> match for the string "<code data-x="">all</code>".</p></li>
<li><p>A <code data-x="attr-picture-source-type">type</code> attribute specified.</p></li>
</ul>
<p>The <code data-x="attr-source-src">src</code> attribute must not be present.</p>
<div w-nodev>
<p>The IDL attributes <dfn data-x="dom-picture-source-srcset"><code>srcset</code></dfn>,
<dfn data-x="dom-picture-source-sizes"><code>sizes</code></dfn> and
<dfn data-x="dom-picture-source-media"><code>media</code></dfn> must <span>reflect</span> the
respective content attributes of the same name.</p>
</div>
<h4>The <dfn><code>img</code></dfn> element</h4>
<dl class="element">
<dt><span data-x="element-dfn-categories">Categories</span>:</dt>
<dd><span>Flow content</span>.</dd>
<dd><span>Phrasing content</span>.</dd>
<dd><span>Embedded content</span>.</dd>
<dd><span>Form-associated element</span>.</dd>
<dd>If the element has a <code data-x="attr-hyperlink-usemap">usemap</code> attribute: <span>Interactive content</span>.</dd>
<dd><span>Palpable content</span>.</dd>
<dt><span data-x="element-dfn-contexts">Contexts in which this element can be used</span>:</dt>
<dd>Where <span>embedded content</span> is expected.</dd>
<dt><span data-x="element-dfn-content-model">Content model</span>:</dt>
<dd>Empty.</dd>
<dt><span data-x="element-dfn-attributes">Content attributes</span>:</dt>
<dd><span>Global attributes</span></dd>
<dd><code data-x="attr-img-alt">alt</code></dd>
<dd><code data-x="attr-img-src">src</code></dd>
<dd><code data-x="attr-img-srcset">srcset</code></dd>
<dd><code data-x="attr-img-sizes">sizes</code></dd>
<dd><code data-x="attr-img-crossorigin">crossorigin</code></dd>
<dd><code data-x="attr-hyperlink-usemap">usemap</code></dd>
<dd><code data-x="attr-img-ismap">ismap</code></dd>
<dd><code data-x="attr-dim-width">width</code></dd>
<dd><code data-x="attr-dim-height">height</code></dd>
<dt><span data-x="element-dfn-dom">DOM interface</span>:</dt><!--TOPIC:DOM APIs-->
<dd>
<pre class="idl">[NamedConstructor=<span data-x="dom-image">Image</span>(optional unsigned long width, optional unsigned long height)]
interface <dfn>HTMLImageElement</dfn> : <span>HTMLElement</span> {
attribute DOMString <span data-x="dom-img-alt">alt</span>;
attribute DOMString <span data-x="dom-img-src">src</span>;
attribute DOMString <span data-x="dom-img-srcset">srcset</span>;
attribute DOMString <span data-x="dom-img-sizes">sizes</span>;
attribute DOMString? <span data-x="dom-img-crossOrigin">crossOrigin</span>;
attribute DOMString <span data-x="dom-img-useMap">useMap</span>;
attribute boolean <span data-x="dom-img-isMap">isMap</span>;
attribute unsigned long <span data-x="dom-img-width">width</span>;
attribute unsigned long <span data-x="dom-img-height">height</span>;
readonly attribute unsigned long <span data-x="dom-img-naturalWidth">naturalWidth</span>;
readonly attribute unsigned long <span data-x="dom-img-naturalHeight">naturalHeight</span>;
readonly attribute boolean <span data-x="dom-img-complete">complete</span>;
readonly attribute DOMString <span data-x="dom-img-currentSrc">currentSrc</span>;
};</pre>
</dd>
</dl><!--TOPIC:HTML-->
<p>An <code>img</code> element represents an image.</p>
<!-- v2 ideas for <img>:
* Maps sites would like to know which images are already cached, so that they can use images
that are vaguely suitable while they wait for the most appropriate image to download.
Almost like lowsrc="", except that many images might be appropriate.
Slight hitch: their images are at a different origin, and we don't want to allow arbitrary
cross-origin inspection (privacy leak risk). So it will require them to do CORS opt-in.
* See note at rel=noreferrer.
-->
<p>The image given by the <dfn data-x="attr-img-src"><code>src</code></dfn> and <dfn
data-x="attr-img-srcset"><code>srcset</code></dfn> attributes,
and any previous sibling <code data-x="picture-source">source</code> elements'
<code data-x="attr-picture-source-srcset">srcset</code> attributes if the parent is a <code>picture</code> element,
is the embedded content; the value of
the <dfn data-x="attr-img-alt"><code>alt</code></dfn> attribute provides equivalent content for
those who cannot process images or who have image loading disabled (i.e. it is the
<code>img</code> element's <span>fallback content</span>).</p>
<p>The requirements on the <code data-x="attr-img-alt">alt</code> attribute's value are described
<a href="#alt">in the next section</a>.</p>
<p>The <code data-x="attr-img-src">src</code> attribute must be present, and must contain a
<span>valid non-empty URL potentially surrounded by spaces</span> referencing a non-interactive,
optionally animated, image resource that is neither paged nor scripted.</p>
<p>The <code data-x="attr-img-srcset">srcset</code> attribute may also be present.
If present, its value must consist of one or more
<span data-x="image candidate string">image candidate strings</span>,
each separated from the next by a U+002C COMMA character (,).
If an <span>image candidate string</span> contains no descriptors
and no <span data-x="space character">space characters</span> after the URL,
the following <span>image candidate string</span>, if there is one,
must begin with one or more <span data-x="space character">space characters</span>.</p>
<!-- XXXX
<div class="note">
<p>The <code data-x="attr-img-srcset">srcset</code> attribute allows authors to provide a set of
images to handle graphical displays of varying dimensions and pixel densities.</p>
<p>The attribute essentially takes a comma-separated list of URLs each with one or more
descriptors giving the maximum viewport dimensions and pixel density allowed to use the image.
From the available options, the user agent then picks the most appropriate image. If the viewport
dimensions or pixel density changes, the user agent can replace the image data with a new image
on the fly.</p>
<p>To specify an image, give first a URL, then one or more descriptors of the form 100w, 100h, or
2x, where "100w" means "maximum viewport width of 100 CSS pixels", "100h" is the same but for
height, and "2x" means "maximum pixel density of 2 device pixels per CSS pixel".</p>
</div>
-->
<p>An <dfn>image candidate string</dfn> consists of the following components, in order, with the
further restrictions described below this list:</p>
<ol>
<li><p>Zero or more <span data-x="space character">space characters</span>.</p></li>
<li><p>A <span>valid non-empty URL</span> that does not start or end with a U+002C COMMA character (,),
referencing a non-interactive, optionally animated, image resource
that is neither paged nor scripted.</p></li>
<li><p>Zero or more <span data-x="space character">space characters</span>.</p></li>
<li>
<p>Zero or one of the following:</p>
<ul>
<li><p>A <i>width descriptor</i>, consisting of:
a <span>space character</span>,
a <span>valid non-negative integer</span> giving a number greater than zero
representing the <i>width descriptor</i> value,
and a U+0077 LATIN SMALL LETTER W character.</p></li>
<li><p>A <i>pixel density descriptor</i>, consisting of:
a <span>space character</span>,
a <span>valid floating-point number</span> giving a number greater than zero
representing the <i>pixel density descriptor</i> value,
and a U+0078 LATIN SMALL LETTER X character.</p></li>
</ul>
</li>
<li><p>Zero or more <span data-x="space character">space characters</span>.</p></li>
</ol>
<p>There must not be an <span>image candidate string</span> for an element that
has the same <i>width descriptor</i> value as another
<span>image candidate string</span>'s <i>width descriptor</i> value for the same element.</p>
<p>There must not be an <span>image candidate string</span> for an element that
has the same <i>pixel density descriptor</i> value as another
<span>image candidate string</span>'s <i>pixel density descriptor</i> value for the same element.
For the purpose of this requirement,
an <span>image candidate string</span> with no descriptors is equivalent to
an <span>image candidate string</span> with a <code data-x="">1x</code> descriptor.</p>
<p>If a <code data-x="picture-source">source</code> element has a <code data-x="attr-picture-source-sizes">sizes</code> attribute present
or an <code>img</code> element has a <code data-x="attr-img-sizes">sizes</code> attribute present,
all <span data-x="image candidate string">image candidate strings</span> for that
element must have the <i>width descriptor</i> specified.</p>
<p>If an <span>image candidate string</span> for an <code data-x="picture-source">source</code> or <code>img</code> element
has the <i>width descriptor</i> specified,
all other <span data-x="image candidate string">image candidate strings</span> for that element
must also have the <i>width descriptor</i> specified.</p>
<p>The specified width in an <span>image candidate string</span>'s <i>width descriptor</i>
must match the intrinsic width in the resource given by the <span>image candidate string</span>'s URL,
if it has an intrinsic width.</p>
<p class="note">The requirements above imply that images can be static bitmaps (e.g. PNGs, GIFs,
JPEGs), single-page vector documents (single-page PDFs, XML files with an SVG root element),
animated bitmaps (APNGs, animated GIFs), animated vector graphics (XML files with an SVG root
element that use declarative SMIL animation), and so forth. However, these definitions preclude
SVG files with script, multipage PDF files, interactive MNG files, HTML documents, plain text
documents, and so forth. <ref spec=PNG> <ref spec=GIF> <ref spec=JPEG> <ref spec=PDF>
<ref spec=XML> <ref spec=APNG> <!-- <ref spec=AGIF> --> <ref spec=SVG> <ref spec=MNG> </p>
<p>If the <code data-x="attr-img-srcset">srcset</code> attribute is present and has any
<span data-x="image candidate string">image candidate strings</span> using a <i>width descriptor</i>,
the <dfn data-x="attr-img-sizes"><code>sizes</code></dfn> attribute must also be present,
and the value must be a <span>valid source size list</span>.</p>
<p>A <dfn>valid source size list</dfn> is a string that matches the following grammar:
<ref spec=CSSVALUES> <ref spec=MQ></p>
<pre><dfn><source-size-list></dfn> = [ <span><source-size></span># , ]? <span><source-size-value></span>
<dfn><source-size></dfn> = <span><media-condition></span> <span><source-size-value></span>
<dfn><source-size-value></dfn> = <span><length></span></pre>
<p>A <span><source-size-value></span> must not be negative.</p>
<p class="note">Percentages are not allowed in a <span><source-size-value></span>,
to avoid confusion about what it would be relative to.
The <code data-x="">vw</code> unit can be used for sizes relative to the viewport width.</p>
<p>The <code>img</code> element must not be used as a layout tool. In particular, <code>img</code>
elements should not be used to display transparent images, as such images rarely convey meaning and
rarely add anything useful to the document.</p>
<hr>
<p>The <dfn data-x="attr-img-crossorigin"><code>crossorigin</code></dfn> attribute is a <span>CORS
settings attribute</span>. Its purpose is to allow images from third-party sites that allow
cross-origin access to be used with <code>canvas</code>.</p>
<div w-nodev>
<hr>
<p>An <code>img</code> element has a <dfn>current request</dfn> and a <dfn>pending request</dfn>.
The <span>current request</span> is initially set to a new <span>image request</span>.
The <span>pending request</span> is initially set to null.
The <span>current request</span> is usually referred to as the <code>img</code> element itself.</p>
<p>An <dfn>image request</dfn> has a <dfn data-x="img-req-state">state</dfn>, <dfn data-x="img-req-url">current URL</dfn> and <dfn data-x="img-req-data">image data</dfn>.</p>
<p>An <span>image request</span>'s <span data-x="img-req-state">state</span> is one of the following:</p>
<dl>
<dt><dfn data-x="img-none">Unavailable</dfn></dt>
<dd>The user agent hasn't obtained any image data,
or has obtained some or all of the image data but
hasn't yet decoded enough of the image to get the image dimensions.</dd>
<dt><dfn data-x="img-inc">Partially available</dfn></dt>
<dd>The user agent has obtained some of the image data and at least the image dimensions are
available.</dd>
<dt><dfn data-x="img-all">Completely available</dfn></dt>
<dd>The user agent has obtained all of the image data and at least the image dimensions are
available.</dd>
<dt><dfn data-x="img-error">Broken</dfn></dt>
<dd>The user agent has obtained all of the image data that it can, but it cannot even decode the
image enough to get the image dimensions (e.g. the image is corrupted, or the format is not
supported, or no data could be obtained).</dd>
</dl>
<p>An <span>image request</span>'s <span data-x="img-req-url">current URL</span> is initially the empty string.</p>
<p>An <span>image request</span>'s <span data-x="img-req-data">image data</span> is the decoded image data.</p>
<p>When an <span>image request</span> is either in the <span data-x="img-inc">partially
available</span> state or in the <span data-x="img-all">completely available</span> state, it is
said to be <dfn data-x="img-available">available</dfn>.</p>
<p>An <span>image request</span> is initially <span data-x="img-none">unavailable</span>.</p>
<p>When an <code>img</code> element is <span data-x="img-available">available</span>,
it <span>provides a paint source</span>
whose width is the image's <span data-x="density-corrected intrinsic width and height">density-corrected intrinsic width</span> (if any),
whose height is the image's <span data-x="density-corrected intrinsic width and height">density-corrected intrinsic height</span> (if any),
and whose appearance is the intrinsic appearance of the image.</p>
<p>In a <span>browsing context</span> where <span data-x="concept-bc-noscript">scripting is
disabled</span>, user agents may obtain images immediately or on demand. In a <span>browsing
context</span> where <span data-x="concept-bc-noscript">scripting is enabled</span>, user agents
must obtain images immediately.</p>
<p>A user agent that obtains images immediately must synchronously
<span>update the image data</span> of an <code>img</code> element,
with the <i>restart animation</i> flag set if so stated,
whenever that element is created or has experienced <span>relevant mutations</span>.</p>
<p>A user agent that obtains images on demand must <span>update the image data</span> of an
<code>img</code> element whenever it needs the image data (i.e. on demand),
but only if the <code>img</code> element is in the
<span data-x="img-none">unavailable</span> state. When an <code>img</code> element
has experienced <span>relevant mutations</span>, if the user
agent only obtains images on demand, the <code>img</code> element must return to the <span
data-x="img-none">unavailable</span> state.</p>
<p>The <dfn>relevant mutations</dfn> for an <code>img</code> element are as follows:</p>
<ul>
<li><p>The element's <code data-x="attr-img-src">src</code>,
<code data-x="attr-img-srcset">srcset</code>
or <code data-x="attr-img-sizes">sizes</code> attributes are set, changed, or removed.</p></li>
<li><p>The element's <code data-x="attr-img-src">src</code> attribute is set to the same value as the previous value.
This must set the <i>restart animation</i> flag for the <span>update the image data</span> algorithm.</p></li>
<li><p>The element's <code data-x="attr-img-crossorigin">crossorigin</code> attribute's state is changed.</p></li>
<li><p>The <span data-x="nodes are inserted">element is inserted</span> into or
<span data-x="nodes are removed">removed</span> from a <code>picture</code> parent element.</p></li>
<li><p>The element's parent is a <code>picture</code> element and a
<code data-x="picture-source">source</code> <span data-x="nodes are inserted">element is inserted</span> as a previous sibling.</p></li>
<li><p>The element's parent is a <code>picture</code> element and a
<code data-x="picture-source">source</code> element that was a previous sibling is <span data-x="nodes are removed">removed</span>.</p></li>
<li><p>The element's parent is a <code>picture</code> element and a
<code data-x="picture-source">source</code> element that is a previous sibling has its
<code data-x="attr-picture-source-srcset">srcset</code>,
<code data-x="attr-picture-source-sizes">sizes</code>,
<code data-x="attr-picture-source-media">media</code>
or <code data-x="attr-picture-source-type">type</code> attributes set, changed, or removed.</p></li>
<li><p>The element's <span data-x="concept-node-adopt-ext">adopting steps</span> are run.</p></li>
<!-- NOT when the base URL changes (except for when adopted into a new doc) -->
</ul>
<p>Each <code>img</code> element has a <dfn>last selected source</dfn>, which must initially be
null.</p>
<p>Each <span>image request</span> has a <dfn>current pixel density</dfn>, which must initially be undefined.</p>
<p>When an <code>img</code> element has a <span>current pixel density</span> that is not 1.0, the
element's image data must be treated as if its resolution, in device pixels per CSS pixels, was
the <span>current pixel density</span>.
The image's <dfn>density-corrected intrinsic width and height</dfn> are the intrinsic width and height
after taking into account the <span>current pixel density</span>.</p>
<p class="example">For example, if the <span>current pixel density</span> is 3.125, that means
that there are 300 device pixels per CSS inch, and thus if the image data is 300x600, it has an
intrinsic dimension of 96 CSS pixels by 192 CSS pixels.</p>
<p>Each <code>Document</code> object must have a <dfn>list of available images</dfn>. Each image
in this list is identified by a tuple consisting of an <span>absolute URL</span>, a <span>CORS
settings attribute</span> mode, and, if the mode is not <span data-x="attr-crossorigin-none">No
CORS</span>, an <span>origin</span>.
Each image furthermore has an <dfn>ignore higher-layer caching</dfn> flag.
User agents may copy entries from one <code>Document</code>
object's <span>list of available images</span> to another at any time (e.g. when the
<code>Document</code> is created, user agents can add to it all the images that are loaded in
other <code>Document</code>s), but must not change the keys of entries copied in this way when
doing so, and must unset the <span>ignore higher-layer caching</span> flag for the copied entry.
User agents may also remove images from such lists at any time (e.g. to save
memory).
User agents must remove entries in the <span>list of available images</span> as appropriate
given higher-layer caching semantics for the resource (e.g. the HTTP <code data-x="">Cache-Control</code>
response header) when the <span>ignore higher-layer caching</span> is unset.</p>
<p class="note">The <span>list of available images</span> is intended to enable synchronous switching
when changing the <code data-x="attr-img-src">src</code> attribute to a URL that has previously been loaded,
and to avoid re-downloading images in the same document even when they don't allow caching per HTTP.
It is not used to avoid re-downloading the same image while the previous image is still loading,
but the <span>fetch</span> algorithm allows the download to be reused in that case.</p>
<p class="note">The user agent can also store the image data separately from the <span>list of available images</span>.</p>
<p class="example">For example, if a resource has the HTTP response header <code data-x="">Cache-Control: must-revalidate</code>,
the user agent would remove it from the <span>list of available images</span> but could keep the image data separately,
and use that if the server responds with a <code data-x="">304 Not Modified</code> status.</p>
<p>When the user agent is to <dfn>update the image data</dfn> of an <code>img</code> element,
optionally with the <i>restart animations</i> flag set,
it must run the following steps:</p>
<ol>
<li>
<p>If the element's <span>node document</span> is not the <span>active document</span>,
then run these substeps:</p>