HTML SVG

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 6 Examples + 6 Try It
<svg> element

Introduction

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.

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>.

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.

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).

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 backgroundbackground-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.

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.).
  • <text> — text labels inside the graphic.

Explore each shape in depth in our dedicated tutorials: SVG Rectangle, SVG Circle, and SVG Path.

SVG Attributes

These presentation attributes appear on most shape elements:

  • width and height — dimensions of the SVG canvas (on the root <svg>).
  • x and y — position of elements like <rect> from the top-left origin.
  • fill — interior color (use none for hollow shapes).
  • stroke — outline color.
  • stroke-width — thickness of the outline in user units.
  • opacity — transparency from 0 (invisible) to 1 (opaque).
html
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
  <circle cx="60" cy="60" r="40"
          fill="blue" stroke="black" stroke-width="3"/>
</svg>

Responsive SVG with viewBox

The viewBox attribute defines the coordinate system inside the SVG. Pair it with CSS width for graphics that scale with the layout:

html
<style>
  .icon { width: 100%; max-width: 120px; height: auto; }
</style>

<svg class="icon" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" fill="#6366f1"/>
</svg>

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.

Styling SVG

Style SVG elements with presentation attributes directly on the tag, or with CSS classes and selectors:

Inline attributes

html
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40"
          fill="blue" stroke="black" stroke-width="3"/>
</svg>

External CSS

css
<style>
  .my-circle {
    fill: green;
    stroke: red;
    stroke-width: 2;
  }
  .my-circle:hover {
    fill: lightgreen;
  }
</style>

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" class="my-circle"/>
</svg>

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>.

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):

html
<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="30" fill="blue">
    <animate attributeName="cx" from="50" to="150"
             dur="2s" repeatCount="indefinite"/>
  </circle>
</svg>

For production sites, animate with CSS (@keyframes on transform or fill) or JavaScript for full control.

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>

⚡ Quick Reference

TaskCode / Element
Container<svg width="..." height="...">
Rectangle<rect x="10" y="10" width="80" height="50"/>
Circle<circle cx="50" cy="50" r="40"/>
Fill colorfill="tomato" or CSS .shape { fill: tomato; }
Outlinestroke="black" stroke-width="2"
ResponsiveviewBox="0 0 100 100" + CSS width: 100%
Accessible label<title>...</title> inside <svg>

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.

📚 Getting Started

Draw your first shapes inside an SVG container.

Example 1 — Basic Circle

Minimal inline SVG with a styled circle.

html
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <circle cx="100" cy="100" r="60"
          fill="#3b82f6" stroke="#1e3a8a" stroke-width="4"/>
</svg>
Try It Yourself

How It Works

cx and cy place the center; r sets the radius. The circle is drawn inside the 200×200 viewport.

Example 2 — Rectangle and Circle Together

Combine multiple shape elements in one SVG.

html
<svg width="220" height="120" xmlns="http://www.w3.org/2000/svg">
  <rect x="10" y="10" width="100" height="100" fill="lightblue" stroke="black" stroke-width="2"/>
  <circle cx="170" cy="60" r="40" fill="tomato" stroke="black" stroke-width="2"/>
</svg>
Try It Yourself

How It Works

Elements are painted in document order—the circle appears on top of the rectangle because it comes second.

🎨 Styling & Motion

Style with attributes, CSS classes, and simple animation.

Example 3 — Inline Attribute Styling

Set fill and stroke directly on the element.

html
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40"
          fill="blue" stroke="black" stroke-width="3"/>
</svg>
Try It Yourself

How It Works

Presentation attributes are quick for one-off shapes. For reusable styles, prefer CSS classes.

Example 4 — CSS Class Styling

Control appearance from a stylesheet and add hover effects.

css
<style>
  .my-circle { fill: green; stroke: red; stroke-width: 2; }
  .my-circle:hover { fill: lightgreen; }
</style>

<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="40" class="my-circle"/>
</svg>
Try It Yourself

How It Works

Inline SVG in HTML lets CSS target .my-circle just like any other element. Hover states work without JavaScript.

Example 5 — Simple Animation

Animate the horizontal position of a circle with <animate>.

html
<svg width="200" height="100" xmlns="http://www.w3.org/2000/svg">
  <circle cx="50" cy="50" r="25" fill="blue">
    <animate attributeName="cx" from="50" to="150"
             dur="2s" repeatCount="indefinite"/>
  </circle>
</svg>
Try It Yourself

How It Works

The circle slides horizontally because cx (center x) animates from 50 to 150. For broader browser support in new projects, prefer CSS @keyframes.

Example 6 — Complete HTML Page

A full page with rectangle, circle, and a curved path—matching the classic tutorial conclusion.

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>SVG Example</title>
  <style>
    .rect-style { fill: lightblue; stroke: black; stroke-width: 2; }
    .circle-style { fill: tomato; stroke: black; stroke-width: 2; }
  </style>
</head>
<body>
  <h1>SVG Example</h1>
  <svg width="220" height="200" xmlns="http://www.w3.org/2000/svg">
    <title>Sample shapes</title>
    <desc>A rectangle, circle, and green curve.</desc>
    <rect x="10" y="10" width="100" height="100" class="rect-style"/>
    <circle cx="170" cy="60" r="40" class="circle-style"/>
    <path d="M10 170 Q 60 140, 110 170 T 210 170"
          fill="none" stroke="green" stroke-width="4"/>
  </svg>
</body>
</html>
Try It Yourself

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.

🚀 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.

💡 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

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Inline SVG in HTML Excellent

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.

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.

Key Takeaways

🛠 02

<svg>

Root element.

HTML
🎨 03

CSS

Style shapes.

Design
📐 04

viewBox

Responsive.

Layout
05

a11y

title & desc.

Access

❓ Frequently Asked Questions

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.

Draw SVG in the editor

Open Try It, edit the circle’s fill and radius, and see the vector graphic update instantly.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful