Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Properties for Special Contexts

The stroke-dashoffset property

Reference for the CSS stroke-dashoffset property: shift where an SVG dash pattern begins, animate marching ants and draw lines on, with examples.

The stroke-dashoffset property moves the starting point of the dash pattern created by stroke-dasharray. A positive offset shifts the pattern backwards along the path, as if you started that far into it; a negative offset shifts it forwards.

On its own it only slides a pattern, but animated it creates two classics: "marching ants" borders and the effect of a line being drawn on screen.

CSS
path {
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
}

Try it

Both lines below are animated with CSS only. The top one slides a dash pattern; the bottom one draws itself on, then repeats.

Marching dashes and a line that draws itself

Syntax

CSS
stroke-dashoffset: <length-percentage> | <number>;
  • Initial value: 0
  • Applies to: SVG shapes and text content elements
  • Inherited: yes
  • Animatable: yes
  • Computed value: an absolute length, or a percentage as specified

Values

Value Description
<number> An offset in user units. Positive moves the pattern toward the start of the path, negative toward the end.
<length> A CSS length.
<percentage> A percentage of the viewport-based reference size.

Usage notes

  • Line drawing. Make dash and gap at least as long as the path (stroke-dasharray: L), start with stroke-dashoffset: L so only the gap shows, then animate to 0. pathLength="1" on the SVG element lets you use 1 instead of measuring L.
  • Direction. To make the marching dashes run the other way, flip the sign of the animated offset.
  • The loop length of a seamless march equals the sum of the dash pattern (here 14 + 8 = 22).
  • Respect motion preferences. Wrap endless animations in a prefers-reduced-motion check, as the example does.
  • Needs a stroke and a dash pattern; without stroke-dasharray the offset changes nothing.
  • Performance. Animating the offset repaints the SVG each frame; this is fine for a few paths but expensive for large drawings.

Browser support

Supported in every major browser.