-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathOverview.bs
2092 lines (1666 loc) · 102 KB
/
Overview.bs
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
<pre class='metadata'>
Title: CSS Image Values and Replaced Content Module Level 3
Status: ED
Shortname: css-images
Level: 3
Group: csswg
ED: https://drafts.csswg.org/css-images-3/
TR: https://www.w3.org/TR/css3-images/
Work Status: Testing
Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/
Editor: Elika J. Etemad / fantasai, Invited Expert, http://fantasai.inkedblade.net/contact
Abstract: This module contains the features of CSS level 3 relating to the <<image>> type and replaced elements. It includes and extends the functionality of CSS level 2 [[CSS21]]. The main extensions compared to CSS2.1 are the generalization of the <<url>> type to the <<image>> type, several additions to the <<image>> type, a generic sizing algorithm for images and other replaced content in CSS, definitions for interpolating several <<image>> types, and several properties controlling the interaction of replaced elements and CSS's layout models.
Issue Tracking: Tracker http://www.w3.org/Style/CSS/Tracker/products/27
Previous Version: https://www.w3.org/TR/2012/CR-css3-images-20120417/
Previous Version: https://www.w3.org/TR/2012/WD-css3-images-20120112/
Ignored Terms: <offset>, background positioning area, border image area, <meetorslice>, <ending-shape>, Map, center, content
Link Defaults: css21 (property) display
</pre>
Introduction {#intro}
=====================
<em>This section is not normative.</em>
In CSS Levels 1 and 2,
image values, such as those used in the 'background-image' property,
could only be given by a single URL value.
This module introduces additional ways of representing 2D images,
for example as <a section href="#image-notation">a URL with color fallback</a>
or as <a section href="#gradients">a gradient</a>.
This module also defines several properties for <a section href="#image-processing">manipulating raster images</a>
and for <a section href="#the-object-fit">sizing</a>
or <a section href="#the-object-position">positioning</a>
replaced elements such as images within the box determined by the CSS layout algorithms.
It also defines in a generic way CSS's <a section href="#sizing">sizing algorithm</a> for images and other replaced elements.
<!--
██ ████ ██ ██ ███ ██████ ████████ ██
██ ██ ███ ███ ██ ██ ██ ██ ██ ██
██ ██ ████ ████ ██ ██ ██ ██ ██
██ ██ ██ ███ ██ ██ ██ ██ ████ ██████ ██
██ ██ ██ ██ █████████ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ██ ██ ██ ██ ██████ ████████ ██
-->
Image Values: the <<image>> type {#image-values}
================================================
The <<image>> value type denotes a 2D image. It can be a
<a section href="#url-notation">url reference</a>,
<a section href="#image-notation">image notation</a>,
or <a section href="#gradients">gradient notation</a>.
Its syntax is:
<pre class="prod"><dfn><image></dfn> = <<url>> | <<image()>> | <<image-set()>> | <<element()>> | <<cross-fade()>> | <<gradient>></pre>
An <<image>> can be used in many CSS properties,
including the 'background-image', 'list-style-image', 'cursor' properties [[!CSS21]]
(where it replaces the <<url>> component in the property's value).
In some cases, an image is invalid,
such as a <<url>> pointing to a resource that is not a valid image format.
An <dfn export lt="invalid image|valid image">invalid image</dfn> is rendered as a solid-color ''transparent'' image with no intrinsic dimensions.
However, <a>invalid images</a> have special behavior in some contexts,
such as the ''image()'' notation.
<!--
██ ██ ██ ████████ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ████████ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██
██ ███████ ██ ██ ████████ ██
-->
Image References and Image Slices: the <<url>> type and ''url()'' notation {#url-notation}
------------------------------------------------------------------------------------------
The simplest way to indicate an image is to reference an image file by URL.
This can be done with the ''url()'' notation, defined in [[css3val]].
<div class="example">
In the example below, a background image is specified with ''url()''syntax:
<pre>background-image: url(wavy.png);</pre>
</div>
If the UA cannot download, parse, or otherwise successfully display the contents at the URL as an image,
it must be treated as an <a>invalid image</a>.
<!--
████ ██ ██ ███ ██████ ████████ ███ ███
██ ███ ███ ██ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██
██ ██ ███ ██ ██ ██ ██ ████ ██████ ██ ██
██ ██ ██ █████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██████ ████████ ███ ███
-->
Image Fallbacks and Annotations: the ''image()'' notation {#image-notation}
---------------------------------------------------------------------------
The ''image()'' function allows an author to:
* use <a href="https://www.w3.org/TR/media-frags/">media fragments</a> to clip out a portion of an image
* use a solid color as an image
* fallback to a solid-color image, when the image at the specified url can't be downloaded or decoded
* automatically respect the image orientation specified in the image's metadata
The ''image()'' notation is defined as:
<pre class='prod'>
<dfn>image()</dfn> = image( [ [ <<image>> | <<string>> ]? , <<color>>? ]! )
</pre>
A <<string>> used in ''image()'' represents a <<url>>.
As usual for URLs in CSS,
relative URLs are resolved to an absolute URL
(as described in Values & Units [[!CSS3VAL]])
when a specified ''image()'' value is computed.
If the image has an orientation specified in its metadata,
such as EXIF,
the UA must rotate or flip the image to correctly orient it
as the metadata specifies.
### Image Fallbacks ### {#image-fallbacks}
If both a URL and a <<color>> are specified in ''image()'',
then whenever the URL represents an <a>invalid image</a>,
the ''image()'' function renders as if the URL were not specified at all;
it generates a solid-color image as specified in [[#color-images]].
<div class='example'>
The fallback color can be used to ensure that text is still readable
even when the image fails to load.
For example, the following legacy code works fine if the image is rectangular and has no transparency:
<pre>
body { color: black; background: white; }
p.special { color: white; background: url("dark.png") black; }
</pre>
When the image doesn't load,
the background color is still there to ensure that the white text is readable.
However, if the image has some transparency,
the black will be visible behind it,
which is probably not desired.
The ''image()'' function addresses this:
<pre>
body { color: black; background: white; }
p.special { color: white; background: image("dark.png", black); }
</pre>
Now, the black won't show at all if the image loads,
but if for whatever reason the image fails,
it'll pop in and prevent the white text from being set against a white background.
</div>
<!-- Good example for the fallback() function
<div class='example'>
For example, if a future specification defined a way to refer to a specific frame of an animated GIF with a fragment identifier,
an author could write the following to get newer browsers to use the GIF's frame,
and older browsers to instead download the fallback image:
<pre>background-image: image("cat_meme.gif#frame=5", "lolcat.png");</pre>
</div>
-->
### Image Fragments ### {#image-fragments}
When a URL specified in ''image()'' represents a portion of a resource
(e.g. by the use of <a href="https://www.w3.org/TR/media-frags/#naming-space">media fragment identifiers</a>)
that portion is clipped out of its context and used as a standalone image.
<div class="example">
For example, given the following image and CSS:
<a href="images/sprites.svg">
<img src="images/sprites.svg" height="20" width="180" alt="[9 circles, with 0 to 8 eighths filled in]">
</a>
<pre>background-image: image('sprites.svg#xywh=40,0,20,20')</pre>
...the background of the element will be the portion of the image that starts at (40px,0px) and is 20px wide and tall,
which is just the circle with a quarter filled in.
</div>
So that authors can take advantage of CSS's forwards-compatible parsing rules to provide a fallback for image slices,
implementations that support the ''image()'' notation
<em>must</em> support the <code>xywh=#,#,#,#</code> form of media fragment identifiers
for images specified via ''image()''. [[!MEDIA-FRAGS]]
<div class='example'>
Note that image fragments can also be used with the ''url()'' notation.
However, a legacy UA that doesn't understand the media fragments notation
will ignore the fragment and simply display the entirety of the image.
Since the ''image()'' notation requires UAs to support media fragments,
authors can take advantage of CSS's forward-compatible parsing rules
to provide a fallback when using an image fragment URL:
<pre>
background-image: url('swirl.png'); /* old UAs */
background-image: image('sprites.png#xywh=10,30,60,20'); /* new UAs */
</pre>
</div>
If a URL uses a fragment identifier syntax that the implementation does not understand,
or does not consider valid for that type of image,
the URL must be treated as representing an <a>invalid image</a>.
Note: This error-handling is limited to ''image()'',
and not in the definition of URL,
for legacy compat reasons.
### Solid-color Images ### {#color-images}
If the ''image()'' function is specified with only a <<color>> argument (no URL),
it represents a solid-color image of the specified color with no intrinsic dimensions.
<div class='example'>
For example,
one can use this as a simple way to "tint" a background image,
by overlaying a partially-transparent color over the top of the other image:
<pre>background-image: image(rgba(0,0,255,.5)), url("bg-image.png");</pre>
'background-color' does not work for this,
as the solid color it generates always lies <em>beneath</em> all the background images.
</div>
<!--
████ ██ ██ ███ ██████ ████████ ██████ ████████ ████████ ███ ███
██ ███ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████ ████ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ███ ██ ██ ██ ██ ████ ██████ ███████ ██████ ██████ ██ ██ ██
██ ██ ██ █████████ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████ ██ ██ ██ ██ ██████ ████████ ██████ ████████ ██ ███ ███
-->
Resolution Negotiation: the ''image-set()'' notation {#image-set-notation}
--------------------------------------------------------------------------
Delivering the most appropriate image resolution for a user's device can be a difficult task.
Ideally, images should be in the same resolution as the device they're being viewed in,
which can vary between users.
However, other factors can factor into the decision of which image to send;
for example, if the user is on a slow mobile connection,
they may prefer to receive lower-res images
rather than waiting for a large proper-res image to load.
The ''image-set()'' function allows an author to ignore most of these issues,
simply providing multiple resolutions of an image
and letting the UA decide which is most appropriate in a given situation.
Issue: This solution assumes that resolution is a proxy for filesize,
and therefore doesn't appropriately handle multi-resolution sets of vector images,
or mixing vector images with raster ones (e.g. for icons).
For example, use a vector for high-res,
pixel-optimized bitmap for low-res,
and same vector again for low-bandwidth (because it's much smaller, even though it's higher resolution).
The syntax for ''image-set()'' is:
<pre class='prod'>
<dfn>image-set()</dfn> = image-set( <<image-set-option>># )
<dfn><image-set-option></dfn> = [ <<image>> | <<string>> ] <<resolution>>
</pre>
Issue: We should add "w" and "h" dimensions as a possibility, and a "format()" function,
to match the functionality of HTML's <a element>picture</a>.
The ''image-set()'' function can not be nested inside of itself,
either directly
or indirectly
(as an argument to another <<image>> type).
Issue: Is this restriction needed?
Each <<string>> inside ''image-set()'' represents a <<url>>,
just like in ''image()''.
Every <<image-set-option>> in a given ''image-set()'' must have a different <<resolution>>,
or else the function is invalid.
UAs must make a UA-specific choice of which <<image-set-option>> to load,
based on whatever criteria they find relevant
(such as the resolution of the display,
connection speed,
etc).
The ''image-set()'' then represents the image associated with the URL of that choice.
The image's <a>intrinsic resolution</a> is the resolution associated with that choice.
UAs <strong>may</strong> change which <<image-set-option>> they wish to use for a given ''image-set()''
over the lifetime of the page,
if the criteria used to determine which option to choose change significantly enough to make it worthwhile in the UA's estimation.
<div class='example'>
This example shows how to use ''image-set()'' to provide an image in three versions:
a "normal" version,
a "high-res" version,
and an extra-high resolution version for use in high-quality printing
(as printers can have <em>extremely</em> high resolution):
<pre>
background-image: image-set( "foo.png" 1x,
"foo-2x.png" 2x,
"foo-print.png" 600dpi );
</pre>
</div>
<!--
██████ ████████ ███████ ██████ ██████ ████████ ███ ████████ ████████ ███ ███
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ████████ ██ ██ ██████ ██████ ███████ ██████ ██ ██ ██ ██ ██████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ █████████ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██████ ██ ██ ███████ ██████ ██████ ██ ██ ██ ████████ ████████ ███ ███
-->
Combining images: the ''cross-fade()'' notation {#cross-fade-function}
----------------------------------------------------------------------
When transitioning between images,
CSS requires a way to explicitly refer to the intermediate image
that is a combination of the start and end images.
This is accomplished with the ''cross-fade()'' function,
which indicates the two images to be combined
and how far along in the transition the combination is.
Note: Authors can also use the ''cross-fade()'' function for many simple image manipulations,
such as tinting an image with a solid color
or highlighting a particular area of the page by combining an image with a radial gradient.
The syntax for ''cross-fade()'' is defined as:
<pre class=prod>
<dfn>cross-fade()</dfn> = cross-fade( <<cf-mixing-image>> , <<cf-final-image>>? )
<dfn><cf-mixing-image></dfn> = <<percentage>>? && <<image>>
<dfn><cf-final-image></dfn> = <<image>> | <<color>>
</pre>
The function represents an image generated by
combining two images.
The <<percentage>> represents how much of the first image is retained
when it is blended with the second image.
The <<percentage>> must be between ''0%'' and ''100%'' inclusive;
any other value is invalid.
If omitted,
it defaults to the value ''50%''.
If the last argument is a <<color>>,
it represents a solid-color image with the same intrinsic dimensions as the first image
(as if it were an ''image()'' function with the color as its sole argument).
If omitted,
it defaults to the color ''transparent''.
More precisely,
given ''cross-fade(<var>p</var> <var>A</var>, <var>B</var>)'',
where <var>A</var> and <var>B</var> are images
and <var>p</var> is a percentage between 0% and 100%,
the function represents an image
with width equal to <code>width<sub>A</sub> × <var>p</var> + width<sub>B</sub> × (1-<var>p</var>)</code>
and height equal to <code>height<sub>A</sub> × <var>p</var> + height<sub>B</sub> × (1-<var>p</var>)</code>.
The contents of the image must be constructed by
first scaling <var>A</var> and <var>B</var> to the size of the generated image,
then applying <code>dissolve(<var>A</var>,<var>p</var>) plus dissolve(<var>B</var>,1-<var>p</var>)</code>.
The "dissolve()" function and "plus" compositing operator are defined in the literature by Porter-Duff. [[PORTERDUFF]]
<!--
██████ ████████ ███ ████████ ████ ████████ ██ ██ ████████ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██
██ ████ ████████ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ██ ██████
██ ██ ██ ██ █████████ ██ ██ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██████ ██ ██ ██ ██ ████████ ████ ████████ ██ ██ ██ ██████
-->
Gradients {#gradients}
======================
A gradient is an image that smoothly fades from one color to another.
These are commonly used for subtle shading in background images, buttons, and many other things.
The gradient notations described in this section allow an author to specify such an image in a terse syntax,
so that the UA can generate the image automatically when rendering the page.
The syntax of a <<gradient>> is:
<pre class=prod>
<dfn><gradient></dfn> =
<<linear-gradient()>> | <<repeating-linear-gradient()>> |
<<radial-gradient()>> | <<repeating-radial-gradient()>>
</pre>
<div class=example>
As with the other <<image>> types defined in this specification,
gradients can be used in any property that accepts images.
For example:
* <code>background: linear-gradient(white, gray);</code>
* <code>list-style-image: radial-gradient(circle, #006, #00a 90%, #0000af 100%, white 100%)</code>
</div>
A gradient is drawn into a box with the dimensions of the <a>concrete object size</a>,
referred to as the <dfn export>gradient box</dfn>.
However, the gradient itself has no <a>intrinsic dimensions</a>.
<div class='example'>
For example, if you use a gradient as a background,
by default the gradient will draw into a <a>gradient box</a> the size of the element's padding box.
If 'background-size' is explicitly set to a value such as ''100px 200px'',
then the <a>gradient box</a> will be 100px wide and 200px tall.
Similarly, for a gradient used as a 'list-style-image',
the box would be a 1em square,
which is the <a>default object size</a> for that property.
</div>
Gradients are specified by defining the <dfn>starting point</dfn> and <dfn>ending point</dfn>
of a <dfn export>gradient line</dfn>
(which, depending on the type of gradient,
may be technically a line, or a ray, or a spiral),
and then specifying colors at points along this line.
The colors are smoothly blended to fill in the rest of the line,
and then each type of gradient defines how to use the color of the <a>gradient line</a> to produce the actual gradient.
<!--
██ ████ ██ ██ ████████ ███ ████████
██ ██ ███ ██ ██ ██ ██ ██ ██
██ ██ ████ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██████ ██ ██ ████████
██ ██ ██ ████ ██ █████████ ██ ██
██ ██ ██ ███ ██ ██ ██ ██ ██
████████ ████ ██ ██ ████████ ██ ██ ██ ██
-->
Linear Gradients: the ''linear-gradient()'' notation {#linear-gradients}
------------------------------------------------------------------------
A linear gradient is created by specifying a straight <a>gradient line</a>,
and then several colors placed along that line.
The image is constructed by creating an infinite canvas
and painting it with lines perpendicular to the gradient line,
with the color of the painted line being the color of the gradient line where the two intersect.
This produces a smooth fade from each color to the next,
progressing in the specified direction.
<h4 class='no-toc' id='linear-gradient-syntax'>linear-gradient() syntax</h4>
The linear gradient syntax is:
<pre class=prod>
<dfn>linear-gradient()</dfn> = linear-gradient(
[ <<angle>> | to <<side-or-corner>> ]? ,
<<color-stop-list>>
)
<dfn><side-or-corner></dfn> = [left | right] || [top | bottom]
</pre>
The first argument to the function specifies the <a>gradient line</a>,
which gives the gradient a direction and determines how color-stops are positioned.
It may be omitted;
if so, it defaults to ''to bottom''.
The <a>gradient line's</a> direction may be specified in two ways:
: using angles
:: For the purpose of this argument,
''0deg'' points upward,
and positive angles represent clockwise rotation,
so ''90deg'' point toward the right.
: using keywords
:: If the argument is ''to top'', ''to right'', ''to bottom'', or ''to left'',
the angle of the <a>gradient line</a> is ''0deg'', ''90deg'', ''180deg'', or ''270deg'', respectively.
If the argument instead specifies a corner of the box such as ''to top left'',
the <a>gradient line</a> must be angled such that it points into the same quadrant as the specified corner,
and is perpendicular to a line intersecting the two neighboring corners of the <a>gradient box</a>.
<span class='note'>This causes a color-stop at 50% to intersect the two neighboring corners (see <a href='#corner-gradient-example'>example</a>).</span>
Starting from the center of the <a>gradient box</a>,
extend a line at the specified angle in both directions.
The ending point is the point on the <a>gradient line</a>
where a line drawn perpendicular to the <a>gradient line</a>
would intersect the corner of the <a>gradient box</a> in the specified direction.
The starting point is determined identically, but in the opposite direction.
<div class='note'>
Given:
* <var>A</var> the angle (in any quadrant) defining the gradient line’s direction such that 0 degrees points upwards and positive angles represent clockwise rotation,
* <var>W</var> the width of the gradient box,
* <var>H</var> the height of the gradient box,
The length of the gradient line (between the <a>starting point</a> and <a>ending point</a>) is:
<code>abs(<var>W</var> * sin(<var>A</var>)) + abs(<var>H</var> * cos(<var>A</var>))</code>
</div>
Note: It is expected that the next level of this module will provide the ability to define the gradient's direction relative to the current text direction and writing-mode.
<div class=example>
<div style="overflow: hidden">
<img style="float: right; margin-left: 1em;" src='images/gradient-diagram.png' alt="[An image showing a box with a background shading gradually from white in the bottom-left corner to black in the top-right corner. There is a line, illustrating the gradient line, angled at 45 degrees and passing through the center of the box. The starting point and ending point of the gradient line are indicated by the intersection of the gradient line with two additional lines that pass through the bottom-left and top-right corners of the box.]">
This example illustrates visually how to calculate the <a>gradient line</a> from the rules above.
This shows the starting and ending point of the <a>gradient line</a>,
long with the actual gradient,
produced by an element with ''background: linear-gradient(45deg, white, black);''.
Notice how, though the starting point and ending point are outside of the box,
they're positioned precisely right so that the gradient is pure white <em>exactly</em> at the corner,
and pure black <em>exactly</em> at the opposite corner.
That's intentional, and will always be true for linear gradients.
</div>
</div>
<div class=note>
Given:
<ul>
<li>
<var>A</var> the angle defining the gradient line’s direction
such that 0 degrees points upwards and positive angles represent clockwise rotation,
<li><var>W</var> the width of the gradient box,
<li><var>H</var> the height of the gradient box,
</ul>
The length of the gradient line (between the starting and ending point) is:
<code>abs(W * sin(A)) + abs(H * cos(A))</code>
</div>
The gradient's color stops are typically placed between the starting point and ending point on the <a>gradient line</a>,
but this isn't required -
the <a>gradient line</a> extends infinitely in both directions.
The starting point and ending point are merely arbitrary location markers -
the starting point defines where 0%, 0px, etc are located when specifying color-stops,
and the ending point defines where 100% is located.
Color-stops are allowed to have positions before 0% or after 100%.
The color of the gradient at any point is determined
by finding the unique line passing through that point that is perpendicular to the <a>gradient line</a>.
The point's color is the color of the <a>gradient line</a>
at the point where this line intersects it.
<h4 class='no-toc' id='linear-gradient-examples'>Linear Gradient Examples</h4>
All of the following ''linear-gradient()'' examples are presumed to be backgrounds applied to a box that is 200px wide and 100px tall.
<div class=example>
Below are various ways of specifying a basic vertical gradient:
<pre>
linear-gradient(yellow, blue);
linear-gradient(to bottom, yellow, blue);
linear-gradient(180deg, yellow, blue);
linear-gradient(to top, blue, yellow);
linear-gradient(to bottom, yellow 0%, blue 100%);
</pre>
<img src="images/linear1.png" alt="" >
</div>
<div class=example>
This demonstrates the use of an angle in the gradient.
Note that, though the angle is not exactly the same as the angle between the corners,
the <a>gradient line</a> is still sized so as to make the gradient yellow exactly at the upper-left corner,
and blue exactly at the lower-right corner.
<pre>
linear-gradient(135deg, yellow, blue);
linear-gradient(-45deg, blue, yellow);
</pre>
<img src="images/linear3.png" alt="" >
</div>
<div class=example>
This demonstrates a 3-color gradient, and how to specify the location of a stop explicitly:
<pre>linear-gradient(yellow, blue 20%, #0f0);</pre>
<img src="images/linear4.png" alt="" >
</div>
<div class=example id='corner-gradient-example'>
This demonstrates a corner-to-corner gradient specified with keywords.
Note how the gradient is red and blue exactly in the bottom-left and top-right corners, respectively,
exactly like the second example.
Additionally, the angle of the gradient is automatically computed so that the color at 50% (in this case, white)
stretches across the top-left and bottom-right corners.
<pre>linear-gradient(to top right, red, white, blue)</pre>
<object data="images/gradient1.svg" width="200" height="100">(Image requires SVG)</object>
</div>
<!--
████████ ███ ████████ ████ ███ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
████████ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ █████████ ██ ██ ██ █████████ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██ ██ ████████ ████ ██ ██ ████████
-->
Radial Gradients: the ''radial-gradient()'' notation {#radial-gradients}
------------------------------------------------------------------------
In a radial gradient,
rather than colors smoothly fading from one side of the <a>gradient box</a> to the other
as with linear gradients,
they instead emerge from a single point
and smoothly spread outward in a circular or elliptical shape.
A radial gradient is specified by indicating the center of the gradient
(where the 0% ellipse will be)
and the size and shape of the <dfn>ending shape</dfn>
(the 100% ellipse).
Color stops are given as a list,
just as for ''linear-gradient()''.
Starting from the <a>gradient center</a>
and progressing towards (and potentially beyond) the <a>ending shape</a>
uniformly-scaled concentric ellipses are drawn and colored
according to the specified color stops.
<h4 class='no-toc' id='radial-gradient-syntax'>radial-gradient() Syntax</h4>
The radial gradient syntax is:
<pre>
<dfn>radial-gradient()</dfn> = radial-gradient(
[ <<ending-shape>> || <<size>> ]? [ at <<position>> ]? ,
<<color-stop-list>>
)
</pre>
<div class=example>
Here is an example of a circular radial gradient 5em wide
and positioned with its center in the top left corner:
<pre>radial-gradient(5em circle at top left, yellow, blue)</pre>
</div>
Issue: We should add the ability to move the focus of the gradient,
as in the original -webkit-gradient() function.
See proposal in <a href="http://lists.w3.org/Archives/Public/www-style/2011Nov/0210.html">http://lists.w3.org/Archives/Public/www-style/2011Nov/0210.html</a>
for "from <<position>>" and "from offset <<offset>>".
The arguments are defined as follows:
<dl dfn-type=value dfn-for="radial-gradient(), repeating-radial-gradient()">
<dt><dfn><<position>></dfn>
<dd>
Determines the <dfn dfn>gradient center</dfn> of the gradient.
The <<position>> value type
(which is also used for 'background-position')
is defined in [[!CSS3VAL]],
and is resolved using the center-point as the object area
and the <a>gradient box</a> as the positioning area.
If this argument is omitted,
it defaults to ''<position>/center''.
<dt><dfn><<ending-shape>></dfn>
<dd>
Can be either <dfn value for="<ending-shape>">circle</dfn> or <dfn value for="<ending-shape>">ellipse</dfn>;
determines whether the gradient's <a>ending shape</a> is a circle or an ellipse, respectively.
If <<ending-shape>> is omitted,
the <a>ending shape</a> defaults to a circle
if the <<size>> is a single <<length>>,
and to an ellipse otherwise.
<dt><dfn><<size>></dfn>
<dd>
Determines the size of the gradient's <a>ending shape</a>.
If omitted it defaults to ''farthest-corner''.
It can be given explicitly or by keyword.
For the purpose of the keyword definitions,
consider the <a>gradient box</a> edges as extending infinitely in both directions,
rather than being finite line segments.
If the ending-shape is an ellipse,
its axises are aligned with the horizontal and vertical axises.
Both ''circle'' and ''ellipse'' gradients accept the following keywords as their <dfn type for=''><size></dfn>:
<dl dfn-for="<size>">
<dt><dfn>closest-side</dfn>
<dd>
The <a>ending shape</a> is sized so that
it exactly meets the side of the <a>gradient box</a> closest to the gradient's center.
If the shape is an ellipse,
it exactly meets the closest side in each dimension.
<dt><dfn>farthest-side</dfn>
<dd>
Same as ''closest-side'',
except the <a>ending shape</a> is sized based on the farthest side(s).
<dt><dfn>closest-corner</dfn>
<dd>
The <a>ending shape</a> is sized so that
it passes through the corner of the <a>gradient box</a> closest to the gradient's center.
If the shape is an ellipse,
the <a>ending shape</a> is given the same aspect-ratio it would have if ''closest-side'' were specified.
<dt><dfn>farthest-corner</dfn>
<dd>
Same as ''closest-corner'',
except the <a>ending shape</a> is sized based on the farthest corner.
If the shape is an ellipse,
the <a>ending shape</a> is given the same aspect ratio it would have if ''farthest-side'' were specified.
</dl>
If <<ending-shape>> is specified as ''circle'' or is omitted,
the <<size>> may be given explicitly as:
<dl>
<dt><dfn for="<size>"><length></dfn>
<dd>
Gives the radius of the circle explicitly.
Negative values are invalid.
Note: Percentages are <em>not</em> allowed here;
they can only be used to specify the size of an elliptical gradient,
not a circular one.
This restriction exists because there is are multiple reasonable answers as to which dimension the percentage should be relative to.
A future level of this module may provide the ability to size circles with percentages,
perhaps with more explicit controls over which dimension is used.
</dl>
If <<ending-shape>> is specified as ''ellipse'' or is omitted,
<<size>> may instead be given explicitly as:
<dl>
<dt><dfn for="<size>">[ <<length>> | <<percentage>> ]{2}</dfn>
<dd>
Gives the size of the ellipse explicitly.
The first value represents the horizontal radius,
the second the vertical radius.
Percentages values are relative to the corresponding dimension of the <a>gradient box</a>.
Negative values are invalid.
</dl>
</dl>
<div class='note'>
Expanded with the above definitions,
the grammar becomes:
<pre>
radial-gradient() = radial-gradient(
[ [ circle || <<length>> ] [ at <<position>> ]? , |
[ ellipse || [ <<length>> | <<percentage>> ]{2} ] [ at <<position>> ]? , |
[ [ circle | ellipse ] || <<extent-keyword>> ] [ at <<position>> ]? , |
at <<position>></span> ,
]?
<<color-stop>> [ , <<color-stop>> ]+
)
<dfn noexport><extent-keyword></dfn> = closest-corner | closest-side | farthest-corner | farthest-side
</pre>
</div>
<h4 class="no-toc" id="radial-color-stops">Placing Color Stops</h4>
Color-stops are placed on a <a>gradient line</a> shaped like a ray
(a line that starts at one point, and extends infinitely in a one direction),
similar to the <a>gradient line</a> of linear gradients.
The <a>gradient line's</a> <a>starting point</a> is at the center of the gradient,
and it extends toward the right,
with the <a>ending point</a> on the point where the <a>gradient line</a> intersects the <a>ending shape</a>.
A color-stop can be placed at a location before 0%;
though the negative region of the <a>gradient line</a> is never directly consulted for rendering,
color stops placed there can affect the color of non-negative locations on the <a>gradient line</a> through interpolation or repetition
(see <a section href='#repeating-gradients'>repeating gradients</a>).
For example, ''radial-gradient(red -50px, yellow 100px)'' produces an elliptical gradient
that starts with a reddish-orange color in the center
(specifically, #f50)
and transitions to yellow.
Locations greater than 100% simply specify a location a correspondingly greater distance from the center of the gradient.
The color of the gradient at any point is determined by
first finding the unique ellipse passing through that point
with the same center, orientation, and ratio between major and minor axises as the ending-shape.
The point's color is then the color of the positive section of the <a>gradient line</a> at the location where this ellipse intersects it.
<h4 class="no-toc" id="degenerate-radials">Degenerate Radial Gradients</h4>
Some combinations of position, size, and shape
will produce a circle or ellipse with a radius of 0.
This will occur, for example,
if the center is on a <a>gradient box</a> edge and ''closest-side'' or ''closest-corner'' is specified
or if the size and shape are given explicitly and either of the radiuses is zero.
In these degenerate cases,
the gradient must be be rendered as follows:
: If the <a>ending shape</a> is a circle with zero radius:
:: Render as if the <a>ending shape</a> was
a circle whose radius was an arbitrary very small number greater than zero.
<span class='note'>This will make the gradient continue to look like a circle.</span>
: If the <a>ending shape</a> has zero width (regardless of the height):
:: Render as if the <a>ending shape</a> was
an ellipse whose height was an arbitrary very large number
and whose width was an arbitrary very small number greater than zero.
<span class='note'>This will make the gradient look similar to a horizontal linear gradient
that is mirrored across the center of the ellipse.
It also means that all color-stop positions specified with a percentage resolve to ''0px''.</span>
: Otherwise, if the <a>ending shape</a> has zero height:
:: Render as if the <a>ending shape</a> was
an ellipse whose width was an arbitrary very large number
and whose height was an arbitrary very small number greater than zero.
<span class='note'>This will make the gradient look like a solid-color image
equal to the color of the last color-stop,
or equal to the average color of the gradient if it's repeating.</span>
<h4 class='no-toc' id='radial-gradient-examples'>Radial Gradient Examples</h4>
All of the following examples are applied to a box that is 200px wide and 100px tall.
<div class=example>
These examples demonstrate different ways to write the basic syntax for radial gradients:
<pre>
radial-gradient(yellow, green);
radial-gradient(ellipse at center, yellow 0%, green 100%);
radial-gradient(farthest-corner at 50% 50%, yellow, green);
</pre>
<img src="images/radial1.png" alt="" >
<pre>radial-gradient(circle, yellow, green);</pre>
<img src="images/radial2.png" alt="" >
<pre>radial-gradient(red, yellow, green);</pre>
<img src="images/radial3.png" alt="" >
</div>
<div class=example>
This image shows a gradient originating from somewhere other than the center of the box:
<pre>radial-gradient(farthest-side at left bottom, red, yellow 50px, green);</pre>
<img src="images/radial4.png" alt="" >
</div>
<div class=example>
Here we illustrate a ''closest-side'' gradient.
<pre>
radial-gradient(closest-side at 20px 30px, red, yellow, green);
radial-gradient(20px 30px at 20px 30px, red, yellow, green);
</pre>
<img src="images/radial6.png" alt="" >
<pre>
radial-gradient(closest-side circle at 20px 30px, red, yellow, green);
radial-gradient(20px 20px at 20px 30px, red, yellow, green);
</pre>
<img src="images/radial7.png" alt="" >
</div>
<!--
████████ ████████ ████████ ████████ ███ ████████ ████ ██ ██ ██████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██
████████ ██████ ████████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ████
██ ██ ██ ██ ██ █████████ ██ ██ ██ ████ ██ ██
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██ ██
██ ██ ████████ ██ ████████ ██ ██ ██ ████ ██ ██ ██████
-->
Repeating Gradients: the ''repeating-linear-gradient()'' and ''repeating-radial-gradient()'' notations {#repeating-gradients}
------------------------------------------------------------------------------------------------------
In addition to ''linear-gradient()'' and ''radial-gradient()'',
this specification defines <dfn>repeating-linear-gradient()</dfn>
and <dfn>repeating-radial-gradient()</dfn> values.
These notations take the same values
and are interpreted the same
as their respective non-repeating siblings defined previously.
When rendered, however, the color-stops are repeated infinitely in both directions,
with their positions shifted by multiples of the difference between
the last specified color-stop's position
and the first specified color-stop's position.
For example, ''repeating-linear-gradient(red 10px, blue 50px)''
is equivalent to ''linear-gradient(..., red -30px, blue 10px, red 10px, blue 50px, red 50px, blue 90px, ...)''.
Note that the last color-stop and first color-stop will always coincide at the boundaries of each group,
which will produce sharp transitions if the gradient does not start and end with the same color.
<div class=example>
Repeating gradient syntax is identical to that of non-repeating gradients:
<pre>repeating-linear-gradient(red, blue 20px, red 40px)</pre>
<img src="images/repeating1.png" alt="">
<pre>repeating-radial-gradient(red, blue 20px, red 40px)</pre>
<img src="images/repeating2.png" alt="">
<pre>repeating-radial-gradient(circle closest-side at 20px 30px, red, yellow, green 100%, yellow 150%, red 200%)</pre>
<img src="images/repeating3.png" alt="">
</div>
If the gradient has only a single color-stop,
it must render as a solid-color image equal to the color of that color-stop.
If the distance between the first and last color-stops is non-zero,
but is small enough that the implementation knows that the physical resolution of the output device is insufficient to faithfully render the gradient,
the implementation must <a lt="gradient-average-color">find the average color of the gradient</a>
and render the gradient as a solid-color image equal to the average color.
If the distance between the first and last color-stops is zero
(or rounds to zero due to implementation limitations),
the implementation must <a lt="gradient-average-color">find the average color of a gradient</a>
with the same number and color of color-stops,
but with the first and last color-stop an arbitrary non-zero distance apart,
and the remaining color-stops equally spaced between them.
Then it must render the gradient as a solid-color image equal to that average color.
If the width of the ending shape of a repeating radial gradient is non-zero
and the height is zero,
or is close enough to zero that the implementation knows that the physical resolution of the output device is insufficient to faithfully render the gradient,
the implementation must <a lt="gradient-average-color">find the average color of the gradient</a>
and render the gradient as a solid-color image equal to the average color.
Note: The <a section href="#degenerate-radials">Degenerate Radial Gradients</a> section
describes how the ending shape is adjusted when its width is zero.
To <dfn lt="gradient-average-color">find the average color of a gradient</dfn>,
run these steps:
1. Define <var>list</var> as an initially-empty list of premultiplied RGBA colors,
and <var>total-length</var> as the distance between first and last color stops.
2. For each adjacent pair of color-stops,
define <var>weight</var> as half the distance between the two color-stops,
divided by <var>total-length</var>.
Add two entries to <var>list</var>,
the first obtained by representing the color of the first color-stop in premultiplied sRGBA
and scaling all of the components by <var>weight</var>,
and the second obtained in the same way with the second color-stop.
3. Sum the entries of <var>list</var> component-wise to produce the average color,
and return it.
Note: As usual, implementations may use whatever algorithm they wish,
so long as it produces the same result as the above.
<div class='example'>
For example, the following gradient is rendered as a solid light-purple image (equal to <code>rgb(75%,50%,75%)</code>):
<pre class="css">repeating-linear-gradient(red 0px, white 0px, blue 0px);</pre>
The following gradient would render the same as the previous under normal circumstances
(because desktop monitors can't faithfully render color-stops 1/10th of a pixel apart),
but would render as a normal repeating gradient if, for example,
the author applied "zoom:100;" to the element on which the gradient appears:
<pre class="css">repeating-linear-gradient(red 0px, white .1px, blue .2px);</pre>
</div>
<!--