How to Add Scalable Vector Graphics to Your Web Page – SitePoint

In this tutorial, we discuss ways you can add SVG images to your web page. Unlike bitmap images, SVGs are XML markup which describe shapes, lines, and fills so the document can be used in a variety of ways.
In this series, we’ve discussed what SVGs are, why you should consider them and basic vector drawings.
At some point, you’ll want to embed your finely crafted SVG directly into a web page. There are at least six ways to achieve that goal — but all methods have pros and cons.
1. Inline SVG XML Directly Into Your HTML Page
An SVG image can be added as a code island directly within your HTML page using outer tags: Embedded SVG

Embedded SVG


This method works in all modern browsers. The SVG becomes part of the page DOM so it can be manipulated with CSS or JavaScript perhaps to add animation or react to click events. (Note that any JavaScript embedded in the SVG itself will be blocked.)
The main disadvantage is that the SVG must be embedded into every page which requires it, and may need to be repeated for reusable icons. This adds to the page weight and, although the HTML may be cached, the SVG code can’t be (easily) reused elsewhere.
One solution to the repeated image issue is to create a hidden SVG block on each page (with CSS display: none). This can optionally contain multiple images referenced using an id:
box circle

Individual items can then be used any number of times with an SVG use element:

The original image can still be styled using CSS, although it’s not possible to apply additional styles to the itself.
2. Use an Tag
SVGs can be added to your web page like any other image:

The usual width, height, alt and other attributes can be added should you require them.
The browser treats the SVG like any other image. For security reasons, any scripts, external stylesheets, links, and other SVG interactivity will be disabled.
A target anchor can be used if multiple images are defined within a single SVG — such as myimage.svg#circle — but this won’t work in older browsers.
3. Apply a CSS Background Image
SVGs can be used as a CSS background for any element:
#myelement {
background-image: url(‘./image.svg’);

Inline data URIs with UTF8 encoding may also be practical for smaller, regularly used SVGs which are unlikely to change often (and invalidate the whole stylesheet):
.myelement {
background: url(‘data:image/svg+xml;utf8,‘);

Like tags, scripts, links, and other types of interactivity are disabled when you use this method.
4. Load in an

This may be a practical if you have a portable SVG with its own scripts and styles that you need to isolate from the main page. However, manipulating an SVG image from your main page’s JavaScript will become more difficult, especially if it’s hosted on another domain.
5. Use an Tag
The HTML tag can be used to add an SVG to your page:

Fallback text or images can be used within the block in a similar way to iframes.
Styles and scripts within the SVG will run. It’s not possible to style the SVG using CSS in the HTML page, but scripts can manipulate individual elements:

const objTag = document.querySelector(‘object’);

objTag.addEventListener(‘load’, () => {

const svg = objTag.getSVGDocument();

.forEach(p => p.setAttribute(‘fill’, ‘#00f’))


was the most-used option around a decade ago, because it was the only reliable way to use an SVG in old releases of IE.
6. Use anTag
I’m includingfor completeness but please don’t use it! It’s similar to in that it embeds external content into the page, but was designed to communicate with an external application such as a browser plugin.not supportedSince the demise of Flash and Silverlight, modern browsers have deprecated and removed support for browser plugins. Never rely on.
Which SVG Technique Should You Use?
If you’re using an SVG as an image and don’t need to change the styling or add scripting, an tag or a CSS background is possibly the best option. The file can be cached in the browser and reused on other pages with no further download.
Where you require interactivity, the most popular option is to inline the SVG directly into the HTML. Styles and scripts can manipulate the DOM, links can be added to any shape, and sophisticated SVG filters can be applied to other page elements.
The tag has fallen from favor. However, it remains a viable technique if you want to keep SVGs separate and cacheable, but they require a little JavaScript manipulation.
Iframes may be suitable for some projects, but never useunless you have the displeasure of coding for an ancient browser.

Coded at

Share your love

Leave a Reply