To show how dashes can be offset, we first set up five identical paths, all of which are given a dash array of a 20-unit dash followed by a 3-unit gap via the SVG attribute stroke-dasharray
. (This could also have been done with the CSS property stroke-dasharray
.) The paths are then given individual dash offsets via CSS.
<svg viewBox="0 0 100 50" width="500" height="250">
<rect x="10" y="5" width="80" height="30" fill="#EEE" />
<g stroke="dodgerblue" stroke-width="2" stroke-dasharray="20,3">
<path d="M 10,10 h 80" />
<path d="M 10,15 h 80" />
<path d="M 10,20 h 80" />
<path d="M 10,25 h 80" />
<path d="M 10,30 h 80" />
</g>
</svg>
path:nth-of-type(1) {
stroke-dashoffset: 0;
}
path:nth-of-type(2) {
stroke-dashoffset: -5;
}
path:nth-of-type(3) {
stroke-dashoffset: 5;
}
path:nth-of-type(4) {
stroke-dashoffset: 5px;
}
path:nth-of-type(5) {
stroke-dashoffset: 5%;
}
In order:
- The first of the five paths is given a zero offset, which is the default behavior.
- The second path is given an offset of
-5
, which shifts the start point of the array to five units before the zero-point. The visual effect is that the dash pattern is pushed forward five units; thus, we see, at the start of the path, the last two units of a dash and then a three-unit gap. - The third path has an offset of
5
, which means the starting point of the dashes is five units into the dash pattern. The visual effect is to push the dash pattern backward by five units; thus, we see, at the start of the path, the last fifteen units of a dash followed by a three-unit gap. - The fourth path has an offset of
5px
, which has the same effect as a value of 5
(see previous). - The fifth and last path has an offset of
5%
, which is very similar to the previous two examples, but is not quite the same. Percentages are calculated against the diagonal measure of the SVG viewport, and so can very depending on that viewport's size and aspect ratio.