How to import a SVG file in JavaScript?

Scalable Vector Graphic (SVG) is a powerful 2D image format that uses mathematical formulas and XML markup to create scalable graphics. Unlike raster images (JPEG, PNG) that rely on pixels, SVG files maintain crisp quality at any size, making them ideal for web development.

SVG files offer significant advantages in web design including infinite scalability, smaller file sizes, better accessibility features, and the ability to be styled with CSS. This article explores different methods to import and use SVG files in JavaScript applications.

Method 1: Using HTML <img> Element

The simplest method to display SVG files is using the standard HTML <img> element.

Syntax

<img src="logo.svg" alt="Logo description" height="100" width="100" />

Example

<!DOCTYPE html>
<html>
<head>
   <title>SVG with IMG Element</title>
</head>
<body style="text-align:center">
   <h2>SVG using IMG tag</h2>
   <img src="/images/sample-logo.svg" alt="Sample Logo" height="90" width="200" />
</body>
</html>

Advantages

  • Simple and quick implementation

  • Can be wrapped in <a> tag to create hyperlinks

  • Browser caching support for faster loading

Disadvantages

  • Cannot manipulate SVG with JavaScript

  • Limited styling options with CSS

  • No access to internal SVG elements

Method 2: Using <object> Element

The <object> element provides more control over SVG files and allows external styling.

Syntax

<object type="image/svg+xml" data="logo.svg" class="svg-logo">
   Fallback text for unsupported browsers
</object>

Example

<!DOCTYPE html>
<html>
<head>
   <title>SVG with Object Element</title>
   <style>
      .svg-logo {
         height: 90px;
         width: 200px;
         border: 1px solid #ccc;
      }
   </style>
</head>
<body style="text-align:center">
   <h2>SVG using Object tag</h2>
   <object type="image/svg+xml" data="/images/sample-logo.svg" class="svg-logo">
      Your browser does not support SVG
   </object>
</body>
</html>

Advantages

  • Can be styled with external/internal CSS

  • Supports fallback content

  • Good caching performance

Disadvantages

  • Requires <style> element in SVG for external stylesheets

  • Less familiar syntax compared to <img>

Method 3: Using <iframe> Element

The <iframe> approach treats SVG as a separate document embedded within the page.

Syntax

<iframe src="logo.svg" width="200" height="120"></iframe>

Example

<!DOCTYPE html>
<html>
<head>
   <title>SVG with Iframe Element</title>
</head>
<body style="text-align:center">
   <h2>SVG using Iframe tag</h2>
   <iframe src="/images/sample-logo.svg" width="200" height="120" style="border:1px solid #ccc;"></iframe>
</body>
</html>

Advantages

  • Quick and simple implementation

  • Similar behavior to <object> element

Disadvantages

  • Cannot manipulate SVG with JavaScript

  • Poor caching performance

  • Creates additional HTTP request

Method 4: Inline SVG with JavaScript

For dynamic manipulation, you can insert SVG code directly into the DOM using JavaScript.

<!DOCTYPE html>
<html>
<head>
   <title>Dynamic SVG with JavaScript</title>
</head>
<body>
   <div id="svg-container"></div>
   
   <script>
      // Create SVG element dynamically
      const svgCode = `
         <svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">
            <circle cx="50" cy="50" r="40" fill="blue" />
            <text x="100" y="55" font-family="Arial" font-size="16">Dynamic SVG</text>
         </svg>
      `;
      
      document.getElementById('svg-container').innerHTML = svgCode;
      
      // You can now manipulate the SVG with JavaScript
      const circle = document.querySelector('circle');
      circle.addEventListener('click', function() {
         this.style.fill = this.style.fill === 'red' ? 'blue' : 'red';
      });
   </script>
</body>
</html>

Comparison of Methods

Method JavaScript Access CSS Styling Caching Best For
<img> No Limited Good Static images
<object> Limited Yes Good Styled graphics
<iframe> No No Poor Isolated content
Inline JS Full Full N/A Interactive graphics

Conclusion

Choose your SVG import method based on your needs: use <img> for simple static graphics, <object> for styled SVGs, and inline JavaScript for interactive graphics. Each method offers different levels of control and performance characteristics.

Updated on: 2026-03-15T23:19:00+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements