CSS stroke-dashoffset Property

The CSS stroke-dashoffset property specifies the starting offset of a dash pattern along the outline of an SVG shape or path.

It works with stroke-dasharray, which defines the lengths of dashes and gaps. Changing the offset shifts the pattern without changing those lengths, making it useful for positioning dashed outlines, animating moving dashes, and creating SVG path-drawing effects.

stroke-dashoffset Syntax

The stroke-dashoffset property accepts a number, length, or percentage representing the offset into an SVG stroke's dash pattern.

Basic Syntax

svg path {
  stroke-dashoffset: 10;
}

Common Values

svg path {
  stroke-dashoffset: 0;
}
svg path {
  stroke-dashoffset: 5;
}
svg path {
  stroke-dashoffset: 15;
}
svg path {
  stroke-dashoffset: -10;
}
svg path {
  stroke-dashoffset: 20px;
}
svg path {
  stroke-dashoffset: 10%;
}
svg path {
  stroke-dashoffset: calc(10px + 5px);
}

CSS-Wide Keywords

svg path {
  stroke-dashoffset: initial;
}
svg path {
  stroke-dashoffset: inherit;
}
svg path {
  stroke-dashoffset: unset;
}
svg path {
  stroke-dashoffset: revert;
}
svg path {
  stroke-dashoffset: revert-layer;
}

The initial value is 0. Unlike dash and gap lengths in stroke-dasharray, dash offsets may be negative.

Property Values

Value Description
<number> Specifies an offset in the current SVG user coordinate system.
<length> Specifies an offset using a CSS length such as px, em, or rem.
<percentage> Specifies an offset relative to the normalized diagonal of the current SVG viewport.
initial Resets the offset to 0.
inherit Uses the computed offset from the parent element.
unset Uses the inherited value because the property is inherited.
revert Reverts the property according to CSS cascade origin rules.
revert-layer Reverts the property according to CSS cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 0
Inherited Yes
Applies to Applicable SVG shapes, paths, and text elements
Computed value As specified, with lengths converted to absolute lengths
Animation type By computed value type
Percentages Relative to the normalized diagonal of the current SVG viewport
Related properties stroke-dasharray, stroke, stroke-width
Specification SVG 2

Valid Examples

svg path {
  stroke-dashoffset: 0;
}
svg path {
  stroke-dashoffset: 12;
}
svg path {
  stroke-dashoffset: -12;
}
svg path {
  stroke-dashoffset: 12px;
}
svg path {
  stroke-dashoffset: 5%;
}
svg path {
  stroke-dashoffset: calc(20px - 5px);
}

Invalid Examples

svg path {
  stroke-dashoffset: auto;
}
svg path {
  stroke-dashoffset: none;
}
svg path {
  stroke-dashoffset: red;
}
svg path {
  stroke-dashoffset: 10 5;
}
svg path {
  stroke-dashoffset: 5deg;
}

The property accepts one numeric, length, or percentage value. Multiple values, color keywords, angles, and unsupported identifiers are invalid.

How It Works

The browser uses stroke-dasharray to establish a repeating sequence of painted and unpainted stroke segments. The stroke-dashoffset property determines where that sequence begins along the path.

Step 1: Create a Dashed Path

<svg viewBox="0 0 300 80">
  <path d="M20 40 H280" fill="none" stroke="#2563eb" stroke-width="5" stroke-dasharray="20 10"/>
</svg>

The path displays repeating 20-unit dashes separated by 10-unit gaps.

Step 2: Apply an Offset

path {
  stroke-dashoffset: 10;
}

The browser advances 10 units into the dash pattern before painting the path's starting point.

Step 3: Change the Offset

path {
  stroke-dashoffset: -10;
}

A negative offset shifts the pattern in the opposite direction.

Complete SVG Example

<svg viewBox="0 0 300 80" role="img" aria-label="Blue dashed line with an offset dash pattern">
  <path d="M20 40 H280" fill="none" stroke="#2563eb" stroke-width="5" stroke-dasharray="20 10" stroke-dashoffset="10"/>
</svg>

Pattern Repetition

For stroke-dasharray: 20 10, the complete repeating pattern measures 30 units. Offsets that differ by 30 units produce equivalent positions within the repeating pattern.

Important Consideration

Changing the offset does not change the dash lengths, gap lengths, or underlying path geometry. It changes only the alignment of the repeating pattern.

Positive and Negative Offsets

Positive and negative values shift the dash pattern in opposite directions along the path.

Zero Offset

.zero-offset {
  stroke-dasharray: 20 10;
  stroke-dashoffset: 0;
}

The pattern begins at its default starting position.

Positive Offset

.positive-offset {
  stroke-dasharray: 20 10;
  stroke-dashoffset: 10;
}

A positive offset advances into the pattern before drawing begins. The visible dashes appear shifted backward relative to the path direction.

Negative Offset

.negative-offset {
  stroke-dasharray: 20 10;
  stroke-dashoffset: -10;
}

A negative offset shifts the pattern forward along the path direction.

Comparing Offset Values

Offset Effect
0 Uses the default dash alignment.
5 Advances 5 units into the pattern.
15 Advances 15 units into the pattern.
-5 Shifts the pattern 5 units forward.
-15 Shifts the pattern 15 units forward.

Offset Direction and Path Direction

The apparent movement depends on the direction in which the SVG path is defined. Reversing the path direction also changes the visual direction of the offset.

Units and Percentages

The stroke-dashoffset property supports unitless numbers, CSS lengths, and percentages.

Unitless Numbers

svg path {
  stroke-dashoffset: 15;
}

Unitless values are interpreted in the current SVG user coordinate system.

Pixel Lengths

svg path {
  stroke-dashoffset: 15px;
}

CSS lengths provide an explicit measurement unit.

Relative Lengths

svg path {
  stroke-dashoffset: 1em;
}

Relative units can make dash offsets respond to font-size or other CSS measurement contexts.

Percentage Values

svg path {
  stroke-dashoffset: 10%;
}

Percentages are relative to the normalized diagonal of the current SVG viewport, not directly to the path length.

Normalized Diagonal

The normalized diagonal is calculated using the viewport width and height:

Normalized diagonal = sqrt(width² + height²) / sqrt(2)

For an SVG viewport measuring 200 by 100 user units, the normalized diagonal is approximately 158.11 units. An offset of 10% therefore represents approximately 15.81 units.

CSS Calculations

svg path {
  stroke-dashoffset: calc(20px - 5px);
}

CSS calculations can combine compatible numeric values and units.

Important Consideration

Use unitless numbers or explicit lengths when the offset must align precisely with a known dash pattern. Percentage values are useful when viewport-relative behavior is intended.

Using stroke-dasharray

The stroke-dasharray and stroke-dashoffset properties are closely related but serve different purposes.

Property Purpose Example
stroke-dasharray Defines the lengths of dashes and gaps. 20 10
stroke-dashoffset Shifts the starting position of the dash pattern. 15

Define a Pattern

.dashed-path {
  stroke: #2563eb;
  stroke-width: 4;
  stroke-dasharray: 20 10;
}

The pattern consists of a 20-unit dash followed by a 10-unit gap.

Shift the Pattern

.dashed-path {
  stroke: #2563eb;
  stroke-width: 4;
  stroke-dasharray: 20 10;
  stroke-dashoffset: 15;
}

The dash and gap lengths remain unchanged, but the pattern begins at a different position.

No Dash Pattern

.solid-path {
  stroke: #2563eb;
  stroke-width: 4;
  stroke-dasharray: none;
  stroke-dashoffset: 15;
}

The offset has no visible effect because the stroke is continuous.

Pattern Length

For a dash list of 20 10, the pattern length is 30 units. For 20 5 5 5, the pattern length is 35 units.

Understanding the complete pattern length is useful when creating seamless dash animations.

Closed Paths and Shapes

The stroke-dashoffset property works with both open and closed SVG paths, including circles, rectangles, and polygons.

Dashed Circle

<circle cx="60" cy="60" r="40" fill="none" stroke="#2563eb" stroke-width="5" stroke-dasharray="15 8" stroke-dashoffset="10"/>

The dash pattern follows the circumference of the circle.

Dashed Rectangle

<rect x="20" y="20" width="160" height="100" fill="none" stroke="#0d9488" stroke-width="5" stroke-dasharray="20 10" stroke-dashoffset="15"/>

The dash pattern follows the rectangle's perimeter.

Dashed Polygon

<polygon points="100,15 185,150 15,150" fill="none" stroke="#a855f7" stroke-width="5" stroke-dasharray="12 6" stroke-dashoffset="-8"/>

The dash pattern follows the connected polygon edges.

Pattern Alignment at the Closing Point

On a closed shape, the repeating pattern may not align perfectly where the path closes. The result depends on the total path length and dash pattern length.

Adjusting the Alignment

Changing stroke-dashoffset can reposition the visible seam or distribute the pattern differently around a closed shape.

Important Consideration

For precisely spaced dashes around closed shapes, consider the perimeter and complete dash pattern length. An offset changes alignment but does not change the total number or lengths of dashes.

Animating Dash Offsets

Animating stroke-dashoffset creates the appearance of dashes moving along an SVG path.

Continuous Dash Movement

@keyframes move-dash-pattern {
  from {
    stroke-dashoffset: 0;
  }
  to {
    stroke-dashoffset: -30;
  }
}
.moving-dashes {
  fill: none;
  stroke: #2563eb;
  stroke-width: 4;
  stroke-dasharray: 20 10;
  animation: move-dash-pattern 2s linear infinite;
}

The animation shifts the pattern by 30 units, matching its complete repeating length and producing a seamless loop.

Reverse Direction

@keyframes reverse-dashes {
  from {
    stroke-dashoffset: 0;
  }
  to {
    stroke-dashoffset: 30;
  }
}
.reverse-dashes {
  stroke-dasharray: 20 10;
  animation: reverse-dashes 2s linear infinite;
}

Changing the offset direction reverses the apparent movement.

CSS Transition

.interactive-dashes {
  stroke-dasharray: 20 10;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.5s ease;
}
.interactive-dashes:hover {
  stroke-dashoffset: 15;
}

The pattern shifts smoothly when the path is hovered.

Reduced Motion

@media (prefers-reduced-motion: reduce) {
  .moving-dashes,
  .reverse-dashes {
    animation: none;
  }
  .interactive-dashes {
    transition: none;
  }
}

Disable decorative dash movement for users who prefer reduced motion.

Important Consideration

For seamless looping animations, animate the offset by the complete dash pattern length or an exact multiple of that length.

Path-Drawing Effects

A common SVG animation technique uses stroke-dasharray and stroke-dashoffset to reveal a path progressively.

Step 1: Define a Path

<svg viewBox="0 0 240 80">
  <path class="drawing-path" d="M20 40 H220"/>
</svg>

The horizontal path is 200 user units long.

Step 2: Define a Dash Equal to the Path Length

.drawing-path {
  fill: none;
  stroke: #2563eb;
  stroke-width: 5;
  stroke-dasharray: 200 200;
}

The pattern contains a 200-unit dash followed by a 200-unit gap.

Step 3: Hide the Stroke

.drawing-path {
  stroke-dashoffset: 200;
}

The offset positions the gap over the visible path, hiding the stroke.

Step 4: Animate the Offset

@keyframes reveal-path {
  from {
    stroke-dashoffset: 200;
  }
  to {
    stroke-dashoffset: 0;
  }
}
.drawing-path {
  fill: none;
  stroke: #2563eb;
  stroke-width: 5;
  stroke-dasharray: 200 200;
  stroke-dashoffset: 200;
  animation: reveal-path 2s ease forwards;
}

The stroke becomes visible progressively as the offset moves toward zero.

Using Other Path Lengths

For a different path, adjust the dash and offset values to match its length. JavaScript can measure an SVG geometry element using getTotalLength() when a dynamic measurement is required.

Reduced-Motion Fallback

@media (prefers-reduced-motion: reduce) {
  .drawing-path {
    animation: none;
    stroke-dashoffset: 0;
  }
}

The complete path remains visible when animation is disabled.

Practical Examples

Shift a Dashed Line

.shifted-line {
  fill: none;
  stroke: #2563eb;
  stroke-width: 4;
  stroke-dasharray: 12 6;
  stroke-dashoffset: 9;
}

Shifts the starting position of a dashed line.

Reverse a Dash Pattern

.reverse-pattern {
  fill: none;
  stroke: #0d9488;
  stroke-width: 4;
  stroke-dasharray: 15 5;
  stroke-dashoffset: -10;
}

Moves the pattern forward along the path direction.

Offset a Dotted Circle

.dotted-circle {
  fill: none;
  stroke: #a855f7;
  stroke-width: 6;
  stroke-linecap: round;
  stroke-dasharray: 0 12;
  stroke-dashoffset: 6;
}

Changes where the dots begin around the circle.

Align a Dashed Rectangle

.dashed-rectangle {
  fill: none;
  stroke: #ec4899;
  stroke-width: 4;
  stroke-dasharray: 20 10;
  stroke-dashoffset: 15;
}

Adjusts the dash alignment around a rectangular outline.

Animated Dashed Border

.animated-border {
  fill: none;
  stroke: #2563eb;
  stroke-width: 4;
  stroke-dasharray: 10 5;
  animation: move-border 1.5s linear infinite;
}
@keyframes move-border {
  to {
    stroke-dashoffset: -15;
  }
}

Creates a continuously moving dashed SVG outline.

Static Dash Alignment

.static-pattern {
  stroke-dasharray: 20 10;
  stroke-dashoffset: 0;
}

Restores the default alignment.

Reset the Offset

.reset-offset {
  stroke-dashoffset: initial;
}

Resets the property to 0.

stroke-dashoffset Example

This example compares four identical dashed SVG lines with different offset values. The dash lengths and gaps remain the same, but each pattern begins at a different position.

HTML Example

<div class="stroke-dashoffset-demo">
  <svg viewBox="0 0 600 260" role="img" aria-label="Four dashed horizontal lines with offsets of zero, positive ten, negative ten, and positive twenty">
    <path class="offset-zero" d="M30 40 H570"/>
    <path class="offset-positive" d="M30 100 H570"/>
    <path class="offset-negative" d="M30 160 H570"/>
    <path class="offset-large" d="M30 220 H570"/>
  </svg>
</div>

CSS Example

.stroke-dashoffset-demo svg {
  display: block;
  width: 100%;
  height: auto;
}
.stroke-dashoffset-demo path {
  fill: none;
  stroke-width: 6;
  stroke-dasharray: 20 10;
}
.stroke-dashoffset-demo .offset-zero {
  stroke: #2563eb;
  stroke-dashoffset: 0;
}
.stroke-dashoffset-demo .offset-positive {
  stroke: #0d9488;
  stroke-dashoffset: 10;
}
.stroke-dashoffset-demo .offset-negative {
  stroke: #a855f7;
  stroke-dashoffset: -10;
}
.stroke-dashoffset-demo .offset-large {
  stroke: #ec4899;
  stroke-dashoffset: 20;
}
Browser Result

Each line uses the same 20 10 dash pattern. The different offsets change which part of the pattern appears at the beginning of each line.

Comparing the Offsets

Line Offset Effect
Blue 0 Default dash alignment
Teal 10 Pattern advanced by 10 units
Purple -10 Pattern shifted forward by 10 units
Pink 20 Pattern advanced by 20 units

Changing the Positive Offset

.stroke-dashoffset-demo .offset-positive {
  stroke-dashoffset: 15;
}

Changes where the teal dash pattern begins.

Reversing the Pink Offset

.stroke-dashoffset-demo .offset-large {
  stroke-dashoffset: -20;
}

Shifts the pink dash pattern in the opposite direction.

Changing the Dash Pattern

.stroke-dashoffset-demo path {
  stroke-dasharray: 10 5;
}

Changes the dash lengths and gaps for all four lines while preserving their different offset values.

Removing the Dash Pattern

.stroke-dashoffset-demo path {
  stroke-dasharray: none;
}

All four lines become continuous strokes, and their offset differences are no longer visible.

Accessibility Considerations

Dash offsets are often used in decorative SVG animations, but they can also affect diagrams, charts, and other meaningful graphics.

Maintain Sufficient Contrast

Ensure dashed strokes remain visible against their backgrounds, especially when gaps occupy a large portion of the path.

Avoid Relying Only on Animation

Do not use moving dash patterns as the only indication of progress, status, or selection.

Respect Reduced-Motion Preferences

Continuous movement can be distracting. Disable decorative dash animations when users request reduced motion.

Keep Essential Paths Visible

For path-drawing animations, provide a fallback that displays the complete path when animation is unavailable or disabled.

Provide Accessible SVG Names

Informative SVG graphics should include accessible names or equivalent descriptions.

<svg viewBox="0 0 200 60" role="img" aria-label="Blue dashed horizontal line with an offset pattern">
  <path d="M10 30 H190" fill="none" stroke="blue" stroke-width="4" stroke-dasharray="12 6" stroke-dashoffset="9"/>
</svg>

Use Additional Visual Cues

When different dash alignments communicate meaning, provide labels or other distinguishing information.

Test Different Display Sizes

Verify that dashed outlines remain recognizable when SVG graphics are resized or zoomed.

Common Mistakes

Using an Offset Without a Dash Pattern

svg path {
  stroke: blue;
  stroke-dashoffset: 10;
}

Without a dash pattern, the offset has no visible effect. Define stroke-dasharray first.

Using Multiple Offset Values

svg path {
  stroke-dashoffset: 10 5;
}

The property accepts only one offset value. Use stroke-dasharray to define multiple dash and gap lengths.

Using none as an Offset

svg path {
  stroke-dashoffset: none;
}

The keyword none is not valid for stroke-dashoffset. Use 0 to reset the offset.

Confusing Positive and Negative Directions

Positive offsets advance into the pattern, while negative offsets shift it forward along the path direction. The apparent movement also depends on the path's direction.

Assuming Percentages Use Path Length

Percentage offsets are based on the normalized diagonal of the SVG viewport, not directly on the path length.

Forgetting to Define a Stroke

The initial stroke value is none. A dash pattern and offset are not visible unless the path has a painted stroke.

Using the Wrong Animation Distance

For seamless dash movement, the animation distance should match the complete repeating pattern length or an exact multiple.

Using Incorrect Path Lengths

Path-drawing effects require dash and offset values that correspond to the path length. Incorrect values may leave parts of the stroke visible or prevent the entire path from appearing.

Ignoring Reduced Motion

Continuous dash movement should have a static alternative for users who prefer reduced motion.

Expecting Offset to Change Dash Lengths

The offset changes pattern alignment only. It does not modify dash lengths or gap lengths.

Important Considerations

Keep the following points in mind when using stroke-dashoffset:

  • It shifts the starting position of an SVG dash pattern.
  • The initial value is 0.
  • The property is inherited.
  • It accepts a number, length, or percentage.
  • Positive and negative values are valid.
  • It accepts only one offset value.
  • Unitless numbers use the current SVG user coordinate system.
  • Percentages are based on the normalized diagonal of the SVG viewport.
  • It works with stroke-dasharray.
  • It does not change dash lengths or gap lengths.
  • It has no visible effect on a continuous stroke.
  • A painted stroke is required for the pattern to be visible.
  • The apparent direction depends on the SVG path direction.
  • Offsets repeat according to the complete dash pattern length.
  • It works with open and closed SVG paths.
  • It can adjust dash alignment around circles and rectangles.
  • It can be animated to create moving dash patterns.
  • Seamless loops should use the complete dash pattern length.
  • It can be combined with stroke-dasharray for path-drawing effects.
  • Path-drawing effects require appropriate path length measurements.
  • Decorative animations should respect reduced-motion preferences.
  • Important SVG graphics should remain visible without animation.
  • Test dash offsets across supported browsers and display sizes.

Browser Support

The CSS stroke-dashoffset property is widely supported for SVG graphics in modern browsers, including Chrome, Edge, Firefox, and Safari.

Basic numeric offsets, negative values, and CSS animations are broadly supported. Percentage values and more complex CSS calculations should be tested when compatibility with older browsers is required.

For current compatibility information, visit Can I Use? and search for stroke-dashoffset.

Feature Detection

@supports (stroke-dashoffset: 10) {
  .offset-outline {
    stroke-dashoffset: 10;
  }
}

Feature queries can test whether a browser recognizes the property and value syntax.

Fallback Behavior

If a declaration is invalid or unsupported, the browser ignores it. The element uses another applicable value from the cascade or its inherited value, falling back to 0 when no other value applies.

Testing Recommendations

Test positive and negative offsets, percentage values, closed-path alignment, and dash animations in the browsers you support.

Specifications

The stroke-dashoffset property is defined in the SVG 2 specification.

SVG 2 - stroke-dashoffset Property

The specification defines how the dash pattern offset is interpreted and applied along SVG paths.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: stroke-dashoffset