How to import a SVG file in JavaScript?


Scalable Vector Graphic, sometimes known as SVG, is a type of 2D graphic or image file. In order to create visuals, SVG files employ mathematical formulas and a set of guidelines about shapes, lines, and other features. SVGs are simply XML code that specifies how colours should be presented, where each form should appear in respect to other shapes inside a file, and how shapes should be displayed.

SVGs and some other vector graphics were significantly different than raster graphics that depend on pixels to convey visual data, like jpegs or png files.

The four advantages of using SVG files in web design are as follows −

Clarity

SVG files can be scaled indefinitely. SVG files have a significant advantage over raster images in that you may enlarge them and resize them as many times as you like without losing clarity. Raster images can lose clarity if they are not scaled correctly and can even start to look gritty.

Flexibility

It is reasonably easy to create responsive SVG files that appear nice on any device, even when the viewer zooms in on a web page. SVG files can be resized repeatedly throughout the editing stage without losing clarity. SVG files are an excellent option for logos and straightforward infographics because of their adaptability. SVG files can also be used for animations, and they are especially beneficial for developing fonts with different colour schemes and gradients.

Files that are smaller in size

Depending on the complexity of the visual or the number of pathways within a design, SVG file sizes could be considerably fewer than those of a PNG or JPG of the exact same image. SVG files can be between 60 and 80 percent smaller than PNGs, according to Vecta.io, which could also reduce load times and improve user experience (UX). Additionally beneficial to website SEO are faster page loads.

Easier access and inclusion

Whenever it comes to accessibility and diversity, SVG files have a number of advantages. Designers have the ability to include structural data defining the visual elements of a graphic within the SVG file itself that can help users of specific assistive technologies in understanding what is included within an image. As an option, raster files only use metadata (i.e., alt text) to inform screen readers and other assistive technologies about the contents of a graphic.

In this article, we'll explore and use many SVG usage scenarios (Scalable Vector Graphics).

First Method

The fastest method is to use the HTML <img> element.

Syntax

<img src='logo.svg' alt="some file" height='100' width='100' style="color:orange;"/>

You require the following to embed an SVG in the <image> element −

  • The src attribute

  • When your SVG doesn't have an intrinsic aspect ratio, use the height attribute.

  • When your SVG doesn't inherently have an aspect ratio, use the width attribute.

Advantages

  • Deployment is simple and quick.

  • Nesting the HTML elements <img> and <a> will turn the SVG image together into hyperlink.

  • SVG file caching, resulting in faster loading.

Disadvantages

  • SVG files are not susceptible to manipulation.

  • Just inline CSS can be used to style SVG.

  • CSS pseudo-classes cannot be used to style the SVG.

Example1

HTML Source code

<!DOCTYPE html>
<html>
   <title>How to import a SVG file in JavaScript article - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body style="text-align:center">
   <body>
      <img src="https://www.tutorialspoint.com/images/logo.png" alt="tutorialspoint-logo" height="90" width="310" style="background-color: transparent" />
   </body>
</body>
</html>

Second Method

In the following method let us understand using SVG as an <object>

Syntax

<object type="image/svg+xml" data="logo.svg" class="logo">
   Logo
</object>

The following is needed in order to embed an SVG using a "object" element −

  • The type attribute
  • The data attribute
  • The class attribute (when internal/external CSS is being used)

Advantages

  • SVG can be styled with external/internal CSS.
  • Coding is simple and quick.
  • Should perform excellently with caching.

Disadvantages

  • You should use <style> element in the SVG file if you want to use an external stylesheet.
  • Lack of familiarity with execution and syntax.

Example 2

HTML Source code

<!DOCTYPE html>
<html>
<title>How to import a SVG file in JavaScript article - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" type="css" href="styles.css" />
</head>
<body style="text-align:center">
   <object type="image/svg+xml" data="/logo.svg" class="logo">
Tutorialspoint Logo
</object>
</body>
</html>

CSS Source code

logo {
   height: 90;
   width: 310;
}

Output

The above code will give the below output −


Third Method

In the following method let us understand using an <iframe> to embed an SVG.

Syntax

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

The <iframe> element is needed to embed an SVG.

  • The src attribute.

  • When your SVG doesn't have an intrinsic aspect ratio, use the height attribute.

  • When your SVG doesn't natively have an aspect ratio, use the width attribute.

Advantages

  • The Implementation is quick and simple.

  • It is identical to the <object> element.

Disadvantages

  • JavaScript cannot be used to modify the SVG.

  • It's not ideal to cache.

Example 3

HTML Source code

<!DOCTYPE html>
<html>
<title>How to import a SVG file in JavaScript article - TutorialsPoint</title>
<head>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="style.css">
</head>
<body style="text-align:center">
   <iframe src="/logo.svg" width="200" height="120"></iframe>
</body>
</html>

Updated on: 10-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements