A comprehensive guide to SVG (Scalable Vector Graphics), covering vector graphics fundamentals, implementation techniques, and applications in modern web design.
SVG (Scalable Vector Graphics) is an XML-based vector image format that uses mathematical descriptions of shapes, paths, and colors to create graphics that can be scaled to any size without loss of quality. SVG is ideal for logos, icons, simple illustrations, and graphics that need to look crisp at any resolution.
SVG is characterized by:
| Feature | SVG (Vector) | Bitmap (Raster) |
|---|---|---|
| Scalability | Infinite, no quality loss | Limited, pixelation occurs |
| File Size | Small for simple graphics | Depends on dimensions |
| Best For | Logos, icons, simple graphics | Photographs, complex images |
| Editability | Code-based, highly editable | Pixel-level editing |
| Animation | CSS/JS animations | Frame-based animation |
| Interactivity | Full CSS/JS support | Limited interactivity |
SVG documents follow XML syntax:
<!-- Basic SVG structure -->
<svg xmlns="http://www.w3.org/2000/svg"
width="200"
height="200"
viewBox="0 0 200 200">
<!-- SVG content goes here -->
<circle cx="100" cy="100" r="50" fill="#3498db" />
</svg>Basic shapes and elements in SVG:
<!-- Rectangle -->
<rect x="10" y="10" width="100" height="50"
fill="#e74c3c" stroke="#c0392b" stroke-width="2" />
<!-- Circle -->
<circle cx="50" cy="50" r="30"
fill="#2ecc71" opacity="0.8" />
<!-- Ellipse -->
<ellipse cx="100" cy="50" rx="40" ry="20"
fill="#f39c12" />
<!-- Line -->
<line x1="0" y1="0" x2="100" y2="100"
stroke="#9b59b6" stroke-width="3" />
<!-- Polygon -->
<polygon points="50,5 20,99 95,39 5,39 80,99"
fill="#1abc9c" />Complex shapes using path elements:
<!-- Path element for complex shapes -->
<path d="M 10 10 L 100 10 L 100 100 L 10 100 Z"
fill="none"
stroke="#34495e"
stroke-width="2" />
<!-- Curved path -->
<path d="M 10 80 Q 52.5 10, 95 80 T 180 80"
fill="none"
stroke="#e67e22"
stroke-width="3" />
<!-- Path commands:
M = Move to
L = Line to
Q = Quadratic curve
C = Cubic curve
A = Arc
Z = Close path -->Adding text elements to SVG graphics:
<!-- Basic text -->
<text x="50" y="50"
font-family="Arial, sans-serif"
font-size="16"
fill="#2c3e50">
Hello SVG!
</text>
<!-- Text along a path -->
<defs>
<path id="textPath" d="M 10 50 Q 50 10 90 50" />
</defs>
<text>
<textPath href="#textPath">
Curved text example
</textPath>
</text>Styling SVG elements with attributes:
<!-- Inline styling with attributes -->
<circle cx="50" cy="50" r="30"
fill="#3498db"
stroke="#2980b9"
stroke-width="3"
opacity="0.8" />Using CSS to style SVG elements:
<!-- CSS styling -->
<style>
.primary-circle {
fill: #3498db;
stroke: #2980b9;
stroke-width: 2;
transition: fill 0.3s ease;
}
.primary-circle:hover {
fill: #5dade2;
}
.text-style {
font-family: 'Helvetica Neue', Arial, sans-serif;
font-size: 14px;
fill: #2c3e50;
text-anchor: middle;
}
</style>
<svg>
<circle cx="50" cy="50" r="30" class="primary-circle" />
<text x="50" y="55" class="text-style">Styled Text</text>
</svg>Advanced SVG styling techniques:
<!-- Linear gradient -->
<defs>
<linearGradient id="blueGradient" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:#3498db;stop-opacity:1" />
<stop offset="100%" style="stop-color:#2980b9;stop-opacity:1" />
</linearGradient>
<!-- Radial gradient -->
<radialGradient id="radialGradient" cx="50%" cy="50%" r="50%">
<stop offset="0%" style="stop-color:#f39c12;stop-opacity:1" />
<stop offset="100%" style="stop-color:#e67e22;stop-opacity:1" />
</radialGradient>
</defs>
<!-- Using gradients -->
<rect width="100" height="100" fill="url(#blueGradient)" />
<circle cx="150" cy="50" r="40" fill="url(#radialGradient)" />Different methods to include SVG in web pages:
<!-- Inline SVG in HTML -->
<div class="icon-container">
<svg width="24" height="24" viewBox="0 0 24 24">
<path d="M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z"
fill="#f39c12" />
</svg>
</div><!-- As an image -->
<img src="icon.svg" alt="Icon" width="24" height="24">
<!-- As background image -->
<div class="svg-background"></div>
<style>
.svg-background {
width: 100px;
height: 100px;
background-image: url('pattern.svg');
background-size: contain;
background-repeat: no-repeat;
}
</style>
<!-- Using object tag -->
<object data="graphic.svg" type="image/svg+xml" width="200" height="200">
<!-- Fallback content -->
<img src="fallback.png" alt="Graphic">
</object>Making SVG graphics responsive:
<!-- Responsive SVG -->
<svg viewBox="0 0 200 100"
preserveAspectRatio="xMidYMid meet"
style="width: 100%; height: auto; max-width: 400px;">
<rect width="200" height="100" fill="#ecf0f1" />
<text x="100" y="50" text-anchor="middle"
font-size="16" fill="#2c3e50">
Responsive SVG
</text>
</svg>
<!-- CSS for responsive SVG containers -->
<style>
.svg-container {
width: 100%;
max-width: 500px;
height: auto;
}
.svg-container svg {
width: 100%;
height: auto;
}
</style>Animating SVG with CSS:
<!-- CSS animations for SVG -->
<style>
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.1); }
}
.rotating-icon {
animation: rotate 2s linear infinite;
transform-origin: center;
}
.pulsing-circle {
animation: pulse 1.5s ease-in-out infinite;
transform-origin: center;
}
</style>
<svg width="100" height="100">
<g class="rotating-icon">
<rect x="40" y="40" width="20" height="20" fill="#3498db" />
</g>
<circle cx="50" cy="50" r="15" fill="#e74c3c" class="pulsing-circle" />
</svg>Native SVG animation using SMIL:
<!-- SMIL animations -->
<svg width="200" height="100">
<!-- Animated circle -->
<circle cx="50" cy="50" r="20" fill="#2ecc71">
<animate attributeName="cx"
values="50;150;50"
dur="3s"
repeatCount="indefinite" />
<animate attributeName="fill"
values="#2ecc71;#e74c3c;#2ecc71"
dur="3s"
repeatCount="indefinite" />
</circle>
<!-- Animated path -->
<path fill="none" stroke="#3498db" stroke-width="3">
<animate attributeName="d"
values="M10,50 Q50,10 90,50;M10,50 Q50,90 90,50;M10,50 Q50,10 90,50"
dur="2s"
repeatCount="indefinite" />
</path>
</svg>Programmatic SVG animation:
// JavaScript SVG animation
const circle = document.querySelector('.animated-circle');
let angle = 0;
function animateCircle() {
const x = 100 + 50 * Math.cos(angle);
const y = 100 + 50 * Math.sin(angle);
circle.setAttribute('cx', x);
circle.setAttribute('cy', y);
angle += 0.1;
requestAnimationFrame(animateCircle);
}
animateCircle();
// Using GSAP for complex animations
gsap.to('.svg-element', {
duration: 2,
rotation: 360,
scale: 1.5,
transformOrigin: 'center',
ease: 'power2.inOut',
repeat: -1,
yoyo: true
});Efficient icon management with SVG sprites:
<!-- SVG sprite definition -->
<svg style="display: none;">
<defs>
<symbol id="icon-home" viewBox="0 0 24 24">
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
</symbol>
<symbol id="icon-user" viewBox="0 0 24 24">
<path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
</symbol>
<symbol id="icon-search" viewBox="0 0 24 24">
<path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/>
</symbol>
</defs>
</svg>
<!-- Using sprite icons -->
<svg class="icon icon-home" width="24" height="24">
<use href="#icon-home"></use>
</svg>
<svg class="icon icon-user" width="24" height="24">
<use href="#icon-user"></use>
</svg>CSS techniques for styling SVG icons:
/* SVG icon styling */
.icon {
display: inline-block;
width: 1em;
height: 1em;
fill: currentColor;
vertical-align: middle;
}
/* Icon sizes */
.icon-sm { width: 16px; height: 16px; }
.icon-md { width: 24px; height: 24px; }
.icon-lg { width: 32px; height: 32px; }
/* Icon colors */
.icon-primary { fill: #3498db; }
.icon-secondary { fill: #e74c3c; }
.icon-success { fill: #2ecc71; }
/* Interactive icons */
.icon-button {
cursor: pointer;
transition: fill 0.2s ease;
}
.icon-button:hover {
fill: #2980b9;
}Techniques to reduce SVG file sizes:
# SVGO (SVG Optimizer)
npx svgo input.svg -o output.svg
# With specific optimizations
npx svgo --config='{"plugins":[{"removeViewBox":false}]}' input.svg
# Batch optimization
npx svgo -f ./icons -o ./optimized-iconsSVG performance best practices:
Making SVG graphics accessible:
<!-- Accessible SVG with proper labeling -->
<svg role="img" aria-labelledby="chart-title chart-desc">
<title id="chart-title">Sales Data Chart</title>
<desc id="chart-desc">
Bar chart showing sales data from January to December.
January: $10,000, February: $15,000, March: $12,000...
</desc>
<!-- Chart content -->
<rect x="10" y="50" width="20" height="100" fill="#3498db"
aria-label="January sales: $10,000" />
</svg>
<!-- Decorative SVG -->
<svg aria-hidden="true" focusable="false">
<!-- Decorative graphics that don't need description -->
</svg>Advanced visual effects with SVG:
<!-- Clipping path -->
<defs>
<clipPath id="circleClip">
<circle cx="50" cy="50" r="40" />
</clipPath>
</defs>
<image href="photo.jpg" width="100" height="100"
clip-path="url(#circleClip)" />
<!-- Masking -->
<defs>
<mask id="fadeMask">
<linearGradient id="fadeGradient">
<stop offset="0%" stop-color="white" />
<stop offset="100%" stop-color="black" />
</linearGradient>
<rect width="100%" height="100%" fill="url(#fadeGradient)" />
</mask>
</defs>
<rect width="200" height="100" fill="#3498db"
mask="url(#fadeMask)" />SVG filter effects:
<!-- SVG filters -->
<defs>
<filter id="dropShadow">
<feDropShadow dx="2" dy="2" stdDeviation="3" flood-color="#000" flood-opacity="0.3" />
</filter>
<filter id="blur">
<feGaussianBlur stdDeviation="2" />
</filter>
</defs>
<circle cx="50" cy="50" r="30" fill="#e74c3c"
filter="url(#dropShadow)" />
<text x="100" y="50" filter="url(#blur)">Blurred Text</text>Software for creating SVG graphics:
Tools for SVG optimization and management:
Best practices for creating effective SVG graphics:
SVG performance best practices:
Explore these comprehensive pillar articles related to SVG
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Discover more about SVG and related color theory concepts
A comprehensive guide to color theory, its fundamental principles, and practical applications in digital design and web development.
A comprehensive guide to monochromatic color schemes - sophisticated single-hue harmonies that create unified visual experiences through systematic variations in saturation, lightness, and tonal relationships, essential for elegant design compositions, brand consistency, and advanced color theory applications requiring cohesive aesthetic unity.
Comprehensive guide to analogous color schemes - adjacent hues on the color wheel that create natural harmony, visual cohesion, and sophisticated design solutions through subtle color relationships and graduated transitions.
Comprehensive guide to complementary colors - opposing hues on the color wheel that create maximum visual contrast, dynamic tension, and powerful design impact through scientific color relationships and perceptual phenomena.
A comprehensive guide to split-complementary color schemes - sophisticated three-color harmonies that combine a base color with two colors adjacent to its complement, creating balanced visual contrast with reduced tension compared to direct complementary pairs, essential for refined design compositions, brand identity systems, and advanced color theory applications.
A comprehensive guide to tetradic color schemes - sophisticated four-color harmonies that use two complementary pairs arranged in rectangular or square formations on the color wheel, providing maximum color variety while maintaining structured relationships essential for dynamic design compositions, brand systems, and advanced color theory applications.
Discover more color terminology and concepts in our comprehensive glossary.
Browse Full Glossary