HTML <svg> Tag

Beginner
6 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <svg> element embeds scalable vector graphics in HTML. Shapes stay sharp at any size because they are drawn from math, not pixels.

Paste an inline <svg>…</svg> into the page when you need DOM access or CSS. To show an existing .svg file by URL, prefer <img> (or <object>). For bitmap drawing with JavaScript, use <canvas>.

Baseline · Widely available.

Core inline SVG in HTML has been widely available since about July 2015. Some advanced SVG features vary by browser.

xmlns in HTML pages.

In normal HTML serialization you can omit xmlns. It is required for standalone SVG/XML files.

HTML Syntax

syntax.html
<svg viewBox="0 0 100 100" width="120" height="120">…</svg>

Both start and end tags are required. Put shapes such as <circle>, <rect>, and <path> inside.

Attributes

Common attributes when embedding SVG in HTML (plus globals):

AttributeDescriptionTry it
viewBoxInternal coordinate box: min-x min-y width heightTry it
widthDisplayed viewport width (length or auto)Try it
heightDisplayed viewport height (length or auto)Try it
preserveAspectRatioHow the viewBox fits if the aspect ratio differs (default xMidYMid meet)Optional
xmlnsSVG namespace — omit in normal HTML; required in standalone SVG/XMLUsually omit

Global Attributes

The <svg> tag also supports the Global Attributes in HTML.

Live Preview

Switch a simple inline graphic. All stay sharp because they are vectors.

Inline <svg> with a viewBox and basic shapes.

Result

A circle drawn in a 100×100 viewBox.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Basic Shapes

Set a viewBox, then draw shapes in that coordinate system.

HTML
<svg width="200" height="120" viewBox="0 0 200 120" role="img" aria-label="Blue rectangle and orange circle">
  <rect x="10" y="20" width="100" height="60" fill="skyblue" />
  <circle cx="160" cy="60" r="30" fill="orange" />
</svg>
Try It Yourself

How It Works

  1. Open <svg> with width, height, and viewBox.
  2. Add shapes using coordinates inside the viewBox.
  3. Close </svg> — both tags are required.
  4. Skip xmlns in normal HTML pages.

Example 2 — Accessible Icon

Meaningful graphics need a short accessible name. Put <title> first.

HTML
<svg viewBox="0 0 24 24" width="48" height="48" role="img">
  <title>Checkmark</title>
  <circle cx="12" cy="12" r="10" fill="#16a34a" />
  <path d="M7 12 l3 3 l7 -7" fill="none" stroke="#fff" stroke-width="2" />
</svg>
Try It Yourself

How It Works

  1. Set role="img" so assistive tech treats it as an image.
  2. Add <title> first with a short description.
  3. Keep the drawing simple for icons.
  4. Hide decorative SVGs with aria-hidden="true" when they add no meaning.

Example 3 — Scale with viewBox

Same drawing, different display size — the viewBox keeps proportions.

HTML
<svg viewBox="0 0 100 100" width="64" height="64" role="img" aria-label="Small teal square">
  <rect x="10" y="10" width="80" height="80" fill="teal" />
</svg>
<svg viewBox="0 0 100 100" width="128" height="128" role="img" aria-label="Large teal square">
  <rect x="10" y="10" width="80" height="80" fill="teal" />
</svg>
Try It Yourself

How It Works

  1. Keep one viewBox for the drawing coordinates.
  2. Change only width/height to resize on the page.
  3. The browser maps the viewBox onto that viewport.
  4. Use CSS (width: 100%, height: auto) for fluid layouts.

Example 4 — Inline svg vs img

Inline SVG is editable in the page. An external file is simpler to cache and reuse.

HTML
<!-- Inline: shapes live in the DOM -->
<svg viewBox="0 0 24 24" width="32" height="32" role="img" aria-label="Star">
  <polygon points="12,2 15,9 22,9 17,14 19,21 12,17 5,21 7,14 2,9 9,9" fill="#eab308" />
</svg>

<!-- External file: display only -->
<img src="icon.svg" width="32" height="32" alt="Star">
Try It Yourself

How It Works

  1. Choose inline svg when you style or script the shapes.
  2. Choose img when the graphic is a separate asset.
  3. Always provide a name (title / aria-label or alt).
  4. Prefer canvas only when you need pixel drawing with JS.

Edge Cases & Pitfalls

Most SVG mistakes mix up viewport size, coordinate space, or accessibility.

viewBox

Width/height ≠ viewBox

width/height size the on-screen box. viewBox defines the drawing coordinates mapped into that box.

xmlns

Do not require xmlns in HTML

Leaving it in pasted SVG is usually harmless, but it is not required for HTML documents.

a11y

Name meaningful graphics

Use role="img" plus <title> or aria-label. Decorative SVGs should be hidden from assistive tech.

ids

IDs must be unique on the page

Gradients and clips use id. Duplicate IDs across multiple inline SVGs break fills and masks.

vs canvas

Not a pixel canvas

SVG elements stay in the DOM. Heavy per-frame animation may fit <canvas> better.

Browser Support

Inline <svg> in HTML is core web platform support. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<svg>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera all render inline SVG. Some advanced SVG modules vary.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Partial / legacy
<svg> Widely supported

Bottom line: Safe for modern browsers. Prefer viewBox for scaling, name meaningful icons, and use img when the SVG is just an external asset.

Key Takeaways

  • Role: <svg> embeds scalable vector graphics in HTML.
  • viewBox: defines drawing coordinates; width/height size the viewport.
  • Compare: img for files; canvas for pixels; inline svg for DOM vectors.
  • a11y: give meaningful SVGs a title or label; hide decorative ones.

One line: use inline <svg> with a viewBox when you need sharp, scalable graphics in the DOM.

Default CSS Settings

Typical user-agent styling for SVG embedded in HTML (simplified):

default.css
svg:not(:root) {
  overflow: hidden;
  display: inline-block;
}

You can size SVG with CSS just like other replaced-style boxes. Keep viewBox so scaling stays proportional.

Frequently Asked Questions

The svg element embeds scalable vector graphics in a page. Inline SVG puts shapes in the DOM so they stay sharp at any size and can be styled or scripted.
No. In normal HTML pages, xmlns is unnecessary. It is required for standalone SVG/XML documents or XML-serialized HTML.
viewBox sets the internal coordinate system (min-x min-y width height). The browser maps that box to the rendered width and height so the graphic scales cleanly.
Use inline svg when you need DOM access, CSS styling, or accessibility markup inside the graphic. Use img (or object) when the SVG is a separate file you only need to display.
svg is a vector DOM tree of shapes. canvas is a bitmap drawing surface controlled by JavaScript. Prefer svg for icons and scalable UI art; canvas for games and pixel drawing.
Treat meaningful graphics like images: add role="img" and a short <title> (first child), or aria-label. Decorative SVGs can use aria-hidden="true".

Next: table

Learn how to structure tabular data with the table element.

table tutorial →

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.

12 people found this page helpful