SVG

Stroke & Fill

Learn how to fill and stroke SVG elements


‘path’ elements, ‘text’ elements and shapes can be filled (which means painting the interior of the object) and stroked (which means painting along the outline of the object). Iin this tutorial:


SVG Fill

The fill of an SVG shape defines the color of the shape inside its outline (the surface of the SVG shape). You can set the fill for any SVG shape.

Fill atributes are:

Example:

<svg>
    <path d="M50,20 l40,40 l-40,40 l-40,-40 l40,-40
            M50,40 l20,20 l-20,20 l-20,-20 l20,-20" style="stroke: #000000;
            fill: #6666ff;
            fill-rule: nonzero;" />

    <path d="M150,20 l40,40 l-40,40 l-40,-40 l40,-40
            M150,40 l-20,20 l20,20 l20,-20 l-20,-20" style="stroke: #000000;
            fill: #6666ff;
            fill-rule: nonzero;" />
</svg>

The nonzero rule considers the inner diamond inside or outside the shape depending on the direction the inner diamond is drawn.

In the following examples arrows indicate the direction of the path.

nonzero fill examples:

Example fillrule-nonzero - demonstrates fill-rule:nonzero

evenodd fill examples:

Example fillrule-evenodd - demonstrates fill-rule:evenodd

SVG Stroke

A subset of the complete set of stroke properties is:

#
Example linecap - demonstrates three stroke-linecap values 'butt' cap 'round' cap 'square' cap
Example linecap - demonstrates three stroke-linecap values 'miter' join 'round' join 'bevel' join

Text Stroke & Fill

Like other SVG shapes, text can have both a stroke and fill set on it. If you specify only a stroke, the text will appear as an outline of the text. If you specify only a fill, the text will look as text is rendered normally.

Example:

Fill only Stroke only Fill and stroke

Example: multi-stroke

MultiStroke Stroked Text Stroked Text Stroked Text
<svg  width="350" height="75" viewBox="0 0 350 75">
    <g style="font-size:45; font-weight: bold; font-family: Impact">
        <text x="175" y="55" style="fill: white; stroke: #0f9; stroke-width: 14">Stroked Text</text>
        <text x="175" y="55" style="fill: white; stroke: #99f; stroke-width: 8">Stroked Text</text>
        <text x="175" y="55" style="fill: white; stroke: black; stroke-width: 2">Stroked Text</text>
    </g>
</svg>