Reusing SVG elements in HTML without copy-pasting it
Snippet for reusing SVG elements in HTML without copy-pasting it. Example made in React.js
Sometimes you might encounter the case where you want to use the same SVGs multiple times across one page. The first example that comes to my mind is the use of social media icons both in the navbar and footer. This is how I would approach this
Of course, you can just copy-paste it, but it will make the size of your HTML document bigger and delay the FCP. You could also export it to a file, and load it through the image
element, but it would cause flicks, which you probably don't want to have, especially when the element is above the fold.
Published on November 23, 2021 • 1 min read