Chapter 10 of 11Part 4 · Finish

SVG line animation: make a path draw itself

Make an SVG path draw itself with stroke-dasharray and stroke-dashoffset, in CSS or GSAP, without confusing screen readers.

9 minBy Zuzo StudioUpdated

9 min

In this chapter you will

  • explain how a dash as long as the path makes a line appear to draw
  • set up a path with pathLength="1" or getTotalLength() and animate it in CSS and GSAP
  • hide a decorative SVG from screen readers and keep it visible without JavaScript

Before you startThis builds on Scroll animation with GSAP and CSS.

Check your understanding

3 questions

Q1A path has pathLength="1" and stroke-dasharray: 1. Which stroke-dashoffset value shows the whole line?

Show the answer

0 With no offset, the one dash covers the path from start to end, so the line looks solid.

Q2Why does the CSS version keep the line fully drawn in the base rule and only hide it under a .js class?

Show the answer

So the underline still appears if the script fails or never loads The finished state is the default. Animation is added on top, which is progressive enhancement applied to motion.

Q3A hand drawn underline sits under a word in a heading. What should a screen reader announce for the SVG?

Show the answer

Nothing; mark the svg with aria-hidden="true" The underline is decoration. The word it marks is already read out, so the SVG should be hidden from assistive technology.

Key takeaways

  1. A line draws itself when its dash is as long as the path and stroke-dashoffset animates from that length to 0.
  2. pathLength="1" makes every path count as 1 unit long, so one set of CSS values works for any path without measuring.
  3. getTotalLength() measures a path in viewBox units when you need the real length in JavaScript.
  4. Leave the line drawn in the base CSS and hide it only when the animation is about to run, so it survives a failed script.
  5. A decorative SVG gets aria-hidden="true"; one that carries meaning gets role="img" and a label.
Up next · Chapter 11 · 9 min Prefers reduced motion: animation for every reader Respect readers who ask for less motion: what WCAG asks, how to read the setting in CSS and GSAP, and what to keep or remove.

02FAQ

Questions on this chapter

Something else on your mind? Email the studio and a designer, not a salesperson, replies within a day.

How do you animate an SVG line drawing?

Set stroke-dasharray to the length of the path so one dash covers it, set stroke-dashoffset to the same length to hide it, then animate stroke-dashoffset to 0. Adding pathLength="1" to the path lets you use 1 instead of measuring.

Can you animate an SVG path with only CSS?

Yes. stroke-dashoffset can be animated with a CSS transition or @keyframes, and with pathLength="1" you do not need any JavaScript to measure the path.