Using the HTML `<svg>` Element to Embed Scalable Vector Graphics
Learn how to embed Scalable Vector Graphics (SVGs) into your HTML pages using the `<svg>` element. This tutorial explains the advantages of SVGs (scalability, crisp rendering), provides examples of embedding SVGs, and demonstrates how to create and style vector graphics directly within your HTML.
Using the HTML `<svg>` Element to Embed Scalable Vector Graphics
What is SVG?
SVG (Scalable Vector Graphics) is a language for describing two-dimensional vector graphics. Unlike raster images (like JPEGs or PNGs), which are made up of pixels, SVG images are based on vectors—mathematical descriptions of shapes. This means that SVG images don't lose quality when scaled or resized, making them ideal for creating graphics that need to be displayed on screens of various sizes. They are often used for logos, icons, illustrations, and other graphics that need to be crisp and clear at any size.
Embedding SVG with the `<svg>` Element
To embed an SVG graphic into your HTML page, you use the <svg>
element. The <svg>
element itself is a container; you use SVG code within it to draw the actual graphics. This example creates a simple circle.
Example: Simple SVG Circle
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
SVG provides various commands for drawing shapes, adding text, and working with images. For a comprehensive guide to SVG, refer to the W3Schools SVG tutorial.
Browser Support for SVG
SVG is widely supported by modern browsers.
Browser | Version |
---|---|
Chrome | 4.0 |
Edge | 9.0 |
Firefox | 3.0 |
Opera | 3.2 |
Safari | 10.1 |