For working professionals
For fresh graduates
More
2. HTML Basics
3. HTML Syntax
9. HTML Head
10. HTML Title
11. HTML Styles
12. HTML Paragraphs
13. HTML Symbols
14. HTML Emojis
15. HTML Formatting
16. HTML Entities
17. HTML Audio
18. HTML Images
19. HTML Lists
20. HTML Links
21. SVG in HTML
22. HTML Forms
23. HTML Video
24. HTML Canvas
25. Adjacency Lists
26. HTML Input Types
27. HTML Tables
31. HTML Layout
33. HTML Div
37. HTML Iframes
40. HTML Code
41. HTML Colors
42. HTML CSS
43. HTML Editors
44. HTML Examples
45. Class in HTML
46. HTML Exercises
47. HTML ID
49. HTML Table Style
50. HTML Script
SVG in HTML stands for Scalable Vector Graphics. It is used to make scalable images and elements. SVG elements are not rasterized like JPG, PNG, and JPEG. This makes SVG elements very convenient and scalable as the resolution does not change if the size of the image is increased. They are usually used to include logos of a company on a website.
Being a web developer myself, I use SVG in HTML for my projects all the time to make my web pages more interactive and interesting.
The HTML SVG tag can be used mainly in two ways. In this section of the tutorial, let me discuss the two ways in which SVG for HTML is used.
We use the <svg> element to embed SVG markup straight into your HTML document. Inline SVG allows you to define and control SVG graphics right within your HTML code. This is used when we want to create some animation directly in our code.
Let me explain with the help of an example.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Inline SVG Example</title>
<style>
.sg1{
background-color: lightblue;
}
</style>
</head>
<body>
<div class="sg1">
<svg width="100" height="100">
<circle cx="60" cy="60" r="40" fill="red" />
</svg>
</div>
</body>
</html>
In the above example,
This includes linking to an external SVG file from your HTML document using the <img> tag. The external HTML image SVG file contains the SVG markup, which can be reused in numerous HTML files.
Let me explain with the help of an example.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>External SVG Example</title>
</head>
<body>
<img src="example.svg" alt="External SVG Example">
</body>
</html>
In the above example,
For this example to work, make sure the HTML and SVG files are in the same directory.
Let us look at some examples of SVG HTML code to clear the concept of SVG in HTML better. Here I have discussed some examples that are sure to help you with this concept.
Let me show you how to create a rectangle in HTML using an example.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG Rectangle Example</title>
</head>
<body>
<h1>SVG Rectangle Example in HTML</h1>
<!-- Inline SVG Rectangle -->
<svg width="3000" height="3000">
<rect x="20" y="20" width="250" height="300" fill="green" />
</svg>
</body>
</html>
In the above example,
Now let me share with you how you can create a polygon in HTML using SVG.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG Polygon Example</title>
</head>
<body>
<h1> SVG Polygon Example in HTML </h1>
<!-- Inline SVG Polygon -->
<svg height="210" width="500">
<polygon points="100,10 150,50 150,150 50,150 50,50"
style="fill:blue;
stroke:red;
stroke-width:5;
fill-rule:nonzero;" />
</svg>
</body>
</html>
In the above example,
There are numerous advantages to using SVG in HTML. Here, let me discuss some of them with you.
It is very easy to get confused between SVG in HTML and the canvas element in HTML. In this section of the tutorial, I will discuss the differences between HTML5 canvas SVG and HTML5 SVG element.
Point of difference | SVG | Canvas |
Type of graphics | SVG is a vector graphics format that defines shapes and routes using mathematical computations. This allows SVG graphics to be a type of scalable vector graphics in HTML5. | <canvas> is bitmap-based, allowing you to draw pixels directly on a bitmap canvas with JavaScript. This outputs raster graphics and scaling can cause pixelation. |
DOM manipulation and accessibility | SVG graphics are defined using XML-based markup, therefore they are part of the HTML DOM. This allows for styling using CSS and manipulation with JavaScript. | Unlike SVG in HTML, <canvas> graphics are not part of the DOM and cannot be styled with CSS or accessed by screen readers. Everything is drawn programmatically using JavaScript. |
Use cases | SVG for HTML can be used for static images, icons, illustrations, and interactive components such as buttons, animations, and data visualizations. | <canvas> is suitable for dynamic and interactive graphics, including games, simulations, real-time visualizations, and complicated animations. |
SVG in HTML added a lot of conveniences and solved a lot of problems. It made images more scalable without losing out on resolution while being relatively small in size. They added a lot of SEO benefits as well. Now feel free to add SVG elements in your future HTML projects to make them more dynamic and eye catchy.
If you want to learn more about web development and HTML, I would suggest doing a course on web development from a reputed platform. One such platform that comes to mind is upGrad. Their courses are in collaboration with some of the best universities around the world. Some of the best teachers have curated their courses thus making its courses one of the best platforms on the market.
SVG in HTML stands for Scalable Vector Graphics, and it is a method of creating graphics and visual elements directly in HTML using XML-based syntax. Essentially, SVG allows you to use code to draw shapes, lines, text, and other objects that are then scaled without losing quality.
SVG in HTML is used to create scalable images, icons, data visualizations, animations, and responsive HTML designs. It is versatile, scalable, and ideal for generating dynamic web elements.
To add SVG in HTML, use the tag with the src attribute pointing to an SVG file, or inline SVG code using the <svg> element.
SVG in HTML is better for static or scalable visuals that need to be styled and edited, but <canvas> is better for dynamic and interactive graphics with complex rendering and animations.
SVG in HTML stands for Scalable Vector Graphics. It allows you to produce graphics and visual elements directly in HTML using XML-based markup. For instance, here's an easy SVG code that creates a red circle:
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="red" />
</svg>
This creates a red circle.
SVG files are compatible with all modern web browsers, including Chrome, Firefox, Safari, Edge, and Opera.
SVGs can be animated with CSS, JavaScript, or SVG-specific animation elements like <animate> and <animateTransform>.
No, SVG in HTML does not need an ID attribute. However, adding IDs to SVG elements can let you target them with CSS or JavaScript for styling, animation, and interactivity. Using IDs, you can apply custom styles or properties to individual SVG elements in your HTML content.
Author
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.