SVG Graphics
Create scalable vector graphics with SVG. Perfect for icons, illustrations, data visualizations, and responsive graphics that look crisp at any size.
What is SVG and how do you use it in HTML?
SVG (Scalable Vector Graphics) is an XML-based format for vector images that stay sharp at any size. You can include it as a file (<img src="logo.svg">), as a CSS background, or inline as an <svg> element in the HTML. Inline SVG can be styled with CSS (fill: currentColor) and animated with CSS or JavaScript.
<svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="2"/>
<path d="M8 12l3 3 5-6" fill="none" stroke="currentColor" stroke-width="2"/>
</svg>SVG Fundamentals
Scalable Vector Graphics (SVG) is an XML-based vector image format that enables crisp, scalable graphics. Unlike raster images, SVG graphics maintain their quality at any size and can be styled with CSS, manipulated with JavaScript, and are SEO-friendly.
Scalable
Perfect quality at any size
Programmable
CSS styling & JS interaction
Lightweight
Small file sizes
SVG Elements & Shapes
Basic Shapes
- • <rect> - rectangles and squares
- • <circle> - circles
- • <ellipse> - ellipses
- • <line> - straight lines
- • <polygon> - closed shapes
- • <polyline> - connected lines
Advanced Elements
- • <path> - complex shapes & curves
- • <text> - scalable text
- • <image> - embedded images
- • <use> - reusable symbols
Styling & Effects
- • fill - shape fill color
- • stroke - outline properties
- • gradients - linear & radial
- • patterns - repeating designs
- • filters - visual effects
Interactivity
- • CSS animations & transitions
- • SMIL animations
- • JavaScript manipulation
- • Event handling
Complete SVG Examples
Interactive examples showcasing basic shapes, paths, text styling, gradients, patterns, animations, interactivity, icons, and data visualization with SVG.
Code Editor
<svg width="220" height="120" viewBox="0 0 220 120">
<rect x="10" y="10" width="80" height="80" rx="12" class="shape" />
<circle cx="150" cy="50" r="40" class="shape" />
<path d="M10 110 L210 110" stroke="currentColor" stroke-width="3" />
</svg>
<button class="icon-btn">
<svg width="20" height="20" viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 12l5 5L20 7" fill="none" stroke="currentColor" stroke-width="3" />
</svg>
Done
</button>Preview
SVG Best Practices
Optimization Tips
- • Use viewBox for responsive scaling
- • Minimize path complexity
- • Remove unnecessary attributes
- • Use symbols for repeated elements
- • Optimize with SVGO tools
Accessibility & SEO
- • Add meaningful <title> elements
- • Use <desc> for descriptions
- • Implement proper ARIA labels
- • Ensure keyboard navigation
- • Provide fallback content
SVG Development Checklist
- Test scalability across devices
- Validate SVG markup
- Optimize file size
- Check browser compatibility
- Implement fallbacks
- Test accessibility features
HTML SVG — step by step
Embed SVG inline
You can place SVG markup directly in your HTML. Inline SVG can be styled and manipulated with CSS and JavaScript, unlike SVG loaded as an img src.
🎯 Quick Check
What is the main advantage of using inline SVG over an img tag with an SVG src?
SVG in HTML: summary
Key points
viewBoxdefines the internal coordinate system, which is what makes SVG scale.- Use SVG for logos, icons, illustrations and charts; use raster formats (WebP, JPEG) for photos.
- Give meaningful inline SVGs
role="img"and a<title>, oraria-label. Hide decorative ones witharia-hidden="true". - Optimise exported SVGs with SVGO to remove editor metadata.
Frequently asked questions
›Should I use inline SVG or an img tag?
Inline SVG when you need to style or animate it with CSS, for example icons that change colour. Use <img src="…svg"> for static illustrations, because the browser can cache the file.