Add an SVG as an Embedded Background Image

InstructorDamon Bauer

Share this video with your friends

Send Tweet

Learn how to set an elements background image to embedded SVG. This method has an added benefit of not incurring any HTTP requests, as the SVG is simply embedded into the CSS. They can also be resized by changing the width and height of the element containing background image, so it’s easy to create multiple sizes of the same icon using this approach.

If you’re looking for the ability to display icons at different sizes and want to modify the SVG interaction styles (such as on :hover), this is could be a good option. One negative to adding the interaction styles is that it requires you to duplicate selectors and the SVG markup; the more icons you have, the larger your stylesheets will become.

Cyrille Beraud
~ 7 years ago

To automate svg escape conversion, with sass, you can use this function: https://codepen.io/jakob-e/pen/doMoML