How to set a stroke-width:1 on only certain sides of SVG shapes?
stroke-width
can be selectively applied to SVG sides by creating individual <path>
elements for each side that needs to be targeted. Here’s an example with separate paths for distinct stroke control:
Manipulate stroke-dasharray
When stroke-width:1
needs to be set on one side of a shape, one could use the stroke-dasharray
property. It allows to craft the precise illusion of varying stroke widths:
Here we set stroke-dasharray
to create a dash length equal to the side we want stroked, and a gap twice as long to avoid coloring the next side of the shape.
Masking and clip-paths for better control
Even though SVG vector effects have limitations, using masks and clip-paths can give us precise control over the strokes of our shapes:
The clip-path
is used here to allow the stroke to be visible only on the top side.
Tackle limitations with multiple shapes
For more complex stroke-width
patterns, creating multiple SVG shapes is the way to go:
Make use of polyline
The <polyline>
element is handy at controlling specific sides and not creating a full border around the shape:
Apply classes for reusability
Creating classes with stroke-dasharray
values can make your code reusable and maintainable:
Was this article helpful?