Fill and Stroke

The fill and stroke attribute lets you color shapes.

Fill SVG attributes

fill

Sets the interior color of an object. Specify:

  • nonetransparent fill
  • a colore.g. red, #ff0000, rgba(255,0,0,1)
  • a URLe.g. url(#my-gradient),

fill-opacity

Sets the fill opacity. Specify something like 0.5 or 50%.

fill-rule

Specify how to decide the "inside part" of the shape.

  • nonzerocolor anything "inside" the shape*
  • evenoddcolor anything "inside" the shape; but parts of the shape that are crossed even number of times by the path are considered "outside" (it's "cancelled out")**
| LIVE PREVIEW
nonzero evenodd

(*) Mathematically, you test the "insideness" of a point in the shape by drawing a ray from that point to infinity in any direction, then counting how many segments of the path crosses the ray. By nonzero rule, if there is at least one path segment crossing the ray, it's considered "inside", otherwise it's considered "outside"

(**) Similar as above. With evenodd rule, if there is an odd amount of path segments crossing the ray, it's considered "inside", otherwise it's considered "outside"

Stroke SVG attributes

stroke

Sets the outline color of the shape.

  • noneno stroke
  • a colore.g. red, #ff0000, rgba(255,0,0,1)
  • a URLe.g. url(#my-gradient),

stroke-width

Sets the width of the stroke. Specify a number or a percentage (relative to the size of the view).

stroke-opacity

Sets the opacity of the stroke. Specify something like 0.5 or 50%.

stroke-linecap

Sets the shape at the end of open paths ("the caps"). Specify butt, round, or square.

| LIVE PREVIEW
butt round square

stroke-linejoin

Defines the shape to be used at the path corners. Specify miter, round, bevel, or arcs.

Possible values for stroke-linejoin
Possible values for stroke-linejoin (Source: SVGWG)

stroke-dasharray

Defines the pattern of dashes and gaps in the path. Specify space/comma separated array of numbers/percentages, e.g. 10 1 4 1. If you specify an odd number of values, it will be repeated to yield an even number of values: i.e. 5 3 2 is equivalent to 5 3 2 5 3 2.

You can combine this with pathLength to control the number of repetitions along the path.

| LIVE PREVIEW

stroke-dashoffset

Offsets the rendering of stroke-dasharray. Specify a number or a percentage (percentage of the current viewport).

| LIVE PREVIEW

Other SVG attributes

opacity

Sets the opacity of an object. Specify a number from 0 to 1.

| LIVE PREVIEW

References