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
|
---
title: 使用 CSS 渐变
slug: Web/Guide/CSS/Using_CSS_gradients
translation_of: Web/CSS/CSS_Images/Using_CSS_gradients
---
<div>{{CSSRef}}</div>
<p><strong>CSS 渐变</strong> {{cssxref("<image>")}} 类型的一种特殊类型 {{cssxref("<gradient>")}} 表示,由两种或多种颜色之间的渐进过渡组成。您可以选择三种类型的渐变:线性 (由 {{cssxref("linear-gradient")}} 函数创建),径向(由 {{cssxref("radial-gradient")}} 函数创建) 和圆锥 (由 {{cssxref("conic-gradient")}} 函数创建)。您还可以使用 {{cssxref("repeating-linear-gradient")}} 和 {{cssxref("repeating-radial-gradient")}} 函数创建重复渐变。</p>
<p>渐变可以在任何使用 <code><image></code> 的地方使用,例如在背景中。 由于渐变是动态生成的,因此它们可以消除对传统用于实现类似效果的栅格图像文件的需求。 此外,由于渐变是由浏览器生成的,因此在放大时它们看起来比栅格图像更好,并且可以动态调整大小。</p>
<p>我们将从线性渐变开始介绍,然后以线性渐变为例介绍所有渐变类型支持的功能,然后继续介绍径向渐变,圆锥渐变和重复渐变。</p>
<h2 id="使用线性渐变">使用线性渐变</h2>
<p>线性渐变创建了一条沿直线前进的颜色带。</p>
<div>
<h3 id="基础线性渐变">基础线性渐变</h3>
<p>要创建最基本的渐变类型,您只需指定两种颜色即可。 这些被称为色标。 至少指定两个色标,也可以指定任意数量。</p>
<div class="hidden">
<pre class="brush: html notranslate" dir="rtl"><div class="simple-linear"></div></pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.simple-linear {
background: linear-gradient(blue, pink);
}</pre>
<p>{{ EmbedLiveSample('A_basic_linear_gradient', 120, 120) }}</p>
</div>
<div>
<h3 id="改变渐变方向">改变渐变方向</h3>
<p>默认情况下,线性渐变的方向是从上到下, 你可以指定一个值来改变渐变的方向。</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="horizontal-gradient"></div></pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.horizontal-gradient {
background: linear-gradient(to right, blue, pink);
}
</pre>
<p>{{ EmbedLiveSample('Changing_the_direction', 120, 120) }}</p>
</div>
<div>
<h3 id="对角线渐变">对角线渐变</h3>
<p>你甚至可以设置渐变方向为从一个对角到另一个对角。</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="diagonal-gradient"></div></pre>
<pre class="brush: css notranslate">div {
width: 200px;
height: 100px;
}</pre>
</div>
<pre class="brush: css notranslate">.diagonal-gradient {
background: linear-gradient(to bottom right, blue, pink);
}
</pre>
<p>{{ EmbedLiveSample('Diagonal_gradients', 200, 100) }}</p>
</div>
<div>
<h3 id="设置渐变角度">设置渐变角度</h3>
<p>如果你想要更精确地控制渐变的方向,你可以给渐变设置一个具体的角度。</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="angled-gradient"></div></pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.angled-gradient {
background: linear-gradient(70deg, blue, pink);
}
</pre>
<p>{{ EmbedLiveSample('Using_angles', 120, 120) }}</p>
<p>在使用角度的时候, <code>0deg</code> 代表渐变方向为从下到上, <code>90deg</code> 代表渐变方向为从左到右,诸如此类正角度都属于顺时针方向。 而负角度意味着逆时针方向。</p>
<p><img alt="linear_redangles.png" class="default internal" src="/files/3811/linear_red_angles.png"></p>
</div>
<h2 id="声明颜色和创建效果">声明颜色和创建效果</h2>
<p>所有的CSS渐变类型都是一个位置依赖的颜色范围。CSS渐变产生的颜色可以随位置不断变化,从而产生平滑的颜色过渡。也可以创建纯色带和两种颜色之间的硬过渡。以下内容适用于所有渐变函数:</p>
<div>
<h3 id="使用多种颜色">使用多种颜色</h3>
<p>无需局限于使用两种颜色,你想使用多少种颜色都可以! 默认情况下,所设置颜色会均匀分布在渐变路径中。</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="auto-spaced-linear-gradient"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.auto-spaced-linear-gradient {
background: linear-gradient(red, yellow, blue, orange);
}
</pre>
<p>{{ EmbedLiveSample('Using_more_than_two_colors', 120, 120) }}</p>
</div>
<div>
<h3 id="颜色终止位置">颜色终止位置</h3>
<p>你不需要让你设置的颜色在默认位置终止。 你可以通过给每个颜色设置0,1%或者2%或者其他的绝对数值来调整它们的位置。如果你将位置设置为百分数, <code>0%</code> 表示起始点, 而100%表示终点,但是如果需要的话你也可以设置这个范围之外的其他值来达到你想要的效果。如果有些位置你没有明确设置,那么它将会被自动计算,第一种颜色会在0%处停止,而最后一种颜色是100%,至于其他颜色则是在它邻近的两种颜色的中间停止。 </p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="multicolor-linear"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.multicolor-linear {
background: linear-gradient(to left, lime 28px, red 77%, cyan);
}
</pre>
<p>{{ EmbedLiveSample('Positioning_color_stops', 120, 120) }}</p>
</div>
<div>
<h3 id="创建实线">创建实线</h3>
<p>要在两种颜色之间创建一条硬线,即创建一个条纹而不是逐渐过渡,可以将相邻的颜色停止设置为相同的位置。在此示例中,两种颜色在50%标记处共享一个颜色停止点,即渐变的一半:</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="striped"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.striped {
background: linear-gradient(to bottom left, cyan 50%, palegoldenrod 50%);
}</pre>
<p>{{ EmbedLiveSample('Creating_hard_lines', 120, 120) }}</p>
</div>
<div>
<h3 id="渐变提示">渐变提示</h3>
<p>默认情况下,渐变会平滑地从一种颜色过渡到另一种颜色。你可以通过设置一个值来将渐变的中心点移动到指定位置。 在如下示例中, 我们将渐变的中心点由50%设为10%。</p>
<div class="hidden">
<pre class="brush: html notranslate" dir="rtl"><div class="color-hint"></div>
<div class="simple-linear"></div></pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px; float: left; margin-right: 10px;
}</pre>
</div>
<pre class="brush: css notranslate">.color-hint {
background: linear-gradient(blue, 10%, pink);
}
.simple-linear {
background: linear-gradient(blue, pink);
}</pre>
<p>{{ EmbedLiveSample('Gradient_hints', 120, 120) }}</p>
</div>
<div>
<h3 id="创建色带和条纹">创建色带和条纹</h3>
<p>要在渐变中包含一个实心的非过渡颜色区域,请包含颜色起止点的两个位置。颜色起止点可以有两个位置,这相当于两个连续颜色在不同位置具有相同的颜色起止点。颜色将在第一个颜色起止点时达到完全饱和,保持该饱和度到第二个颜色起止点,并通过相邻颜色起止点的第一个位置过渡到相邻颜色起止点的颜色。</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="multiposition-stops"></div>
<div class="multiposition-stop2"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
float: left; margin-right: 10px; box-sizing: border-box;
}</pre>
</div>
<pre class="brush: css notranslate">.multiposition-stops {
background: linear-gradient(to left,
lime 20%, red 30%, red 45%, cyan 55%, cyan 70%, yellow 80% );
background: linear-gradient(to left,
lime 20%, red 30% 45%, cyan 55% 70%, yellow 80% );
}
.multiposition-stop2 {
background: linear-gradient(to left,
lime 25%, red 25%, red 50%, cyan 50%, cyan 75%, yellow 75% );
background: linear-gradient(to left,
lime 25%, red 25% 50%, cyan 50% 75%, yellow 75% );
}
</pre>
<p>{{ EmbedLiveSample('Creating_color_bands_stripes', 120, 120) }}</p>
<p>In the first example above, the lime goes from the 0% mark, which is implied, to the 20% mark, transitions from lime to red over the next 10% of the width of the gradient, reach solid red at the 30% mark, and staying solid red up until 45% through the gradient, where it fades to cyan, being fully cyan for 15% of the gradient, and so on.</p>
<p>In the second example, the second color stop for each color is at the same location as the first color stop for the adjacent color, creating a striped effect.</p>
<p>In both examples, the gradient is written twice: the first is the CSS Images Level 3 method of repeating the color for each stop and the second example is the CSS Images Level 4 multiple color stop method of including two color-stop-lengths in a linear-color-stop declaration.</p>
</div>
<div>
<h3 id="Controlling_the_progression_of_a_gradient">Controlling the progression of a gradient</h3>
<p>By default, a gradient evenly progresses between the colors of two adjacent color stops, with the midpoint between those two color stops being the midpoint color value. You can control the interpolation, or progression, between two color stops by including a color hint location. In this example, the color reaches the midpoint between lime and cyan 20% of the way through the gradient rather than 50% of the way through. The second example does not contain the hint to hilight the difference the color hint can make:</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="colorhint-gradient"></div> <div class="regular-progression"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
float: left; margin-right: 10px; box-sizing: border-box;
}</pre>
</div>
<pre class="brush: css notranslate">.colorhint-gradient {
background: linear-gradient(to top, black, 20%, cyan);
}
.regular-progression {
background: linear-gradient(to top, black, cyan);
}
</pre>
<p>{{ EmbedLiveSample('Controlling_the_progression_of_a_gradient', 120, 120) }}</p>
</div>
<h3 id="Overlaying_gradients">Overlaying gradients</h3>
<p>Gradients support transparency, so you can stack multiple backgrounds to achieve some pretty fancy effects. The backgrounds are stacked from top to bottom, with the first specified being on top.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="layered-image"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 300px;
height: 150px;
}</pre>
</div>
<pre class="brush: css notranslate">.layered-image {
background: linear-gradient(to right, transparent, mistyrose),
url("https://mdn.mozillademos.org/files/15525/critters.png");
}
</pre>
<p>{{ EmbedLiveSample('Overlaying_gradients', 300, 150) }}</p>
<h3 id="Stacked_gradients">Stacked gradients</h3>
<p>You can even stack gradients with other gradients. As long as the top gradients aren't entirely opaque, the gradients below will still be visible.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="stacked-linear"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 200px;
height: 200px;
}</pre>
</div>
<pre class="brush: css notranslate">.stacked-linear {
background:
linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
}
</pre>
<p>{{ EmbedLiveSample('Stacked_gradients', 200, 200) }}</p>
<h2 id="Using_radial_gradients">Using radial gradients</h2>
<p>Radial gradients are similar to linear gradients, except that they radiate out from a central point. You can dictate where that central point is. You can also make them circular or elliptical.</p>
<h3 id="A_basic_radial_gradient">A basic radial gradient</h3>
<p>As with linear gradients, all you need to create a radial gradient are two colors. By default, the center of the gradient is at the 50% 50% mark, and the gradient is elliptical matching the aspect ratio of it's box:</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="simple-radial"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 240px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.simple-radial {
background: radial-gradient(red, blue);
}
</pre>
<p>{{ EmbedLiveSample('A_basic_radial_gradient', 120, 120) }}</p>
<h3 id="Positioning_radial_color_stops">Positioning radial color stops</h3>
<p>Again like linear gradients, you can position each radial color stop with a percentage or absolute length.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="radial-gradient"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.radial-gradient {
background: radial-gradient(red 10px, yellow 30%, #1e90ff 50%);
}
</pre>
<p>{{ EmbedLiveSample('Positioning_radial_color_stops', 120, 120) }}</p>
<h3 id="Positioning_the_center_of_the_gradient">Positioning the center of the gradient</h3>
<p>You can position the center of the gradient with keyterms, percentage, or absolute lengths, length and percentage values repeating if only one is present, otherwise in the order of position from the left and position from the top.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="radial-gradient"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 240px;
}</pre>
</div>
<pre class="brush: css notranslate">.radial-gradient {
background: radial-gradient(at 0% 30%, red 10px, yellow 30%, #1e90ff 50%);
}
</pre>
<p>{{ EmbedLiveSample('Positioning_the_center_of_the_gradient', 120, 120) }}</p>
<h3 id="Sizing_radial_gradients">Sizing radial gradients</h3>
<p>Unlike linear gradients, you can specify the size of radial gradients. Possible values include closest-corner, closest-side, farthest-corner, and farthest-side, with farthest-corner being the default.</p>
<h4 id="Example_closest-side_for_ellipses">Example: closest-side for ellipses</h4>
<p>This example uses the <code>closest-side</code> size value, which means the size is set by the distance from the starting point (the center) to the closest side of the enclosing box.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="radial-ellipse-side"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 240px;
height: 100px;
}</pre>
</div>
<pre class="brush: css notranslate">.radial-ellipse-side {
background: radial-gradient(ellipse closest-side,
red, yellow 10%, #1e90ff 50%, beige);
}
</pre>
<p>{{ EmbedLiveSample('Example_closest-side_for_ellipses', 240, 100) }}</p>
<h4 id="Example_farthest-corner_for_ellipses">Example: farthest-corner for ellipses</h4>
<p>This example is similar to the previous one, except that its size is specified as <code>farthest-corner</code>, which sets the size of the gradient by the distance from the starting point to the farthest corner of the enclosing box from the starting point.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="radial-ellipse-far"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 240px;
height: 100px;
}</pre>
</div>
<pre class="brush: css notranslate">.radial-ellipse-far {
background: radial-gradient(ellipse farthest-corner at 90% 90%,
red, yellow 10%, #1e90ff 50%, beige);
}
</pre>
<p>{{ EmbedLiveSample('Example_farthest-corner_for_ellipses', 240, 100) }}</p>
<h4 id="Example_closest-side_for_circles">Example: closest-side for circles</h4>
<p>This example uses <code>closest-side</code>, which makes the circle's size to be the distance between the starting point (the center) and the closest side. The circle's radius is the distance between the center of the gradient and the closest edge, which due to the positioning of the 25% from the top and 25% from the bottom, is closest to the bottom, since the height in this case is narrower than the width.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="radial-circle-close"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 240px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.radial-circle-close {
background: radial-gradient(circle closest-side at 25% 75%,
red, yellow 10%, #1e90ff 50%, beige);
}
</pre>
<p>{{ EmbedLiveSample('Example_closest-side_for_circles', 240, 120) }}</p>
<h3 id="Stacked_radial_gradients">Stacked radial gradients</h3>
<p>Just like linear gradients, you can also stack radial gradients. The first specified is on top, the last on the bottom.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="stacked-radial"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 200px;
height: 200px;
}</pre>
</div>
<pre class="brush: css notranslate">.stacked-radial {
background:
radial-gradient(circle at 50% 0,
rgba(255,0,0,.5),
rgba(255,0,0,0) 70.71%),
radial-gradient(circle at 6.7% 75%,
rgba(0,0,255,.5),
rgba(0,0,255,0) 70.71%),
radial-gradient(circle at 93.3% 75%,
rgba(0,255,0,.5),
rgba(0,255,0,0) 70.71%) beige;
border-radius: 50%;
}
</pre>
<p>{{ EmbedLiveSample('Stacked_radial_gradients', 200, 200) }}</p>
<h2 id="Using_repeating_gradients">Using repeating gradients</h2>
<p>The {{cssxref("linear-gradient")}} and {{cssxref("radial-gradient")}} properties don't support automatically repeated color stops. However, the {{cssxref("repeating-linear-gradient")}} and {{cssxref("repeating-radial-gradient")}} properties are available to offer this functionality.</p>
<p>The size of the gradient line that repeats is the length between the first color stop value and the last color stop length value. If the last color stop has just a color and no color stop length, the value defaults to 0, meaning the linear gradient will not repeat and the radial gradient will only repeat if the radius of the gradient is smaller than the length between the center of the gradient and the farthest corner.</p>
<div>
<h3 id="Repeating_linear_gradients">Repeating linear gradients</h3>
<p>This example uses {{cssxref("repeating-linear-gradient")}} to create a gradient that progresses repeatedly in a straight line. The colors get cycled over again as the gradient repeats. In this case the gradient line is 10px long.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="repeating-linear"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.repeating-linear {
background: repeating-linear-gradient(-45deg, red, red 5px, blue 5px, blue 10px);
}
</pre>
<p>{{ EmbedLiveSample('Repeating_linear_gradients', 120, 120) }}</p>
</div>
<div>
<h3 id="Multiple_repeating_linear_gradients">Multiple repeating linear gradients</h3>
<p>Similar to regular linear and radial gradients, you can include multiple gradients, one on top of the other. This only makes sense if the gradients are partially transparent allowing subsequent gradients to show through the transparent areas, or if you include different <a href="/en-US/docs/Web/CSS/background-size">background-sizes</a>, optionally with different <a href="/en-US/docs/Web/CSS/background-position">background-position</a> property values, for each gradient image. We are using transparency.</p>
<p>In this case the gradient lines are 300px, 230px, and 300px long.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="multi-repeating-linear"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 600px;
height: 400px;
}</pre>
</div>
<pre class="brush: css notranslate">.multi-repeating-linear {
background:
repeating-linear-gradient(190deg, rgba(255, 0, 0, 0.5) 40px,
rgba(255, 153, 0, 0.5) 80px, rgba(255, 255, 0, 0.5) 120px,
rgba(0, 255, 0, 0.5) 160px, rgba(0, 0, 255, 0.5) 200px,
rgba(75, 0, 130, 0.5) 240px, rgba(238, 130, 238, 0.5) 280px,
rgba(255, 0, 0, 0.5) 300px),
repeating-linear-gradient(-190deg, rgba(255, 0, 0, 0.5) 30px,
rgba(255, 153, 0, 0.5) 60px, rgba(255, 255, 0, 0.5) 90px,
rgba(0, 255, 0, 0.5) 120px, rgba(0, 0, 255, 0.5) 150px,
rgba(75, 0, 130, 0.5) 180px, rgba(238, 130, 238, 0.5) 210px,
rgba(255, 0, 0, 0.5) 230px),
repeating-linear-gradient(23deg, red 50px, orange 100px,
yellow 150px, green 200px, blue 250px,
indigo 300px, violet 350px, red 370px);
}
</pre>
<p>{{ EmbedLiveSample('Multiple_repeating_linear_gradients', 600, 400) }}</p>
</div>
<h3 id="Plaid_gradient">Plaid gradient</h3>
<p>To create plaid we include several overlapping gradients with transparency. In the first background declaration we listed every color stop separately. The second background property declaration using the multiple position color stop syntax:</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="plaid-gradient"></div></pre>
<pre class="brush: css notranslate">div {
width: 200px;
height: 200px;
}</pre>
</div>
<pre class="brush: css notranslate">.plaid-gradient {
background:
repeating-linear-gradient(90deg, transparent, transparent 50px,
rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
transparent 56px, transparent 63px,
rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
transparent 69px, transparent 116px,
rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
repeating-linear-gradient(0deg, transparent, transparent 50px,
rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
transparent 56px, transparent 63px,
rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
transparent 69px, transparent 116px,
rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
repeating-linear-gradient(-45deg, transparent, transparent 5px,
rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px),
repeating-linear-gradient(45deg, transparent, transparent 5px,
rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px);
background:
repeating-linear-gradient(90deg, transparent 0 50px,
rgba(255, 127, 0, 0.25) 50px 56px,
transparent 56px 63px,
rgba(255, 127, 0, 0.25) 63px 69px,
transparent 69px 116px,
rgba(255, 206, 0, 0.25) 116px 166px),
repeating-linear-gradient(0deg, transparent 0 50px,
rgba(255, 127, 0, 0.25) 50px 56px,
transparent 56px 63px,
rgba(255, 127, 0, 0.25) 63px 69px,
transparent 69px 116px,
rgba(255, 206, 0, 0.25) 116px 166px),
repeating-linear-gradient(-45deg, transparent 0 5px,
rgba(143, 77, 63, 0.25) 5px 10px),
repeating-linear-gradient(45deg, transparent 0 5px,
rgba(143, 77, 63, 0.25) 5px 10px);
}
</pre>
<p>{{ EmbedLiveSample('Plaid_gradient', 200, 200) }}</p>
<h3 id="Repeating_radial_gradients">Repeating radial gradients</h3>
<p>This example uses {{cssxref("repeating-radial-gradient")}} to create a gradient that radiates repeatedly from a central point. The colors get cycled over and over as the gradient repeats.</p>
<div class="hidden">
<pre class="brush: html notranslate"><div class="repeating-radial"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 120px;
height: 120px;
}</pre>
</div>
<pre class="brush: css notranslate">.repeating-radial {
background: repeating-radial-gradient(black, black 5px, white 5px, white 10px);
}
</pre>
<p>{{ EmbedLiveSample('Repeating_radial_gradients', 120, 120) }}</p>
<h3 id="Multiple_repeating_radial_gradients">Multiple repeating radial gradients</h3>
<div class="hidden">
<pre class="brush: html notranslate"><div class="multi-target"></div>
</pre>
<pre class="brush: css notranslate">div {
width: 250px;
height: 150px;
}</pre>
</div>
<pre class="brush: css notranslate">.multi-target {
background:
repeating-radial-gradient(ellipse at 80% 50%,rgba(0,0,0,0.5),
rgba(0,0,0,0.5) 15px, rgba(255,255,255,0.5) 15px,
rgba(255,255,255,0.5) 30px) top left no-repeat,
repeating-radial-gradient(ellipse at 20% 50%,rgba(0,0,0,0.5),
rgba(0,0,0,0.5) 10px, rgba(255,255,255,0.5) 10px,
rgba(255,255,255,0.5) 20px) top left no-repeat yellow;
background-size: 200px 200px, 150px 150px;
}
</pre>
<p>{{ EmbedLiveSample('Multiple_repeating_radial_gradients', 250, 150) }}</p>
<h3 id="Plaid_gradient_2">Plaid gradient</h3>
<p>To create plaid we include several overlapping gradients with transparency. In the first background declaration we listed every color stop separately. The second background property declaration using the multiple position color stop syntax:</p>
<pre class="notranslate"><div class="plaid-gradient"></div></pre>
<pre class="notranslate">div {
width: 200px;
height: 200px;
}</pre>
<pre class="notranslate">.plaid-gradient {
background:
repeating-linear-gradient(90deg, transparent, transparent 50px,
rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
transparent 56px, transparent 63px,
rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
transparent 69px, transparent 116px,
rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
repeating-linear-gradient(0deg, transparent, transparent 50px,
rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px,
transparent 56px, transparent 63px,
rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px,
transparent 69px, transparent 116px,
rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
repeating-linear-gradient(-45deg, transparent, transparent 5px,
rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px),
repeating-linear-gradient(45deg, transparent, transparent 5px,
rgba(143, 77, 63, 0.25) 5px, rgba(143, 77, 63, 0.25) 10px);
background:
repeating-linear-gradient(90deg, transparent 0 50px,
rgba(255, 127, 0, 0.25) 50px 56px,
transparent 56px 63px,
rgba(255, 127, 0, 0.25) 63px 69px,
transparent 69px 116px,
rgba(255, 206, 0, 0.25) 116px 166px),
repeating-linear-gradient(0deg, transparent 0 50px,
rgba(255, 127, 0, 0.25) 50px 56px,
transparent 56px 63px,
rgba(255, 127, 0, 0.25) 63px 69px,
transparent 69px 116px,
rgba(255, 206, 0, 0.25) 116px 166px),
repeating-linear-gradient(-45deg, transparent 0 5px,
rgba(143, 77, 63, 0.25) 5px 10px),
repeating-linear-gradient(45deg, transparent 0 5px,
rgba(143, 77, 63, 0.25) 5px 10px);
}
</pre>
<p>{{ EmbedLiveSample('Plaid_gradient', 200, 200) }}</p>
<h2 id="See_also">See also</h2>
<ul>
<li>Gradient functions: {{cssxref("linear-gradient")}}, {{cssxref("radial-gradient")}}, {{cssxref("repeating-linear-gradient")}}, {{cssxref("repeating-radial-gradient")}}</li>
<li>Gradient-related CSS data types: {{cssxref("<gradient>")}}, {{cssxref("<image>")}}</li>
<li>Gradient-related CSS properties: {{cssxref("background")}}, {{cssxref("background-image")}}</li>
<li><a class="external" href="http://lea.verou.me/css3patterns/">CSS Gradients Patterns Gallery, by Lea Verou</a></li>
<li><a class="external" href="http://standardista.com/cssgradients">CSS3 Gradients Library, by Estelle Weyl</a></li>
<li><a href="https://cssgenerator.org/gradient-css-generator.html">Gradient CSS Generator</a></li>
</ul>
|