SVG Stroke
Learn to outline SVG shapes with the stroke attribute and shape the line using stroke-width, stroke-dasharray, and stroke-linecap.
The stroke Attribute
The stroke attribute paints a line along the outline of a shape's path, separately from its fill. Its default value is none, meaning a shape has no visible outline even though the underlying path exists — you must explicitly set stroke to a color for it to appear. Setting fill="none" alongside a stroke color is a common combination for drawing wireframes, icons, and diagrams where only the outline should be visible.
Basic Strokes
<!DOCTYPE html>
<html>
<body>
<svg width="220" height="100" viewBox="0 0 220 100" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="20" width="70" height="60" fill="none" stroke="steelblue" />
<circle cx="140" cy="50" r="30" fill="none" stroke="crimson" />
</svg>
</body>
</html>Controlling Line Thickness with stroke-width
The stroke-width attribute sets how thick the outline is, measured in user units, with a default of 1. By default the stroke is drawn centered on the path — half its width extends inward and half extends outward — so a large stroke-width on a small shape can noticeably grow or shrink the shape's apparent size. Thicker strokes are useful for emphasis, while thin strokes suit fine technical drawings.
Comparing Stroke Widths
<!DOCTYPE html>
<html>
<body>
<svg width="260" height="100" viewBox="0 0 260 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="30" fill="none" stroke="#333" stroke-width="1" />
<circle cx="130" cy="50" r="30" fill="none" stroke="#333" stroke-width="6" />
<circle cx="210" cy="50" r="30" fill="none" stroke="#333" stroke-width="12" />
</svg>
</body>
</html>Dashed and Capped Lines
The stroke-dasharray attribute breaks a solid line into a repeating pattern of dashes and gaps, defined as a comma- or space-separated list of lengths. The stroke-linecap attribute controls the shape drawn at the open ends of a path — this is especially visible on dashed lines and open paths like a single <line> element, since each dash segment has its own two ends.
- stroke-dasharray="5" — equal 5-unit dashes and 5-unit gaps
- stroke-dasharray="10,5" — 10-unit dashes separated by 5-unit gaps
- stroke-linecap="butt" — a flat edge exactly at the end of the path (the default)
- stroke-linecap="round" — a rounded cap extending half the stroke-width beyond the path
- stroke-linecap="square" — a flat cap extending half the stroke-width beyond the path
Dashes with Different Line Caps
<!DOCTYPE html>
<html>
<body>
<svg width="260" height="140" viewBox="0 0 260 140" xmlns="http://www.w3.org/2000/svg">
<line x1="20" y1="30" x2="240" y2="30" stroke="#333" stroke-width="10" stroke-dasharray="20,15" stroke-linecap="butt" />
<line x1="20" y1="70" x2="240" y2="70" stroke="#333" stroke-width="10" stroke-dasharray="20,15" stroke-linecap="round" />
<line x1="20" y1="110" x2="240" y2="110" stroke="#333" stroke-width="10" stroke-dasharray="20,15" stroke-linecap="square" />
</svg>
</body>
</html>Exercise: SVG Stroke
If no stroke is specified on an SVG shape, what is drawn by default?