Connect with us


Add Scalable Vector Graphics (SVG) to Your Internet Web page

On this tutorial, we focus on methods you’ll be able to add SVG pictures to your net web page. In contrast to bitmap pictures, SVGs are XML markup which describe shapes, strains, and fills so the doc can be utilized in a wide range of methods.

On this collection, we’ve mentioned what SVGs are, why you need to contemplate them and fundamental vector drawings.

In some unspecified time in the future, you’ll need to embed your finely crafted SVG straight into an online web page. There are not less than six methods to attain that purpose — however all strategies have execs and cons.

1. Inline SVG XML Instantly Into Your HTML Web page

An SVG picture may be added as a code island straight inside your HTML web page utilizing outer <svg> tags:

<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8" />
  <title>Embedded SVG</title>

  <h1>Embedded SVG</h1>

  <svg width="300px" peak="300px"
    <textual content x="10" y="50" font-size="30">My SVG</textual content>


This technique works in all trendy browsers. The SVG turns into a part of the web page DOM so it may be manipulated with CSS or JavaScript maybe so as to add animation or react to click on occasions. (Observe that any JavaScript embedded within the SVG itself might be blocked.)

The principle drawback is that the SVG should be embedded into each web page which requires it, and will have to be repeated for reusable icons. This provides to the web page weight and, though the HTML could also be cached, the SVG code can’t be (simply) reused elsewhere.

One resolution to the repeated picture concern is to create a hidden SVG block on every web page (with CSS show: none). This may optionally include a number of pictures referenced utilizing an id:

<svg xmlns="" model="show: none;">
    <image id="field" viewBox="0 0 32 32">
      <rect width="32" peak="32" fill="#00c" />
    <image id="circle" viewBox="0 0 32 32">
      <circle cx="16" cy="16" r="16" fill="#c00" />

Particular person objects can then be used any variety of instances with an SVG use aspect:

<svg width="20" peak="20">
  <use xlink:href="#field" />

<svg width="30" peak="30">
  <use xlink:href="#field" />

<svg width="40" peak="40">
  <use xlink:href="#circle" />

The unique picture can nonetheless be styled utilizing CSS, though it’s not potential to use extra kinds to the <use> itself.

2. Use an <img> Tag

SVGs may be added to your net web page like some other picture:

<img src="picture.svg" alt="my picture" />

The same old width, peak, alt and different <img> attributes may be added do you have to require them.

The browser treats the SVG like some other picture. For safety causes, any scripts, exterior stylesheets, hyperlinks, and different SVG interactivity might be disabled.

A goal anchor can be utilized if a number of pictures are outlined inside a single SVG — akin to myimage.svg#circle — however this gained’t work in older browsers.

3. Apply a CSS Background Picture

SVGs can be utilized as a CSS background for any aspect:

#myelement {
  background-image: url('./picture.svg');

Inline information URIs with UTF8 encoding can also be sensible for smaller, often used SVGs that are unlikely to alter usually (and invalidate the entire stylesheet):

.myelement {
  background: url('information:picture/svg+xml;utf8,<svg xmlns="" viewBox="0 0 300 300"><circle cx="150" cy="150" r="100" stroke-width="5" stroke="#f00" fill="#ff0" /></svg>');

Like <img> tags, scripts, hyperlinks, and different kinds of interactivity are disabled once you use this technique.

4. Load in an <iframe>

Browsers can render an SVG doc by itself, so you’ll be able to load a picture inside an iframe:

<iframe src="./picture.svg">
  Your browser doesn't help iframes.

This can be a sensible in case you have a conveyable SVG with its personal scripts and kinds that you must isolate from the primary web page. Nonetheless, manipulating an SVG picture out of your predominant web page’s JavaScript will develop into tougher, particularly if it’s hosted on one other area.

5. Use an <object> Tag

The HTML <object> tag can be utilized so as to add an SVG to your web page:

<object kind="picture/svg+xml" information="./picture.svg">
  <img src="./fallback-bitmap.png" />

Fallback textual content or pictures can be utilized inside the <object> block in the same approach to iframes.

Types and scripts inside the SVG will run. It’s not potential to model the SVG utilizing CSS within the HTML web page, however scripts can manipulate particular person components:

const objTag = doc.querySelector('object');

objTag.addEventListener('load', () => {

  const svg = objTag.getSVGDocument();

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


<object> was the most-used possibility round a decade in the past, as a result of it was the one dependable means to make use of an SVG in previous releases of IE.

6. Use an <embed> Tag

I’m together with <embed> for completeness however please don’t use it! It’s just like <object> in that it embeds exterior content material into the web page, however was designed to speak with an exterior software akin to a browser plugin.

<embed kind="picture/svg+xml" src="./picture.svg">
  not supported

Because the demise of Flash and Silverlight, trendy browsers have deprecated and eliminated help for browser plugins. By no means depend on <embed>.

Which SVG Approach Ought to You Use?

When you’re utilizing an SVG as a picture and don’t want to alter the styling or add scripting, an <img> tag or a CSS background is probably the best choice. The file may be cached within the browser and reused on different pages with no additional obtain.

The place you require interactivity, the most well-liked possibility is to inline the SVG straight into the HTML. Types and scripts can manipulate the DOM, hyperlinks may be added to any form, and complicated SVG filters may be utilized to different web page components.

The <object> tag has fallen from favor. Nonetheless, it stays a viable approach if you wish to maintain SVGs separate and cacheable, however they require a bit of JavaScript manipulation.

Iframes could also be appropriate for some tasks, however by no means use <embed> until you might have the displeasure of coding for an historic browser.

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *