Getting Started with SVGs: A Beginner's Guide
· 5 min read

Table of contents
Have you ever zoomed into an image on a website and watched it turn into a blurry mess of pixels? SVGs solve that. They let graphics stay crisp and perfect across every device and screen size — from a tiny favicon to a full-screen hero — without ever pixelating.
What are SVGs?
SVG stands for Scalable Vector Graphics. Instead of storing a grid of pixels like a JPEG or PNG, an SVG stores mathematical instructions for how to draw the image.
Think of it like a cake. A photograph of a cake (JPEG/PNG) is fixed — blow it up and it gets blurry. A recipe for the cake (SVG) can be scaled to any size and still come out perfect, because the browser re-draws it from the instructions every time.
Why Use SVGs?
- Scalability: Perfect appearance at any size.
- File efficiency: Often smaller than equivalent raster graphics.
- Styling flexibility: Customizable with CSS and JavaScript.
- Accessibility: Support text descriptions for screen readers.
- SEO advantages: Text content is indexable by search engines.
- Performance: No separate HTTP request when embedded inline in HTML.
How to Use SVGs in HTML
The simplest way is to drop an <svg> element straight into your markup:
<!DOCTYPE html>
<html>
<head>
<title>My First SVG</title>
</head>
<body>
<h1>Welcome to SVGs!</h1>
<svg width="200" height="200">
<circle cx="100" cy="100" r="80" fill="lightblue" />
</svg>
</body>
</html>Basic SVG Shapes
Rectangle (<rect>)
<svg width="300" height="200">
<rect x="50" y="30" width="200" height="100" fill="coral" />
</svg>Attributes: x, y (position), width, height (dimensions), fill (color).
Circle (<circle>)
<svg width="200" height="200">
<circle cx="100" cy="100" r="70" fill="lightgreen" stroke="darkgreen" stroke-width="3" />
</svg>Attributes: cx, cy (center), r (radius), stroke, stroke-width.
Line (<line>)
<svg width="300" height="100">
<line x1="20" y1="50" x2="280" y2="50" stroke="navy" stroke-width="4" />
</svg>Combined Example
<svg width="300" height="200">
<rect x="50" y="50" width="200" height="100" fill="lightcoral" stroke="darkred" stroke-width="2" />
<circle cx="150" cy="100" r="30" fill="white" />
<line x1="120" y1="100" x2="180" y2="100" stroke="darkred" stroke-width="3" />
</svg>The viewBox and Making SVGs Scalable
The viewBox attribute works like a camera viewfinder — it controls which part
of the SVG is visible and how it scales. The format is
viewBox="min-x min-y width height".
<svg width="400" height="300" viewBox="0 0 200 150">
<circle cx="100" cy="75" r="50" fill="purple" />
</svg>The same graphic scales just by changing the display dimensions:
<!-- Small version -->
<svg width="100" height="75" viewBox="0 0 200 150">
<circle cx="100" cy="75" r="50" fill="purple" />
</svg>
<!-- Large version -->
<svg width="800" height="600" viewBox="0 0 200 150">
<circle cx="100" cy="75" r="50" fill="purple" />
</svg>Styling SVGs
Using Attributes
<svg width="200" height="200">
<circle cx="100" cy="100" r="80" fill="lightblue" stroke="navy" stroke-width="5" opacity="0.8" />
</svg>Using CSS
<style>
.my-circle {
fill: lightblue;
stroke: navy;
stroke-width: 5px;
opacity: 0.8;
}
.my-circle:hover {
fill: darkblue;
transition: fill 0.3s ease;
}
</style>
<svg width="200" height="200">
<circle cx="100" cy="100" r="80" class="my-circle" />
</svg>Creating Complex Shapes with Paths
The <path> element draws custom shapes using drawing commands:
<svg width="200" height="200" viewBox="0 0 200 200">
<path d="M 100 20 L 180 160 L 20 160 Z" fill="orange" stroke="darkorange" stroke-width="3" />
</svg>Command key: M (move), L (line), Z (close path).
A heart shape using curves:
<svg width="200" height="200" viewBox="0 0 200 200">
<path d="M 100 150 C 100 150, 80 120, 80 100 C 80 80, 100 80, 100 100 C 100 80, 120 80, 120 100 C 120 120, 100 150, 100 150 Z" fill="red" />
</svg>Adding Text to SVGs
<svg width="300" height="100" viewBox="0 0 300 100">
<text x="150" y="50" text-anchor="middle" font-family="Arial, sans-serif" font-size="24" fill="darkblue">
Hello, SVG World!
</text>
</svg>Organizing with Groups
The <g> tag groups related elements so you can style or transform them
together:
<svg width="200" height="200" viewBox="0 0 200 200">
<g id="house" fill="brown" stroke="darkbrown" stroke-width="2">
<rect x="50" y="80" width="100" height="80" />
<polygon points="40,80 100,40 160,80" fill="red" />
<rect x="80" y="120" width="20" height="40" fill="darkbrown" />
<rect x="110" y="100" width="15" height="15" fill="lightblue" />
</g>
</svg>SVG Animations: Bringing Graphics to Life
CSS Animations
<style>
.spinning-circle {
animation: spin 2s linear infinite;
transform-origin: center;
}
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
</style>
<svg width="400" height="200" viewBox="0 0 400 200">
<circle cx="100" cy="100" r="40" fill="lightblue" stroke="navy" stroke-width="3" class="spinning-circle" />
</svg>SVG-Specific Animations
<svg width="300" height="200" viewBox="0 0 300 200">
<circle cx="50" cy="100" r="20" fill="orange">
<animate attributeName="cx" values="50;250;50" dur="3s" repeatCount="indefinite" />
<animate attributeName="fill" values="orange;red;purple;orange" dur="3s" repeatCount="indefinite" />
</circle>
</svg>Gradients
Linear Gradients
<svg width="300" height="200" viewBox="0 0 300 200">
<defs>
<linearGradient id="sunsetGradient" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:orange" />
<stop offset="50%" style="stop-color:red" />
<stop offset="100%" style="stop-color:purple" />
</linearGradient>
</defs>
<rect x="50" y="50" width="200" height="100" fill="url(#sunsetGradient)" />
</svg>Creating Interactive SVGs with JavaScript
<script>
function changeColor() {
const circle = document.getElementById('clickableCircle');
const colors = ['red', 'blue', 'green', 'orange', 'purple'];
const randomColor = colors[Math.floor(Math.random() * colors.length)];
circle.setAttribute('fill', randomColor);
}
</script>
<svg width="200" height="200" viewBox="0 0 200 200">
<circle id="clickableCircle" cx="100" cy="100" r="60" fill="lightblue" style="cursor: pointer;" onclick="changeColor()" />
</svg>Performance Tips and Best Practices
- Keep designs simple to start.
- Remove unnecessary path points.
- Use CSS classes for maintainability.
- Organize elements with
<g>tags. - Set appropriate
viewBoxvalues. - Compress SVG files with tools like SVGO.
Conclusion
SVGs are scalable vector graphics suitable for everything from simple icons to complex, interactive visuals. Start with basic shapes, then progressively layer on paths, gradients, animation, and interactivity. Mastery comes with practice — so go experiment!