I've got about 300 path
elements in an SVG, all with different outlines. I am trying to work out if there's a way to make them inherit their stroke
and stroke-width
properties from one central place, so that I can change that stroke in one place (using JavaScript) and all the paths will automatically inherit the change (without having to iterate over them and change them one-by-one, which might be slow).
I tried doing this:
<svg>
<defs>
<path id="base-path" stroke="#f00" stroke-width="2px"></path>
</defs>
<g>
<use xlink:href="#base-path" d="M655.4122777593284,559.3735699875456L654.1949190078633,555.3887773907059L658.1760111410327,552.9949065234823L662.5848239166088,564.7584631711534L654.5239348866377,566.2861923041205L652.878855492766,562.0062446004627L653.569788838192,559.4960802535879Z"/>
<use xlink:href="#base-path" d="M668.9348303769535,569.1558517040312L664.0653953710934,566.6527113415123L663.3415604377898,569.6439835699075L661.4990715166535,568.1793081850988L663.7034779044416,561.2718180255733L665.5459668255778,563.7190849448948L668.5729129103017,563.229819290997Z"/>
<!-- etc... -->
</g>
</svg>
I expected all the use
elements to be rendered like path
elements, each with their own unique outline (they all have unique d
attributes), but all with the same stroke
and stroke-width
as the #base-path
.
But this didn't work. The use
elements don't appear at all. When I inspect them in Chrome dev tools, they seem to be technically 'rendered', but with zero width and height.
Am I misunderstanding how use
works? Or is it just not possible to do what I'm trying to do?