logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

HTML SVG Graphics


SVG (Scalable Vector Graphics) is used to define vector-based graphics for the Web. Unlike standard images (like .jpg or .png), SVG graphics are defined in XML format and do not lose any quality when zoomed or resized.


What is SVG?

  • Scalable:Graphics maintain perfect quality at any size.

  • XML-Based:Every element is available within the SVG DOM, allowing for CSS styling and JavaScript interaction.

  • Interactive:Every attribute and element in an SVG file can be animated.

  • LearnHTML StandardIt is a recommended web standard that integrates with HTML, CSS, and JavaScript.


The <svg> Element

The HTML <svg> element acts as a container for drawing paths, shapes, and text. Below are common examples of how to use it.


Drawing a Circle

The cx and cy attributes define the center, and r defines the radius.

<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow">
</svg>

Drawing a Rectangle

Use rx and ry to create rounded corners, and opacity to control transparency.

<svg width="400" height="180">
<rect x="50" y="20" rx="20" ry="20" width="100%" height="100%"
style="fill:red;stroke:black;stroke-width:5;">
</svg>

Creating a Polygon (Star Shape)

The points attribute defines the coordinates for each corner of the shape.



<svg width="300" height="200">
<polygon points="100,10 40,198 190,78 10,78 160,198"
style="fill:yellow;stroke:green;stroke-width:5;fill-rule:evenodd;" /> </svg>

Gradients and Text

You can define complex gradients within the <defs> tag and apply them to shapes.

LeranHTML

<svg height="130" width="500">
<defs>
<linearGradient id="grad1">
<stop offset="0%" stop-color="yellow" />
<stop offset="100%" stop-color="red" />
</linearGradient>
</defs>
<ellipse cx="100" cy="70" rx="85" ry="55" fill="url(#grad1)" />
<text fill="#ffffff" font-size="24" font-family="Verdana" x="30" y="75"> LeranHTML <LeranHTML>
</svg>

SVG vs. Canvas: Which one should you use?

The primary difference is that SVG is XML-based (object-oriented), while Canvas is pixel-based (raster).

Image comparing vector graphics vs raster graphics

Feature SVG SVG
Resolution Independent (Scalable) Dependent (Fixed)
Event Handlers Supported (can click shapes) Not supported
Text Rendering Excellent Poor
Performance Slower with many objects Faster for complex animations
Best Use Case Icons, UI elements, Diagrams High-performance games, Photo editing

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis