Chapter 13: SVG textPath

SVG textPath is a special child element that lives inside <text>. It lets you make text follow the shape of a <path> (the powerful d-attribute path we learned earlier). This is how you get cool effects like:

  • Text curved around a circle (like a logo ring)
  • Text waving along a sine curve
  • Text following an arc, diagonal, or custom shape
  • Animated text sliding along a path

It’s vector magic — text stays sharp when zoomed, scalable for logos, icons, titles, decorative web elements, charts labels, etc.

1. Basic Structure (How It Works)

You need three parts:

  1. Define a <path> inside <defs> (give it an id)
  2. Use <text> as parent
  3. Put <textPath> inside <text>, with href=”#id-of-path” (or xlink:href in older code)

Simple skeleton:

XML
  • <defs> hides the path (you don’t want to see the invisible guide line usually)
  • href=”#myCurve” → links to the path’s id (modern browsers use href; old ones used xlink:href)
  • Text inside <textPath> follows the path’s direction & shape

2. Your First <textPath> Example (Copy-Paste Right Now!)

Save as textpath.html:

HTML

What you see: Text starts from left, follows the upward-then-downward curve smoothly.

  • Path d=”M 50 200 Q 250 50 450 200″ → start at (50,200), curve to control (250,50), end at (450,200)
  • Text follows exactly that shape
  • Added dashed gray line so you see the “invisible road” the text is walking on

Zoom browser — text + curve stays perfect!

3. Important Attributes for <textPath>

Attribute Meaning Common Values Notes
href (or xlink:href) Reference to the <path> id “#myPath” Required! Modern: href; old: xlink:href
startOffset Shift text start position along path “0%”, “50%”, “20”, “-10%” % of path length or absolute units — most used!
method How glyphs (letters) follow path “align” (default), “stretch” align = normal; stretch = deform letters to fit
spacing Letter spacing adjustment “auto” (default), “exact” auto = natural; exact = fixed spacing
lengthAdjust How to fit text if too long/short “spacing” (default), “spacingAndGlyphs” spacing = adjust gaps; glyphs = stretch/compress letters
textLength Force total text length “300”, “80%” Used with lengthAdjust

4. More Fun & Useful Examples

Example 1: Text Around a Circle (Classic Ring Logo)

XML
  • A command makes full ellipse (circle here)
  • startOffset=”25%” → starts a quarter way around (top-ish)
  • Add another <textPath> for bottom text if needed

Example 2: Start from Middle + Offset

XML

→ Text begins halfway along the path

Example 3: Text on Straight Line (Like Normal but Flexible)

XML

Example 4: Combine with <tspan> for Style Changes

XML

→ Style parts differently even on curve

5. Teacher’s Tips (Hyderabad Style 😄)

  • Path direction matters — text follows path from start to end (use lowercase commands for relative if needed)
  • If text overflows path → clips at end (or use textLength to squeeze)
  • Browser differences: Chrome/Firefox good; older IE/Edge might need xlink:href
  • For reverse direction: Make path go opposite way or use negative startOffset
  • Animation idea: Animate startOffset from “0%” to “100%” → text slides along path!
  • Common mistake: Forget # in href → text invisible! Or no <defs> → path visible
  • Real uses: Curved titles, circular badges, wavy slogans, map labels, decorative web art

Got <textPath> clear now? It’s one of the coolest SVG features! Want next: Animated sliding text? Text on heart path? Full circular logo example? Or combine with gradients/filters? Just say — we’re making you an SVG pro! 🚀

You may also like...

Leave a Reply

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