Chapter 17: SVG Stroke

SVG Stroke

This is the second half of coloring in SVG (the first half was fill which we covered earlier). If fill paints the inside of shapes, then stroke paints the outline / border / edge of the shape.

Think of it like this:

  • fill = coloring inside the lines of a coloring book
  • stroke = actually drawing the black lines themselves

I’m going to explain it slowly and clearly, like we’re sitting together building stylish outlines step by step.

What is stroke in SVG?

stroke = the color, thickness, style, and ending details of the border around (or along) any SVG shape or path.

It applies to almost everything:

  • <rect>, <circle>, <ellipse>, <polygon>, <path>
  • <line>, <polyline> (they only have stroke — no fill makes sense)
  • <text> and <textPath> (outline around letters!)

Default value: none (no visible stroke unless you set it)

Most important stroke properties

Property What it controls Common values Default Super useful?
stroke Color of the outline #333, red, currentColor, url(#grad) none ★★★★★
stroke-width Thickness of the line 1, 4, 10, “2px”, “0.5em” 1 ★★★★★
stroke-opacity Transparency of only the stroke 0 to 1 (0 = invisible, 1 = opaque) 1 ★★★★☆
stroke-linecap Shape of the line endings butt / round / square butt ★★★★☆
stroke-linejoin Shape of corners / joins miter / round / bevel miter ★★★★☆
stroke-dasharray Creates dashed / dotted lines “5”, “10 5”, “3 6 10 6” none ★★★★★
stroke-dashoffset Shifts where the dash pattern starts number (positive or negative) 0 ★★★☆☆

You can set these as attributes on the element or as CSS properties.

Example 1 – Basic stroke on shapes

HTML

Example 2 – stroke-linecap (line endings – very visible on open paths)

HTML

Here are visual differences of the three cap styles:

  • butt: flat cut at exact end point
  • round: half-circle cap (great for dotted lines)
  • square: square cap extending half the stroke-width beyond end

Example 3 – stroke-linejoin (corners)

HTML
  • miter (default) → sharp point (can get very long on sharp angles)
  • round → smooth rounded corners
  • bevel → flat cut at 45°

Example 4 – Dashed & dotted lines (stroke-dasharray magic)

HTML

Example 5 – stroke + fill together + currentColor

HTML

Hover → stroke color changes with parent color!

Quick Cheat Sheet

Goal Use this…
Visible border stroke=”#333″ + stroke-width=”4″
No border stroke=”none”
Rounded ends (good for dots) stroke-linecap=”round”
Smooth corners stroke-linejoin=”round”
Dashed line stroke-dasharray=”10 5″
Dotted line stroke-dasharray=”1 6″ + stroke-linecap=”round”
Outline only (no fill) fill=”none”

Common beginner mistakes

  • Forget stroke color → nothing visible even with stroke-width
  • Set stroke on filled shape but want only border → add fill=”none”
  • Very thin stroke on huge shape → looks invisible (use relative units or bigger number)
  • Dasharray pattern too long/short → experiment with numbers!
  • Expect stroke to affect text inside <text> → text uses its own stroke

Mini practice for you

  1. Draw a red circle with a thick dashed border (stroke-dasharray)
  2. Create three horizontal lines: one with round caps, one square, one butt — different colors
  3. Make a button-like rectangle with thick rounded stroke and no fill (outline button)

Paste your code here if you want feedback — I’ll review it like we’re pair-coding 😄

Any part still confusing? Dasharray patterns? linecap vs linejoin? currentColor with stroke? Just tell me — we’ll zoom in with more examples until it’s super clear! 🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *