CSS stroke-dasharray Property
The CSS stroke-dasharray property defines a repeating pattern of dashes and gaps along the outline of an SVG shape or path.
It accepts a list of numbers or lengths that alternate between painted and unpainted portions of a stroke. Combined with stroke, stroke-width, and stroke-linecap, it can create dashed lines, dotted outlines, and decorative SVG borders.
stroke-dasharray Syntax
The stroke-dasharray property accepts none or a list of non-negative numbers, lengths, and percentages.
Basic Syntax
svg path {
stroke-dasharray: 10 5;
}
This creates a repeating pattern of 10-unit dashes separated by 5-unit gaps.
Common Values
svg path {
stroke-dasharray: none;
}
svg path {
stroke-dasharray: 5;
}
svg path {
stroke-dasharray: 10 5;
}
svg path {
stroke-dasharray: 10, 5;
}
svg path {
stroke-dasharray: 15 5 3 5;
}
svg path {
stroke-dasharray: 10px 5px;
}
svg path {
stroke-dasharray: 5% 2%;
}
CSS-Wide Keywords
svg path {
stroke-dasharray: initial;
}
svg path {
stroke-dasharray: inherit;
}
svg path {
stroke-dasharray: unset;
}
svg path {
stroke-dasharray: revert;
}
svg path {
stroke-dasharray: revert-layer;
}
Dash values may be separated by spaces or commas. The browser repeats the resulting pattern along the path.
Property Values
| Value | Description |
|---|---|
none |
Draws a continuous stroke without a dash pattern. This is the initial value. |
<number> |
Specifies dash or gap lengths in the current SVG user coordinate system. |
<length> |
Specifies dash or gap lengths using CSS length units such as px. |
<percentage> |
Specifies dash or gap lengths relative to the normalized diagonal of the current SVG viewport. |
initial |
Resets the property to none. |
inherit |
Uses the computed dash pattern 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 | none |
| Inherited | Yes |
| Applies to | Applicable SVG shapes, paths, and text elements |
| Computed value | As specified, with lengths converted to absolute lengths |
| Animation type | Repeatable list |
| Percentages | Relative to the normalized diagonal of the current SVG viewport |
| Related properties | stroke, stroke-width, stroke-dashoffset |
| Specification | SVG 2 |
Valid Examples
svg path {
stroke-dasharray: 5 5;
}
svg path {
stroke-dasharray: 10, 5;
}
svg path {
stroke-dasharray: 12 4 3 4;
}
svg path {
stroke-dasharray: 10px 5px;
}
svg path {
stroke-dasharray: 5% 3%;
}
svg path {
stroke-dasharray: 0 8;
}
svg path {
stroke-dasharray: calc(5px + 5px) 5px;
}
Invalid Examples
svg path {
stroke-dasharray: -5 10;
}
svg path {
stroke-dasharray: solid;
}
svg path {
stroke-dasharray: auto;
}
svg path {
stroke-dasharray: red blue;
}
svg path {
stroke-dasharray: 10px -5px;
}
Negative dash or gap lengths invalidate the declaration. Color keywords, border styles, and unsupported identifiers are not valid values.
How It Works
The browser measures distances along an SVG path and alternates between painted segments and gaps according to the values in stroke-dasharray.
Step 1: Create a Path
<svg viewBox="0 0 300 80">
<path d="M20 40 H280"/>
</svg>
This creates a horizontal SVG path.
Step 2: Define the Stroke
path {
fill: none;
stroke: #2563eb;
stroke-width: 4;
}
The path now has a continuous blue outline.
Step 3: Add a Dash Pattern
path {
fill: none;
stroke: #2563eb;
stroke-width: 4;
stroke-dasharray: 12 6;
}
The path displays repeating 12-unit dashes and 6-unit gaps.
Step 4: Repeat Along the Path
The pattern repeats automatically until the entire path has been processed. If the path ends partway through a dash or gap, the final segment is shortened.
Complete SVG Example
<svg viewBox="0 0 300 80" role="img" aria-label="Blue dashed horizontal line">
<path d="M20 40 H280" fill="none" stroke="#2563eb" stroke-width="4" stroke-dasharray="12 6"/>
</svg>
Important Consideration
The dash pattern follows the path geometry. Curves, circles, rectangles, and irregular paths can all use stroke-dasharray.
Dash Patterns
Different combinations of dash and gap lengths produce different visual styles.
Equal Dashes and Gaps
.equal-dashes {
stroke-dasharray: 10 10;
}
Creates dashes and gaps of equal length.
Long Dashes, Short Gaps
.long-dashes {
stroke-dasharray: 20 5;
}
Creates longer painted segments with narrow gaps.
Short Dashes, Long Gaps
.short-dashes {
stroke-dasharray: 5 15;
}
Creates short painted segments separated by larger gaps.
Alternating Dash Lengths
.mixed-dashes {
stroke-dasharray: 20 5 5 5;
}
Creates a repeating sequence of one long dash, one short gap, one short dash, and another short gap.
Dense Dash Pattern
.dense-dashes {
stroke-dasharray: 3 3;
}
Creates a closely spaced dashed outline.
Wide Dash Pattern
.wide-dashes {
stroke-dasharray: 30 15;
}
Creates widely spaced dashes.
Continuous Stroke
.solid-stroke {
stroke-dasharray: none;
}
Removes the dash pattern and restores a continuous stroke.
Odd and Even Value Lists
The number of values in a dash list determines how the repeating pattern is constructed.
Two Values
svg path {
stroke-dasharray: 10 5;
}
The first value specifies the dash length and the second specifies the gap length.
Four Values
svg path {
stroke-dasharray: 20 5 5 5;
}
The list alternates between dashes and gaps, allowing different dash lengths within one repeating pattern.
One Value
svg path {
stroke-dasharray: 10;
}
An odd-length list is repeated to produce an even-length pattern. This single value behaves like 10 10.
Three Values
svg path {
stroke-dasharray: 10 5 3;
}
The browser repeats the three values, producing the equivalent pattern:
svg path {
stroke-dasharray: 10 5 3 10 5 3;
}
Why Odd Lists Repeat
Repeating an odd-length list ensures the pattern alternates correctly between painted and unpainted segments when it begins again.
Important Consideration
Odd-length lists are valid. You do not need to manually duplicate them, although explicitly listing the complete pattern can make complex designs easier to understand.
Units and Percentages
Dash lengths can be expressed using unitless numbers, CSS lengths, or percentages.
Unitless Numbers
svg path {
stroke-dasharray: 10 5;
}
Unitless numbers are interpreted in the current SVG user coordinate system.
Pixel Lengths
svg path {
stroke-dasharray: 10px 5px;
}
CSS lengths can specify dash and gap sizes.
Percentage Values
svg path {
stroke-dasharray: 5% 2%;
}
Percentages are resolved against the normalized diagonal of the current SVG viewport, not directly against the path length.
Normalized Diagonal
The normalized diagonal is calculated from the viewport width and height:
Normalized diagonal = sqrt(width² + height²) / sqrt(2)
This provides a consistent reference for percentage-based stroke lengths.
Mixing Values
svg path {
stroke-dasharray: 10px 2%;
}
Numbers, lengths, and percentages may be combined in a single list.
Important Consideration
Percentage values can behave differently from what you might expect if you assume they represent percentages of the path length. Use unitless numbers or lengths when exact dash sizes are important.
Creating Dotted Strokes
Combining short dash lengths with rounded line caps can create a dotted appearance.
Round-Capped Short Dashes
.dotted-line {
fill: none;
stroke: #2563eb;
stroke-width: 6;
stroke-linecap: round;
stroke-dasharray: 1 12;
}
The short painted segments receive rounded caps, making them appear as small dots.
Zero-Length Dashes
.round-dots {
fill: none;
stroke: #0d9488;
stroke-width: 6;
stroke-linecap: round;
stroke-dasharray: 0 12;
}
With round line caps, zero-length dash segments can render as circular dots.
Changing Dot Spacing
.close-dots {
stroke-dasharray: 0 8;
}
.wide-dots {
stroke-dasharray: 0 18;
}
Smaller gap values create closer dots, while larger gap values increase their spacing.
Changing Dot Size
.small-dots {
stroke-width: 3;
stroke-linecap: round;
stroke-dasharray: 0 10;
}
.large-dots {
stroke-width: 8;
stroke-linecap: round;
stroke-dasharray: 0 16;
}
The stroke-width property affects the diameter of dots created using round line caps.
Important Consideration
Zero-length dashes do not produce visible dots with every line cap style. Use stroke-linecap: round for this technique.
Using stroke-dashoffset
The stroke-dashoffset property shifts the starting position of a dash pattern along an SVG path.
Basic Dash Offset
.offset-line {
stroke-dasharray: 12 6;
stroke-dashoffset: 6;
}
The dash pattern is shifted by 6 units.
Negative Offset
.reverse-offset {
stroke-dasharray: 12 6;
stroke-dashoffset: -6;
}
A negative value shifts the pattern in the opposite direction.
Changing the Pattern Position
.pattern-start {
stroke-dasharray: 20 10;
stroke-dashoffset: 0;
}
.pattern-shifted {
stroke-dasharray: 20 10;
stroke-dashoffset: 15;
}
Both paths use the same dash lengths but begin at different positions in the pattern.
Important Consideration
The stroke-dasharray property defines the dash and gap lengths. The stroke-dashoffset property controls where that repeating pattern begins.
Animating Dash Patterns
Dash patterns can be animated to create moving outlines, drawing effects, and progress indicators.
Moving Dashes
@keyframes move-dashes {
from {
stroke-dashoffset: 0;
}
to {
stroke-dashoffset: -30;
}
}
.moving-dashes {
fill: none;
stroke: #2563eb;
stroke-width: 4;
stroke-dasharray: 20 10;
animation: move-dashes 2s linear infinite;
}
The dash pattern appears to move continuously along the path.
Animating Dash Lengths
@keyframes change-dashes {
from {
stroke-dasharray: 5 15;
}
to {
stroke-dasharray: 15 5;
}
}
.changing-dashes {
animation: change-dashes 2s ease-in-out infinite alternate;
}
The lengths of painted segments and gaps change over time.
Path Drawing Effect
@keyframes draw-path {
from {
stroke-dashoffset: 200;
}
to {
stroke-dashoffset: 0;
}
}
.drawn-path {
fill: none;
stroke: #0d9488;
stroke-width: 4;
stroke-dasharray: 200 200;
stroke-dashoffset: 200;
animation: draw-path 2s ease forwards;
}
For a path that is 200 user units long, this technique can reveal the stroke progressively. Other path lengths require corresponding dash values.
Reduced Motion
@media (prefers-reduced-motion: reduce) {
.moving-dashes,
.changing-dashes,
.drawn-path {
animation: none;
}
.drawn-path {
stroke-dashoffset: 0;
}
}
Disable decorative dash animations for users who prefer reduced motion. Ensure essential paths remain visible when animation is removed.
Important Consideration
Dash pattern animation depends on the values being interpolated. Compatible lists of lengths or numbers can animate smoothly, while transitions involving none may not produce a continuous visual change.
Practical Examples
Dashed Horizontal Line
.dashed-line {
fill: none;
stroke: #2563eb;
stroke-width: 4;
stroke-dasharray: 12 6;
}
Creates a standard dashed SVG line.
Dotted Circle
.dotted-circle {
fill: none;
stroke: #0d9488;
stroke-width: 6;
stroke-linecap: round;
stroke-dasharray: 0 12;
}
Creates a circular outline made of dots.
Dashed Rectangle
.dashed-rectangle {
fill: none;
stroke: #a855f7;
stroke-width: 4;
stroke-dasharray: 15 5;
}
Creates a dashed outline around an SVG rectangle.
Mixed Dash Pattern
.mixed-pattern {
fill: none;
stroke: #ec4899;
stroke-width: 5;
stroke-dasharray: 20 5 5 5;
}
Alternates long and short dashes.
Fine Dashed Outline
.fine-dashes {
fill: none;
stroke: #475569;
stroke-width: 2;
stroke-dasharray: 3 3;
}
Creates a fine dashed outline suitable for diagrams.
Wide-Spaced Dashes
.wide-spaced-dashes {
fill: none;
stroke: #2563eb;
stroke-width: 5;
stroke-dasharray: 25 15;
}
Creates long dashes separated by wide gaps.
Offset Dash Pattern
.offset-pattern {
fill: none;
stroke: #0d9488;
stroke-width: 4;
stroke-dasharray: 12 6;
stroke-dashoffset: 9;
}
Shifts the position of the dash pattern.
Continuous Outline
.continuous-outline {
fill: none;
stroke: #2563eb;
stroke-width: 4;
stroke-dasharray: none;
}
Removes the dash pattern and draws a continuous stroke.
stroke-dasharray Example
This example compares four SVG lines using different dash patterns. Each line has the same stroke width, making the differences in dash and gap lengths easier to see.
HTML Example
<div class="stroke-dasharray-demo">
<svg viewBox="0 0 600 260" role="img" aria-label="Four horizontal lines showing equal dashes, long dashes, mixed dashes, and round dots">
<path class="dash-equal" d="M30 40 H570"/>
<path class="dash-long" d="M30 100 H570"/>
<path class="dash-mixed" d="M30 160 H570"/>
<path class="dash-dotted" d="M30 220 H570"/>
</svg>
</div>
CSS Example
.stroke-dasharray-demo svg {
display: block;
width: 100%;
height: auto;
}
.stroke-dasharray-demo path {
fill: none;
stroke-width: 6;
}
.stroke-dasharray-demo .dash-equal {
stroke: #2563eb;
stroke-dasharray: 10 10;
}
.stroke-dasharray-demo .dash-long {
stroke: #0d9488;
stroke-dasharray: 25 8;
}
.stroke-dasharray-demo .dash-mixed {
stroke: #a855f7;
stroke-dasharray: 20 5 5 5;
}
.stroke-dasharray-demo .dash-dotted {
stroke: #ec4899;
stroke-linecap: round;
stroke-dasharray: 0 14;
}
The blue line uses equal dash and gap lengths, the teal line uses long dashes, the purple line alternates long and short dashes, and the pink line uses rounded dots.
Comparing the Patterns
| Line | Dash Pattern | Appearance |
|---|---|---|
| Blue | 10 10 |
Equal dashes and gaps |
| Teal | 25 8 |
Long dashes with short gaps |
| Purple | 20 5 5 5 |
Alternating long and short dashes |
| Pink | 0 14 |
Round dots |
Changing Dash Lengths
.stroke-dasharray-demo .dash-equal {
stroke-dasharray: 5 5;
}
Creates shorter dashes and gaps in the first line.
Increasing Dash Spacing
.stroke-dasharray-demo .dash-long {
stroke-dasharray: 25 20;
}
Increases the spacing between the teal dashes.
Changing Dot Spacing
.stroke-dasharray-demo .dash-dotted {
stroke-dasharray: 0 20;
}
Increases the spacing between the pink dots.
Removing a Dash Pattern
.stroke-dasharray-demo .dash-mixed {
stroke-dasharray: none;
}
Changes the purple dashed line into a continuous stroke.
Accessibility Considerations
Dash patterns can help distinguish SVG lines and shapes, especially when color alone is not sufficient.
Use Dash Patterns as Additional Cues
Different dash patterns can help distinguish chart lines, diagram connections, and other graphics without relying only on color.
Maintain Visible Stroke Widths
Very thin dashed strokes may be difficult to see. Choose appropriate stroke widths and dash lengths.
Avoid Excessively Dense Patterns
Closely spaced dashes can become difficult to distinguish at small sizes or reduced display resolutions.
Maintain Sufficient Contrast
Ensure dashed lines remain visible against their backgrounds and adjacent graphics.
Provide Accessible SVG Names
Informative SVG graphics should have accessible names or equivalent text descriptions.
<svg viewBox="0 0 200 60" role="img" aria-label="Blue dashed horizontal line">
<path d="M10 30 H190" fill="none" stroke="blue" stroke-width="4" stroke-dasharray="10 5"/>
</svg>
Respect Reduced Motion
Moving dash patterns can be distracting. Provide a static alternative for users who prefer reduced motion.
Test Zoom and Scaling
Verify that dash patterns remain recognizable when SVG graphics are resized or zoomed.
Avoid Pattern-Only Meaning
When dashed lines represent important categories or states, provide labels or other supporting information.
Common Mistakes
Using Negative Values
svg path {
stroke-dasharray: 10 -5;
}
Negative dash or gap lengths are invalid and cause the declaration to be ignored.
Forgetting to Define a Stroke
The initial stroke value is none. A dash pattern is not visible unless the SVG element has a painted stroke.
Using Border-Style Keywords
svg path {
stroke-dasharray: dashed;
}
The stroke-dasharray property does not accept CSS border-style keywords. Use numeric dash and gap lengths.
Confusing Dash Lengths With Stroke Width
The stroke-dasharray property controls lengths along the path. The stroke-width property controls thickness across the path.
Assuming Percentages Use Path Length
Percentages are relative to the normalized diagonal of the SVG viewport, not directly to the path length.
Expecting One Value to Create Continuous Dashes
A single value is repeated to create alternating dashes and gaps. For example, 10 behaves like 10 10.
Using Zero-Length Dashes Without Round Caps
Zero-length dashes generally require stroke-linecap: round to produce visible dots.
Ignoring Stroke Line Caps
Round and square line caps extend beyond the nominal dash endpoints, changing the visible dash lengths and gaps.
Forgetting stroke-dashoffset
When a pattern needs to begin at a different position, use stroke-dashoffset rather than changing every dash length.
Using All-Zero Patterns
svg path {
stroke-dasharray: 0 0;
}
A dash list containing only zeros is treated as a continuous stroke rather than a useful dashed pattern.
Important Considerations
Keep the following points in mind when using stroke-dasharray:
- It defines repeating dash and gap lengths along an SVG stroke.
- The initial value is
none. - The property is inherited.
- It accepts non-negative numbers, lengths, and percentages.
- Values can be separated by spaces or commas.
- The first value represents a painted segment.
- The second value represents an unpainted gap.
- Additional values continue alternating between dashes and gaps.
- Odd-length lists are repeated to create even-length patterns.
- A single value creates equal dash and gap lengths.
- Negative values invalidate the declaration.
- All-zero dash lists produce a continuous stroke.
- Percentages are based on the normalized diagonal of the SVG viewport.
- Unitless numbers use the current SVG user coordinate system.
- The dash pattern follows the geometry of the SVG path.
- A visible dash pattern requires a painted stroke.
- The
stroke-widthproperty controls stroke thickness. - The
stroke-linecapproperty affects visible dash endpoints. - Round line caps can create dotted outlines.
- The
stroke-dashoffsetproperty shifts the pattern position. - Dash patterns can be animated.
- Different patterns can improve the readability of charts and diagrams.
- Animated patterns should respect reduced-motion preferences.
- Test dash appearance across supported browsers and display sizes.
Browser Support
The CSS stroke-dasharray property is widely supported for SVG graphics in modern browsers, including Chrome, Edge, Firefox, and Safari.
Basic dash patterns using unitless numbers and lengths are broadly supported. More advanced combinations involving percentages, calculations, or animations should be tested when compatibility with older browsers is required.
For current compatibility information, visit Can I Use? and search for stroke-dasharray.
Feature Detection
@supports (stroke-dasharray: 10 5) {
.dashed-outline {
stroke-dasharray: 10 5;
}
}
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 none when no other value applies.
Testing Recommendations
Test dash patterns, rounded line caps, percentage values, and animations in the browsers you support.
Specifications
The stroke-dasharray property is defined in the SVG 2 specification.
SVG 2 - stroke-dasharray Property
The specification defines how dash and gap lengths are interpreted and repeated along SVG paths.
For additional documentation and examples, consult MDN Web Docs.
