Chapter 15: SVG marker

SVG: markers

Imagine you’re drawing a map, a flowchart, a graph, or just a fancy arrow — and you want nice arrowheads, dots, diamonds, or custom shapes automatically placed at the start, middle, or end of lines and paths. Doing this manually with many <polygon> or <circle> elements would be painful and hard to maintain.

SVG markers solve exactly this problem — they let you define a small reusable graphic once, and then tell any <line>, <polyline>, or <path> to “put this marker on my ends (or even along the whole path)”.

I’m going to explain it like we’re sitting together building arrows step by step — slowly, clearly, with examples you can copy-paste right now.

What is an SVG <marker>?

  • <marker> is a definition element — it lives inside <defs> (like <clipPath>, <pattern>, <filter>)
  • It defines a small independent drawing area (like a mini-SVG inside your SVG)
  • You give it an id
  • Then you attach it to lines/paths using special attributes: marker-start, marker-mid, marker-end

Once attached, the marker automatically:

  • Rotates to match the direction of the line/path
  • Scales if needed
  • Positions itself correctly at the exact endpoint (or midpoints)

Core attributes on <marker>

Attribute Meaning / Purpose Common values Default Very important?
id Unique name so you can reference it “arrow”, “dot”, “myMarker” ★★★★★
markerWidth / markerHeight Size of the marker’s viewport (like viewBox size) 10, 12, 20, etc. 3 ★★★★★
refX / refY Which point inside marker is “pinned” to the line end 0–markerWidth, usually center or tip 0 ★★★★★
orient How to rotate the marker “auto” (follows line), angle in deg, “auto-start-reverse” “auto” ★★★★☆
markerUnits What units for width/height/refX/refY “userSpaceOnUse” or “strokeWidth” “strokeWidth” ★★★☆☆

Most common & important combo (arrow tip):

text

Here are some visual explanations of refX/refY and orient (very helpful to see where the “pin” point is):

(Imagine seeing here: diagram showing arrow marker with refX=10 refY=5 pointing exactly at line end + rotation arrows)

Example 1 – Classic arrowhead on both ends

HTML

Result: Blue line with orange arrowheads on both ends, automatically rotated.

Example 2 – Different marker for start & end (very common pattern)

HTML

Example 3 – Markers along the whole path (marker-mid)

HTML

→ Dots appear at start, end, and every sharp corner (where direction changes)

Example 4 – Custom size with strokeWidth units (very useful for consistent look)

HTML

→ Arrow scales with stroke-width — stays proportional (very clean look)

Quick Cheat Sheet – When to use which marker placement

Attribute Places marker at… Typical use case
marker-start Beginning of path (first point) Starting dot, open arrow
marker-end End of path (last point) Arrowhead, destination marker
marker-mid Every vertex (corner/point) except start & end Mid dots, vertices in graphs, dashed styles
All three Start + mid + end Full control

Common beginner mistakes

  • Forget to put <marker> inside <defs> → nothing shows
  • Wrong refX/refY → arrow tip not touching line end (most common!)
  • orient=”0″ instead of “auto” → arrows don’t rotate with line direction
  • markerUnits=”userSpaceOnUse” but forget to scale → markers too big/small
  • Using fill=”none” on marker path → invisible
  • Referencing wrong id (url(#arrow) vs url(#Arrow) — case sensitive!)

Mini practice challenges

  1. Make a red dashed line with small black circles at both ends and mid points
  2. Create a green flowchart-style arrow with a bigger triangle head and a small square start marker
  3. Draw a curved path (use Q or C) with orange diamonds at every corner

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

Any part still fuzzy? refX/refY positioning? orient=”auto” vs fixed angle? marker-mid behavior? Scaling with stroke-width? Just say the word — we’ll redraw examples bigger/slower until it clicks perfectly! 🚀

You may also like...

Leave a Reply

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