The Ultimate Guide to SVG
Introduction to SVG
Scalable Vector Graphics (SVG) is an XML-based markup language for describing two-dimensional vector graphics. It was developed by the W3C and became a web standard in 2001.

In this post, you will learn about SVG (Scalable Vector Graphics), its uses, pros and cons, and the basics of SVG editing. Additionally, you’ll get a SVG Editor view, the ability to save in SVG and PNG formats, and a guide on creating SVGs.
Key features:
- Scalability: SVG images can be resized without quality loss.
- Interactivity: SVG supports JavaScript and CSS for dynamic effects.
- Editability: SVG files can be modified in text editors or vector tools.
- Small File Size: SVG files are often smaller than raster images.
Why Use SVG?
✅ Resolution Independence – Perfect for responsive
design.
✅ Small File Size – Reduces page load time.
✅ Styling & Scripting – Can be styled with CSS and
manipulated with JavaScript.
✅ Animation Support – Built-in SMIL animations or
CSS/JS-based animations.
✅ Accessibility – Text inside SVG is selectable and
indexable by search engines.
SVG vs. Other Image Formats
Feature | SVG | PNG | JPEG | GIF |
---|---|---|---|---|
Scalability | ✅ | ❌ | ❌ | ❌ |
Transparency | ✅ | ✅ | ❌ | ✅ |
Animation | ✅ | ❌ | ❌ | ✅ |
File Size | Small | Medium | Large | Small-Medium |
Best For | Icons, Logos, Graphs | Photos | Photos | Simple Animations |
Basic SVG Structure
An SVG file is defined using the <svg>
element. Here's a
minimal example:
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" fill="red" />
</svg>
SVG Shapes and Elements
SVG supports various shapes:
<circle>
– Draws a circle<rect>
– Draws a rectangle<line>
– Draws a straight line-
<polygon>
– Draws a closed shape with multiple sides -
<path>
– The most powerful element for complex shapes
Example: SVG Shapes
<svg width="200" height="200">
<rect x="10" y="10" width="50" height="50" fill="blue" />
<circle cx="100" cy="50" r="30" fill="green" />
<line x1="150" y1="20" x2="180" y2="80" stroke="red" stroke-width="2" />
<polygon points="50,100 100,150 150,100" fill="yellow" />
</svg>
Styling SVG with CSS
SVG elements can be styled using:
-
Inline attributes (
fill
,stroke
,stroke-width
) - Internal/External CSS (using classes or IDs)
Example: CSS Styling
<style>
.my-circle {
fill: purple;
stroke: black;
stroke-width: 3;
}
</style>
<svg width="100" height="100">
<circle class="my-circle" cx="50" cy="50" r="40" />
</svg>
SVG Filters and Effects
SVG supports advanced effects like:
- Blur (
<feGaussianBlur>
) - Drop Shadow (
<feDropShadow>
) -
Gradients (
<linearGradient>
,<radialGradient>
)
Example: Drop Shadow
<svg width="200" height="200">
<defs>
<filter id="shadow">
<feDropShadow dx="3" dy="3" stdDeviation="2" />
</filter>
</defs>
<rect x="20" y="20" width="100" height="80" fill="orange" filter="url(#shadow)" />
</svg>
SVG Animations
SVG can be animated using:
- SMIL (Synchronized Multimedia Integration Language) – Native SVG animations
- CSS Animations – Using
@keyframes
- JavaScript – For dynamic interactions
Example: CSS Animation
<style>
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
.spinner {
animation: rotate 2s linear infinite;
transform-origin: center;
}
</style>
<svg width="100" height="100">
<circle class="spinner" cx="50" cy="50" r="40" fill="none" stroke="blue" stroke-width="5" />
</svg>
SVG in HTML
SVG can be embedded in HTML in multiple ways:
- Inline SVG – Directly in HTML
-
<img> Tag –
<img src="image.svg" alt="SVG Image">
-
CSS Background –
background-image: url('image.svg');
-
<object> Tag –
<object data="image.svg" type="image/svg+xml"></object>
Best Practice: Use Inline SVG for Interactivity
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="red" onclick="alert('Clicked!')" />
</svg>
SVG Editors
Live: Open in New Tab ↗
Several tools can help create and edit SVG files:
- Adobe Illustrator – Professional vector graphics editor
- Inkscape – Free and open-source vector editor
- Figma – Web-based design tool with SVG export
- SVGOMG – Web-based SVG optimizer
- Vectr – Free online SVG editor
- Boxy SVG – Simple browser-based SVG editor
Tip: Many code editors (like VS Code) have SVG preview extensions that let you view SVG files as you edit them.
Optimizing SVG
To optimize SVG files:
- Remove unnecessary metadata (use tools like SVGO)
- Minify SVG code (remove whitespace)
- Use <path> instead of multiple shapes where possible
Accessibility & SEO for SVG
Make SVG accessible:
-
Add
role="img"
and<title>
/<desc>
for screen readers - Use
aria-label
if SVG is interactive
<svg role="img" aria-label="Company Logo">
<title>Company Logo</title>
<desc>A red circle with a white star inside.</desc>
<circle cx="50" cy="50" r="40" fill="red" />
</svg>
Advanced SVG Techniques
For more complex SVG implementations:
- SVG Sprites – Combine multiple icons into one file
-
Clipping & Masking (
<clipPath>
,<mask>
) -
Responsive SVG (using
viewBox
andpreserveAspectRatio
) - Data Visualization – Charts and graphs with SVG
Practical SVG Examples
Example 1: SVG Logo
<svg width="120" height="120">
<rect x="10" y="10" width="100" height="100" fill="#4CAF50" rx="10" />
<text x="60" y="70" text-anchor="middle" fill="white" font-size="24">Logo</text>
</svg>
Example 2: Animated Hamburger Menu Icon
<svg width="40" height="40" viewBox="0 0 100 100" onclick="this.classList.toggle('active')">
<rect class="top" x="10" y="20" width="80" height="10" fill="#333" />
<rect class="middle" x="10" y="45" width="80" height="10" fill="#333" />
<rect class="bottom" x="10" y="70" width="80" height="10" fill="#333" />
</svg>
<style>
svg.active .top { transform: rotate(45deg) translate(20px, 20px); }
svg.active .middle { opacity: 0; }
svg.active .bottom { transform: rotate(-45deg) translate(20px, -20px); }
rect { transition: all 0.3s; transform-origin: center; }
</style>
Conclusion
SVG is a versatile format for web graphics, offering scalability, interactivity, and small file sizes. Whether you're creating icons, logos, or complex animations, SVG is a powerful tool for modern web development.
🚀 Start using SVG today to enhance your web projects!