HTML SVG

Beginner
14 min read
Updated: Nov 2026
6 examples
Vector graphics

Definition and Usage

SVG (Scalable Vector Graphics) is a format for drawing images with shapes and paths instead of a grid of pixels. On an HTML page, SVG stays sharp when you zoom or resize—ideal for icons, logos, and diagrams.

What does this mean for HTML authors?

You do not need to master every SVG feature to use it. This tutorial focuses on how to put vector graphics into a webpage: with <img>, by pasting <svg> markup inline, with <iframe>, or as a CSS background.

Vector vs raster on the web

Raster files (PNG, JPEG, GIF) store pixels—great for photos, but they look blocky when enlarged. Vector SVG stores instructions the browser redraws at any size, so edges stay clean.

When should you use SVG?

Prefer SVG for UI icons, logos, charts, and simple illustrations. Prefer raster for photographs. Prefer canvas for pixel games and heavy JS drawing.

Sharp at any size.

Scale an SVG logo in CSS or with width/height—it should stay crisp on retina screens.

Not a full SVG course.

This page teaches embedding SVG in HTML. For the <svg> element itself (viewBox, accessibility), see HTML svg Tag. For shape deep-dives, continue to SVG tutorials.

Editors help.

Hand-code simple circles and rects; export complex art from Inkscape, Illustrator, or Figma as .svg.

HTML Syntax — Ways to Include SVG

Four common patterns on an HTML page:

HTML
<img src="logo.svg" alt="Company logo" width="120" height="40">
HTML
<svg width="200" height="200" role="img" aria-label="Blue circle">
  <circle cx="100" cy="100" r="60" fill="#3b82f6"/>
</svg>
HTML
<iframe src="diagram.svg" title="Process diagram" width="400" height="300"></iframe>
CSS
.hero {
  background-image: url("pattern.svg");
  background-size: cover;
}

Core HTML Concepts

Choose an embed method by what the page needs:

MethodBest forCSS / JS from the page
<img src="*.svg">Icons, logos, cached assetsLimited (treat as an image)
Inline <svg>…</svg>Hover styles, DOM, a11y inside graphicFull (page CSS & script)
<iframe src="*.svg">Standalone SVG documentsLimited / same-origin only
CSS background-imageDecorative patternsBackground only (not content)

img vs inline vs canvas

img + SVG fileInline SVGCanvas
Scales sharplyYesYesNo (bitmap)
Browser cacheYesNo (in HTML)N/A
Style with page CSSHardEasyDraw via JS
Typical useStatic iconsInteractive UI artGames / pixels

Common SVG shapes (quick map)

ElementDraws
<circle>Circle (cx, cy, r)
<rect>Rectangle
<path>Custom curves and shapes
<line> / <polygon>Lines and multi-point shapes
<text>Text that stays selectable / accessible

How to Add SVG to an HTML Page

A practical workflow:

  1. Decide if the graphic is decorative, a logo, or needs interaction / hover styles.
  2. For a reusable file → save icon.svg and use <img> with meaningful alt.
  3. For CSS hover or scripting → paste the <svg>…</svg> markup into the HTML.
  4. Set width / height (or CSS size) so layout does not jump.
  5. For meaningful graphics, add accessible text (alt on img, or title / aria-label on inline SVG).
GoalHTML pattern
Show a logo file<img src="logo.svg" alt="…">
Hover-color iconInline <svg> + CSS :hover
Decorative patternCSS background-image: url(…svg)
Fallback for old browsers<img src="icon.png" srcset="icon.svg">

Live Preview

Inline SVG scales cleanly. Drag the slider—shapes redraw sharply at every size.

160px

Same markup as an inline <svg> on an HTML page.

Preview

HTML Examples

Six hands-on patterns: basic circle, multiple shapes, attributes, CSS styling, animation, and a full page.

Example 1 — Basic Circle (Inline SVG)

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

  1. svg creates a drawing area in the HTML page.
  2. circle uses center (cx, cy) and radius r.
  3. fill / stroke color the shape without CSS.
  4. Inline means the graphic is part of the document DOM.

Example 2 — Rectangle and Circle Together

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

  1. One svg can hold many shapes.
  2. Paint order follows markup—later shapes sit on top.
  3. rect and circle share fill/stroke attributes.
  4. Diagrams are built by stacking simple elements.

Example 3 — Inline Attribute Styling

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

  1. Presentation attributes style a one-off shape quickly.
  2. No stylesheet required for basic colors.
  3. Reusable look → prefer CSS classes (next example).
  4. img embeds cannot be restyled this way from the page.

Example 4 — CSS Class Styling

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

  1. Inline SVG lives in the HTML DOM like other elements.
  2. Page CSS can target classes and :hover.
  3. This is a key reason to choose inline over img for interactive icons.
  4. External SVG via img usually cannot use these page styles.

Example 5 — Simple Animation

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

  1. animate changes an attribute over time.
  2. cx slides the circle horizontally.
  3. Inline SVG is required for this markup to live in the page.
  4. Modern alternative: CSS @keyframes on inline SVG.

Example 6 — Complete HTML Page

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

  1. Full HTML document hosts inline SVG like any other content.
  2. CSS classes style shapes from the page stylesheet.
  3. title / desc help describe meaningful graphics.
  4. path draws a curve—hand-code simple paths or export from an editor.

Edge Cases & Pitfalls

Most SVG-in-HTML issues are the wrong embed method or missing accessibility text.

method

Expecting page CSS to style an img SVG

External files in img are largely opaque to your stylesheet. Inline the SVG if you need hover colors.

size

Huge inline SVG everywhere

Duplicating large markup bloats HTML and skips caching. Reuse a file with img when you do not need DOM access.

a11y

Meaningful graphic with no alt / title

Logos and informative diagrams need text alternatives. Decorative SVGs can use aria-hidden="true".

photo

Tracing a photo into SVG

Complex photos often make heavy SVG files. Prefer JPEG/PNG for photographs.

iframe

Using iframe as the default

Usually worse than img or inline for icons. Prefer the simpler methods first.

server

Wrong MIME type for .svg files

Servers should serve SVG as image/svg+xml. Misconfiguration can prevent images from showing.

Browser Support

SVG in HTML via img and inline svg is Baseline and supported in all modern browsers. Prefer img or inline over iframe for most UI graphics.

✓ Baseline · Widely available

SVG on HTML pages

Vector graphics embed cleanly with img, inline svg, and CSS backgrounds across current browsers.

100% SVG in HTML
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
HTML SVG Universal

Bottom line: Pick img for cached static icons, inline svg when you need CSS or DOM—and keep photos as raster.

Key Takeaways

  • SVG is vector art that stays sharp in HTML pages.
  • img for simple display; inline svg for CSS and scripting.
  • Use raster for photos; SVG for icons, logos, and diagrams.
  • Tag details live on /html/tags/svg; shape lessons continue under /svg.

One line: Put SVG on the page with img when you only need to show it, or inline <svg> when you need to style or script it.

Frequently Asked Questions

SVG (Scalable Vector Graphics) describes images with shapes and paths instead of pixels. On HTML pages you can show SVG with img, paste the markup inline, use an iframe, or set a CSS background.
This tutorial teaches how to include vector graphics in HTML (img vs inline vs iframe vs CSS). The /html/tags/svg page is the element reference—viewBox, attributes, accessibility details for the svg tag itself.
Use img (or object) for icons and logos you only need to display and cache. Use inline svg when you need CSS hover styles, DOM access, or title/desc inside the graphic.
SVG stays sharp at any size and is great for icons, logos, and diagrams. Raster formats (PNG, JPEG) are better for photos and complex pictures with many colors.
Yes for inline SVG in the HTML document—classes, hover, and animations work. External SVG loaded through img has limited CSS control from the page stylesheet.
Simple shapes can be hand-coded. Complex illustrations are usually made in Inkscape, Illustrator, or Figma, then exported as .svg and embedded in HTML.

Practice in the editor

Open a Try It demo and edit inline SVG shapes on a full HTML page.

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.

8 people found this page helpful