Chapter 16: SVG marker

SVG Marker means using the <marker> element to create small reusable graphics (like arrowheads, dots, circles, diamonds, custom symbols) that automatically attach to the start, middle, or end of lines, paths, polylines, or polygons.

Think of it this way:

  • You draw a line or path
  • You want nice arrow tips at the ends? Or dots at every corner?
  • Instead of manually drawing each arrow every time → define one small symbol once (the marker), then attach it to any line/path you want

This is super common in:

  • Flowcharts (arrows connecting boxes)
  • Graphs & charts (arrowed axes)
  • Maps (direction arrows)
  • Technical diagrams
  • Decorative lines (dots, stars along path)

1. How Markers Work (Big Picture)

  1. Define the marker inside <defs> (hidden reusable area) with <marker id=”…” …>
  2. Inside the marker, draw your symbol (circle, path for arrow, etc.)
  3. On your <line>, <path>, <polyline>, or <polygon>, add attributes:
    • marker-start=”url(#myArrow)” → at beginning
    • marker-mid=”url(#myDot)” → at every middle vertex/corner
    • marker-end=”url(#myArrow)” → at end

Browser automatically places, rotates, and scales the marker to fit the line direction!

2. Key Attributes of <marker>

Attribute Meaning Required? Default Common Example Notes
id Unique name to reference later Yes “arrow”, “dot” Must have!
markerWidth Width of marker’s viewport No 3 10, 13 Size of marker “box”
markerHeight Height of marker’s viewport No 3 10, 13 Usually same as width
refX X point in marker that “pins” to line vertex No 0 5, 0, 2 Center or tip point
refY Y point in marker that pins No 0 5, 6 Aligns with line end
orient Rotation: “auto” (follows line direction) or angle No 0 “auto”, 45 “auto” = most used
markerUnits Coordinate system: “strokeWidth” or “userSpaceOnUse” No strokeWidth userSpaceOnUse strokeWidth = scales with line thickness

3. Your First Marker Example – Arrowhead on Line (Copy-Paste Now!)

HTML

What you see: Thick navy line with black arrowheads at both ends!

Breakdown:

  • <marker id=”arrow”> → defines reusable arrow
  • Inside: small <path> drawing triangle arrow
  • refX=”2″ refY=”6″ → tip of arrow (pointy part) pins to line end
  • orient=”auto” → arrow automatically rotates to match line direction
  • On <line>: marker-start & marker-end reference it with url(#arrow)

4. More Useful Examples

Example 1: Circle Dot Marker (Polymarker at Corners)

XML

→ Red dots at start, every corner, and end

Example 2: Different Start & End Arrows

XML

→ Blue arrow at start, orange at end, on curved path

Example 3: Marker Scales with Stroke Width (Default Behavior)

If line is thicker → marker auto-scales bigger (because markerUnits=”strokeWidth” default)

5. Teacher’s Tips (Hyderabad Student Style 😄)

  • Always put <marker> in <defs> — keeps it hidden & reusable
  • refX/refY is the anchor point — usually center (width/2) or tip for arrows
  • orient=”auto” → magic rotation; use number like 90 for fixed angle
  • For same fill/stroke as line: use CSS fill: context-fill; stroke: context-stroke; (advanced, supported in modern browsers)
  • Common mistake: Forget url(#id) or wrong refX/refY → marker off-center or wrong rotation
  • Job/freelance: Arrows in diagrams, flowchart tools, data viz (D3.js loves markers), custom bullet points on paths

Understood SVG Markers now? It’s what turns boring lines into professional diagrams! Want next: Animated marker? Custom diamond marker? Mid markers on complex path? Or full flowchart example? Just tell me — we’re making your SVG look pro! 🚀

You may also like...

Leave a Reply

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