Scalable Vector Graphics (SVG) is an XML-based markup language for describing two-dimensional graphics. SVG is widely used on the web for icons, logos, charts, diagrams, and animations. Because SVG is vector-based, graphics stay sharp at any size or resolution—making it ideal for responsive web design and high-DPI screens.
This tutorial shows how to use SVG inside HTML pages: the basic syntax, common shape elements, styling, animation, accessibility, and complete working examples you can run in the browser.
What You’ll Learn
01
<svg>
Root container.
02
Shapes
rect, circle, path.
03
Attributes
fill & stroke.
04
CSS styling
Classes & hover.
05
viewBox
Responsive SVG.
06
a11y
title & desc.
Fundamentals
What Is SVG?
SVG is a vector image format that uses XML to describe shapes, lines, curves, and text. Unlike raster images (PNG, JPG, GIF) which store pixels, SVG stores mathematical paths. Scale an SVG to 10× or shrink it to an icon—edges stay crisp.
Modern browsers render SVG natively. You write SVG markup directly in HTML (or load an .svg file) without plugins or special libraries.
💡
Beginner Tip
Think of SVG like HTML for drawings. Each shape is its own element in the DOM—you can style it with CSS, animate it, and attach click handlers, just like a <div>.
Comparison
SVG vs Canvas vs Raster Images
Choosing the right graphics format depends on what you need:
SVG — Vector markup in the DOM. Best for icons, logos, charts, and UI illustrations. Scales perfectly; each shape is selectable and styleable.
HTML canvas — Bitmap drawn with JavaScript. Best for games, photo filters, and heavy pixel manipulation. See the HTML canvas tag tutorial.
PNG / JPG / WebP — Fixed-resolution pixels. Best for photographs and complex textures where vector paths would be impractical.
Reference
Basic SVG Syntax
An SVG graphic lives inside an <svg> element. That container holds shape elements and optional metadata:
html
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<!-- SVG shape elements go here -->
</svg>
width / height — size of the SVG viewport on the page (pixels by default).
xmlns — required namespace when SVG is standalone; browsers accept inline SVG in HTML5 without it, but including it is good practice.
viewBox — internal coordinate system (covered below for responsive graphics).
Integration
Ways to Use SVG in HTML
There are several ways to add SVG to a web page:
Inline SVG — paste <svg>...</svg> directly in HTML. Full CSS/JS control; best for icons and interactive graphics.
<img src="logo.svg"> — simple embedding; no DOM access to inner shapes.
<object data="chart.svg"> — loads external SVG as a separate document; rarely needed today.
CSS background — background-image: url(icon.svg) for decorative graphics.
This tutorial focuses on inline SVG because it is the most flexible approach for learning and for interactive web apps.
Shapes
Common SVG Elements
SVG provides dedicated elements for basic shapes and complex paths:
<svg> — root container for the graphic.
<rect> — rectangles (optionally with rounded corners via rx/ry).
<circle> — circles defined by center (cx, cy) and radius (r).
<ellipse> — ellipses with separate x and y radii.
<line> — straight line between two points.
<polyline> — series of connected straight segments (open shape).
<polygon> — closed shape with multiple sides.
<path> — arbitrary curves and shapes using path commands (M, L, Q, C, etc.).
viewBox="min-x min-y width height" maps internal coordinates to the displayed size. The circle above always fills the box proportionally, whether the SVG renders at 48 px or 480 px wide.
CSS
Styling SVG
Style SVG elements with presentation attributes directly on the tag, or with CSS classes and selectors:
CSS gives you pseudo-classes (:hover), media queries, and animations on SVG shapes—something you cannot do when SVG is loaded only as an external <img>.
Motion
Animating SVG
SVG has built-in animation elements. The SMIL <animate> tag changes an attribute over time (note: SMIL is deprecated in Chrome but still useful for learning; CSS animations are preferred for new projects):
For production sites, animate with CSS (@keyframes on transform or fill) or JavaScript for full control.
a11y
Accessibility Considerations
Make SVG content usable for screen reader users and keyboard navigation:
Add <title> and <desc> as the first children of <svg> to describe the graphic.
Use role="img" and aria-labelledby when the SVG conveys meaningful information.
For decorative icons, hide them with aria-hidden="true" so assistive tech skips them.
Ensure interactive SVG elements are focusable (tabindex="0") and operable with the keyboard.
html
<svg width="100" height="100" role="img"
aria-labelledby="svg-title svg-desc"
xmlns="http://www.w3.org/2000/svg">
<title id="svg-title">Blue Circle</title>
<desc id="svg-desc">A circle with a blue fill and black outline.</desc>
<circle cx="50" cy="50" r="40" fill="blue" stroke="black" stroke-width="3"/>
</svg>
Cheat Sheet
⚡ Quick Reference
Task
Code / Element
Container
<svg width="..." height="...">
Rectangle
<rect x="10" y="10" width="80" height="50"/>
Circle
<circle cx="50" cy="50" r="40"/>
Fill color
fill="tomato" or CSS .shape { fill: tomato; }
Outline
stroke="black" stroke-width="2"
Responsive
viewBox="0 0 100 100" + CSS width: 100%
Accessible label
<title>...</title> inside <svg>
Hands-On
Examples Gallery
Six examples from a single circle to a full HTML page with multiple shapes. Open Try It Yourself to edit and preview live in the browser.
CSS classes for rect and circle
<title> and <desc> for accessibility
<path> with quadratic curve (Q) command
How It Works
This combines everything from the tutorial: multiple shapes, CSS classes, accessibility metadata, and a <path> for a smooth curve. The path command Q draws a quadratic Bézier curve.
Applications
🚀 Common Use Cases
Icons and logos — crisp at any size on retina displays.
Data charts — bar, line, and pie charts with DOM-accessible segments.
Maps and diagrams — scalable illustrations for documentation and education.
Interactive UI — hover states, clickable regions, and animated infographics.
Responsive illustrations — hero graphics that scale with CSS layout.
Accessible graphics — labeled shapes for users who rely on screen readers.
🧠 How Inline SVG Works in HTML
1
Add <svg>
Create a viewport with width, height, and optional viewBox.
Container
2
Draw shapes
Add <rect>, <circle>, <path>, or other elements.
Markup
3
Style
Set fill/stroke attributes or CSS classes.
CSS
=
🎨
Scalable graphic
Browser renders crisp vector art that scales with your layout.
Pro Tips
💡 Best Practices
✅ Do
Use viewBox for responsive icons and illustrations
Add <title> and <desc> for meaningful graphics
Optimize SVG files—remove editor metadata and unused nodes
Prefer CSS animations over SMIL for new projects
Use SVG sprites or inline icons to reduce HTTP requests
Keep coordinates simple; use <path> for complex shapes only when needed
❌ Don’t
Embed large photographic content as SVG (use WebP/PNG instead)
Rely on SMIL animation in Chrome-only workflows
Forget aria-hidden="true" on purely decorative icons
Hard-code pixel sizes when the graphic must scale with layout
Export bloated SVG from design tools without running SVGO or similar
Assume <img src="file.svg"> can be styled with CSS like inline SVG
Compatibility
Universal Browser Support
Inline SVG is supported in all modern browsers—Chrome, Firefox, Safari, and Edge. SVG 1.1 and SVG 2 features (like viewBox, CSS styling, and DOM APIs) work consistently for typical web use cases.
✓ Baseline · Since HTML
Inline SVG in HTML
Inline SVG is supported in all modern browsers—Chrome, Firefox, Safari, and Edge. SVG 1.1 and SVG 2 features (like viewBox, CSS styling, and DOM APIs) work consistently for typical web use cases.
98%Modern browser support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Inline SVG in HTMLExcellent
Bottom line: SVG is a safe choice for icons, charts, and illustrations across all current browsers. SMIL animation is the main exception—prefer CSS for animated graphics.
Wrap Up
Conclusion
SVG is a versatile tool for creating scalable, interactive graphics on the web. By embedding the <svg> element in HTML, drawing shapes with declarative markup, and styling with CSS, you can build crisp visuals that work at any screen size.
Start with simple circles and rectangles, then explore dedicated shape tutorials like SVG Circle and SVG Path for deeper coverage. Combine SVG with good accessibility habits and responsive viewBox settings for production-ready graphics.
SVG (Scalable Vector Graphics) is an XML-based format for two-dimensional graphics. In HTML you embed SVG with the <svg> element and child shape tags like <circle>, <rect>, and <path>. Because SVG is vector-based, it scales to any size without losing sharpness.
Use inline SVG when you need CSS styling, JavaScript interaction, or animations on individual parts of the graphic. Use <img src="icon.svg"> for simple decorative icons that do not need scripting. Inline SVG also improves accessibility when you add <title> and <desc> elements.
viewBox defines the internal coordinate system of an SVG, for example viewBox="0 0 100 100". Combined with width="100%" in CSS, it makes the graphic responsive—the browser scales the drawing to fit its container while preserving aspect ratio.
Yes. Presentation attributes like fill and stroke can be set directly on elements or overridden with CSS selectors. CSS also enables :hover effects and @keyframes animations on SVG shapes.
SVG is declarative DOM markup—each shape is an element you can style and click. Canvas is a bitmap surface drawn with JavaScript; it is better for games and pixel effects. SVG stays sharp when scaled; canvas does not unless you redraw at higher resolution.
Add <title> and <desc> inside the <svg> for screen readers. For interactive SVG, ensure focusable elements have tabindex and keyboard handlers. Treat meaningful SVG like any other content—not purely decorative images.
Did you know?
SVG is a W3C standard and has been supported in browsers since the early 2000s. Major sites use inline SVG for icons because a single 2 KB file replaces multiple PNG sizes (@1x, @2x, @3x) and stays sharp on 4K displays.