HTML SVG-Basics - GeeksforGeeks

10 Dec, 2021
In this article, we will know HTML SVG Basics, & their implementation through the examples. SVG stands for Scalable Vector Graphics. It basically defines vector-based graphics in XML format. SVG graphics do NOT lose any quality if they are zoomed or resized. Every element and every attribute in SVG files can be animated.
Advantages of SVG: Advantages of using SVG over other image formats (like JPEG and GIF) are: 
SVG images can be created and edited with any text editor.
SVG images can be searched, indexed, scripted, and compressed.
SVG images are scalable.
SVG images can be printed with high quality at any resolution.
Differences between HTML SVG and HTML Canvas:
SVG is a language for describing 2D graphics in XML whereas Canvas draws 2D graphics, on the fly with JavaScript.
If attributes of an SVG object are changed, the browser can automatically re-render the shape whereas Canvas is rendered pixel by pixel. In canvas, once the graphic is drawn, it is forgotten by the browser.
SVG is resolution independent whereas CANVAS is resolution-dependent.
SVG supports event handlers whereas CANVAS doesn’t have support for event handlers.
Example 1: In this example, We create an SVG line in HTML.
HTML
<!DOCTYPE html><html> <body>  <h2>Welcome To GeeksforGeeks</h2>  <svg height="250" width="600">    <line x1="10" y1="10" x2="400" y2="400"          style="stroke:rgb(0,0,255);stroke-width:3" />  </svg></body> </html>
Output:
SVG line Drawing
Example 2: Drawing A SVG Circle in HTML
HTML
<!DOCTYPE html><html> <body>  <!-- html svg tag is used here -->  <svg width="200" height="200">    <circle cx="80" cy="80" r="50" stroke="black"            stroke-width="2" fill="grey" />  </svg></body> </html>
Output:
SVG Circle Drawing
Example 3: Drawing A SVG Rectangle in HTML
HTML
<DOCTYPE html>  <html>   <body>    <!-- html svg tag is used here -->    <svg width="400" height="100">      <rect width="400" height="100"            style="fill: rgb(0, 0, 255);                   stroke-width: 10; stroke: rgb(0, 0, 0)" />    </svg>  </body>   </html>
Output:
SVG Rectangle Drawing
Example 4: Drawing A SVG Rounded Rectangle in HTML
HTML
<!DOCTYPE html><html> <body>  <!-- html svg tag is used here -->  <svg width="400" height="380">    <rect x="80" y="20" rx="20"          ry="20" width="150"          height="150"      style="fill: orange;             stroke: black;             stroke-width: 2;             opacity: 0.5" />  </svg></body> </html>
Output:
SVG Rounded Rectangle Drawing
Example 5: Drawing A SVG Star in HTML 
HTML
<!DOCTYPE html><html> <body>  <!-- html svg tag is used here -->  <svg width="300" height="200">    <polygon points="100,10 40,198 190,78 10,78 160,198"      style="fill: grey; stroke: orange;             stroke-width: 5; fill-rule: evenodd" />  </svg></body> </html>
Output:
 SVG Star Drawing
Example 6: Drawing A Logo in HTML using SVG
HTML
<!DOCTYPE html><html> <body>     <!-- html svg tag is used here -->  <svg height="300" width="700">    <defs>      <linearGradient id="grad1" x1="0%"                      y1="0%" x2="100%"                      y2="0%">        <stop offset="0%"              style="stop-color: white; stop-opacity: 1" />        <stop offset="100%"              style="stop-color: green; stop-opacity: 1" />      </linearGradient>    </defs>    <ellipse cx="200" cy="100"             rx="120" ry="80"             fill="url(#grad1)" />    <text fill="#ffffff" font-size="22"          font-family="ARIAL"          x="120" y="110">      GeeksforGeeks    </text>  </svg></body> </html>
Output:
GFG Logo using SVG
Supported Browsers: 
Google Chrome 4.0
Internet Explorer 9.0
Firefox 3.0
Opera 10.1
Safari 3.2
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
nidhi_biet
shubhamyadav4
bhaskargeeksforgeeks
HTML and XML
HTML-Basics
Web technologies-HTML and XML
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to insert spaces/tabs in text using HTML/CSS?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to update Node.js and NPM to next version ?
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?