-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
488 lines (411 loc) · 12.1 KB
/
index.md
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
---
title: fill
slug: Web/SVG/Attribute/fill
page-type: svg-attribute
browser-compat: svg.global_attributes.fill
---
{{SVGRef}}
The **`fill`** attribute has two different meanings. For shapes and text it's a presentation attribute that defines the color (_or any SVG paint servers like gradients or patterns_) used to paint the element; for animation it defines the final state of the animation.
You can use this attribute with the following SVG elements:
- {{SVGElement('circle')}}
- {{SVGElement('ellipse')}}
- {{SVGElement('path')}}
- {{SVGElement('polygon')}}
- {{SVGElement('polyline')}}
- {{SVGElement('rect')}}
- {{SVGElement('text')}}
- {{SVGElement('textPath')}}
- {{SVGElement('tref')}}
- {{SVGElement('tspan')}}
For animation, these elements are using this attribute: {{SVGElement('animate')}}, {{SVGElement('animateMotion')}}, {{SVGElement('animateTransform')}}, and {{SVGElement('set')}}.
## Examples
### Basic color and gradient fills, and animation
```css hidden
html,
body,
svg {
height: 100%;
}
```
```html
<svg viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
<!-- Simple color fill -->
<circle cx="50" cy="50" r="40" fill="pink" />
<!-- Fill circle with a gradient -->
<defs>
<radialGradient id="myGradient">
<stop offset="0%" stop-color="pink" />
<stop offset="100%" stop-color="black" />
</radialGradient>
</defs>
<circle cx="150" cy="50" r="40" fill="url(#myGradient)" />
<!--
Keeping the final state of an animated circle
which is a circle with a radius of 40.
-->
<circle cx="250" cy="50" r="20">
<animate
attributeType="XML"
attributeName="r"
from="0"
to="40"
dur="5s"
fill="freeze" />
</circle>
</svg>
```
{{EmbedLiveSample("Basic color and gradient fills, and animation", '100%', 200)}}
### `context-fill` example
In this example, we define three shapes using {{SVGElement('path')}} elements, each with a different [`stroke`](/en-US/docs/Web/SVG/Attribute/stroke) and `fill` color set on them. We also define a {{SVGElement('circle')}} element as a marker via the {{SVGElement('marker')}} element. Each shape has the marker applied via the `marker` CSS property.
The {{SVGElement('circle')}} has `stroke="context-stroke"` and `fill="context-fill"` set on it. Because it is being set as a marker in the context of the shapes, these attributes cause it to inherit the `fill` and `stroke` set on the {{SVGElement('path')}} element in each case.
```html-nolint
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 90">
<style>
path {
stroke-width: 2px;
marker: url(#circle);
}
</style>
<path d="M 10 44.64 L 30 10 L 70 10 L 90 44.64 L 70 79.28 L 30 79.28 Z"
stroke="red" fill="orange" />
<path d="M 100 44.64 L 80 10 L 120 10 L 140 44.64 L 120 79.28 L 80 79.28 Z"
stroke="green" fill="lightgreen" />
<path d="M 150 44.64 L 130 10 L 170 10 L 190 44.64 L 170 79.28 L 130 79.28 Z"
stroke="blue" fill="lightblue" />
<marker id="circle" markerWidth="12" markerHeight="12"
refX="6" refY="6" markerUnits="userSpaceOnUse">
<circle cx="6" cy="6" r="3" stroke-width="2"
stroke="context-stroke" fill="context-fill" />
</marker>
</svg>
```
The output is as follows:
{{EmbedLiveSample("`context-stroke` example", '100%', 220)}}
> **Note:** Elements can also use `context-stroke` and `context-fill` to inherit `stroke` and `fill` values when they are referenced by {{SVGElement('use')}} elements.
## animate
For {{SVGElement('animate')}}, `fill` defines the final state of the animation.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<code>freeze</code> (<em>Keep the state of the last animation frame</em
>) | <code>remove</code> (<em
>Keep the state of the first animation frame</em
>)
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>remove</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>No</td>
</tr>
</tbody>
</table>
## animateMotion
For {{SVGElement('animateMotion')}}, `fill` defines the final state of the animation.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<code>freeze</code> (<em>Keep the state of the last animation frame</em
>) | <code>remove</code> (<em
>Keep the state of the first animation frame</em
>)
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>remove</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>No</td>
</tr>
</tbody>
</table>
## animateTransform
For {{SVGElement('animateTransform')}}, `fill` defines the final state of the animation.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<code>freeze</code> (<em>Keep the state of the last animation frame</em
>) | <code>remove</code> (<em
>Keep the state of the first animation frame</em
>)
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>remove</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>No</td>
</tr>
</tbody>
</table>
## circle
For {{SVGElement('circle')}}, `fill` is a presentation attribute that defines the color of the circle.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## ellipse
For {{SVGElement('ellipse')}}, `fill` is a presentation attribute that defines the color of the ellipse.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## path
For {{SVGElement('path')}}, `fill` is a presentation attribute that defines the color of the interior of the shape. (_Interior is define by the {{SVGAttr('fill-rule')}} attribute_)
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## polygon
For {{SVGElement('polygon')}}, `fill` is a presentation attribute that defines the color of the interior of the shape. (_Interior is define by the {{SVGAttr('fill-rule')}} attribute_)
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## polyline
For {{SVGElement('polyline')}}, `fill` is a presentation attribute that defines the color of the interior of the shape. (_Interior is define by the {{SVGAttr('fill-rule')}} attribute_)
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## rect
For {{SVGElement('rect')}}, `fill` is a presentation attribute that defines the color of the rectangle.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## set
For {{SVGElement('set')}}, `fill` defines the final state of the animation.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<code>freeze</code> (<em>Keep the state of the last animation frame</em
>) | <code>remove</code> (<em
>Keep the state of the first animation frame</em
>)
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>remove</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>No</td>
</tr>
</tbody>
</table>
## text
For {{SVGElement('text')}}, `fill` is a presentation attribute that defines the color of the text.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## textPath
For {{SVGElement('textPath')}}, `fill` is a presentation attribute that defines the color of the text.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## tref
> **Warning:** As of SVG2 {{SVGElement('tref')}} is deprecated and shouldn't be used.
For {{SVGElement('tref')}}, `fill` is a presentation attribute that defines the color of the text.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## tspan
For {{SVGElement('tspan')}}, `fill` is a presentation attribute that defines the color of the text.
<table class="properties">
<tbody>
<tr>
<th scope="row">Value</th>
<td>
<strong
><a href="/en-US/docs/Web/SVG/Content_type#paint"><paint></a></strong
>
</td>
</tr>
<tr>
<th scope="row">Default value</th>
<td><code>black</code></td>
</tr>
<tr>
<th scope="row">Animatable</th>
<td>Yes</td>
</tr>
</tbody>
</table>
> **Note:** As a presentation attribute `fill` can be used as a CSS property.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}