HTML <svg> Tag

Beginner
⏱️ 7 min read
📚 Updated: Jun 2026
🎯 3 Examples
Graphics

What You’ll Learn

The <svg> tag embeds scalable vector graphics in HTML. This guide covers syntax, shapes, icons, viewBox sizing, comparisons with img and canvas, accessibility, and best practices for beginners.

01

Inline SVG

Vector graphics in HTML markup.

02

Basic Shapes

Rectangles, circles, lines, paths.

03

Icons & Logos

Sharp graphics at any size.

04

viewBox Sizing

Responsive coordinate scaling.

05

svg vs img / canvas

Choose the right format.

06

Best Practices

Optimize, label, and test SVGs.

What Is the <svg> Tag?

The <svg> tag is an HTML element used to embed scalable vector graphics directly into web pages. Unlike raster images (JPEG, PNG), SVG graphics are resolution-independent and stay sharp when scaled up or down.

Valid HTML5 — Vector Graphics Container

SVG content is written as XML-like markup inside svg. Child elements such as rect, circle, and path become part of the DOM and can be styled with CSS.

Common uses include icons, logos, charts, diagrams, maps, and decorative illustrations. Because SVG is markup, it works well for responsive layouts and accessible graphics.

📝 Syntax

Wrap your vector graphic content between opening and closing svg tags:

syntax.html
<svg width="100" height="100">
  <!-- Your SVG content here -->
</svg>

Rectangle and Circle Example

basic-shapes.html
<svg width="200" height="200" role="img" aria-label="Blue square and red circle">
  <rect width="100" height="100" fill="blue" />
  <circle cx="150" cy="150" r="50" fill="red" />
</svg>

Syntax Rules

  • In HTML5, xmlns is optional for inline SVG; it is required in standalone SVG files.
  • Child shape elements like rect and circle use XML-style self-closing tags.
  • Set width and height (or CSS) so the graphic has a visible box.
  • Use viewBox when you want internal coordinates to scale responsively.

⚡ Quick Reference

TopicCode SnippetNotes
Basic container<svg>...</svg>Vector root
Sizewidth="200" height="150"Display box
viewBoxviewBox="0 0 200 150"Coordinate system
Rectangle<rect width="100" height="50" fill="blue"/>Basic shape
Circle<circle cx="50" cy="50" r="40"/>Basic shape
Accessibilityrole="img" aria-label="..."Screen readers

⚖️ <svg> vs <img>

Feature<svg><img>
FormatVector markup (DOM)Raster file (PNG, JPEG, WebP)
ScalingStays sharp at any sizeCan pixelate when enlarged
SourceInline markup or external .svg filesrc attribute points to image file
StylingCSS can target SVG elementsLimited to image-level CSS
Best forIcons, logos, diagramsPhotos and complex bitmap art

⚖️ <svg> vs <canvas>

Choose the right graphics technology for your project:

FeatureSVG<canvas>
TypeVector XML markupBitmap pixels
DrawingMarkup + optional JavaScriptJavaScript Canvas API required
Best forIcons, logos, accessible diagramsGames, dynamic charts, filters
AccessibilityNative DOM elements + ARIANeeds aria-label and alternatives

🧰 Attributes

The <svg> element supports sizing, coordinate, and accessibility attributes plus global HTML attributes.

attributes.html
<svg width="200" height="150" viewBox="0 0 200 150" role="img" aria-label="Decorative chart">
  <!-- Your SVG content here -->
</svg>
width / height Sizing

Sets the displayed dimensions of the SVG viewport in CSS pixels.

width="200" height="150"
viewBox Scaling

Defines min-x, min-y, width, and height of the internal coordinate system.

viewBox="0 0 200 150"
role / aria-label A11y

Describe meaningful graphics for assistive technologies.

role="img" aria-label="Sales chart"
class / id Global

Hook for CSS to style the SVG container or child elements.

class="site-logo"

Examples Gallery

viewBox layouts, basic shapes, and icon-style graphics with copy-ready code and live previews.

👀 Live Preview

Inline SVG with rectangle and circle:

📚 Common Use Cases

Use <svg> for icons, logos, charts, diagrams, and any graphic that must scale cleanly across screen sizes.

viewBox and Coordinates

Control responsive scaling with viewBox while drawing shapes inside a fixed coordinate space:

viewbox-layout.html
<svg width="200" height="150" viewBox="0 0 200 150">
  <rect x="10" y="10" width="100" height="50" fill="skyblue" />
  <circle cx="150" cy="75" r="30" fill="orange" />
</svg>
Try It Yourself

Basic Shapes

The svg tag enables rectangles, circles, lines, and paths as a foundation for more complex graphics:

basic-shapes.html
<svg width="200" height="200">
  <rect width="100" height="100" fill="blue" />
  <circle cx="150" cy="150" r="50" fill="red" />
</svg>
Try It Yourself

Icons and Logos

SVGs are ideal for icons and logos because they remain sharp at any display size:

icons-and-logos.html
<svg width="100" height="100" role="img" aria-label="Red circle icon">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>
Try It Yourself

♿ Accessibility

  • Add meaningful labels — Use role="img" with aria-label or aria-labelledby for informative graphics.
  • Mark decorative SVGs — Use aria-hidden="true" when the graphic adds no information beyond decoration.
  • Provide text alternatives — Include visible captions or nearby text for charts and diagrams.
  • Keep contrast readable — Ensure stroke and fill colors meet contrast guidelines.

🧠 How <svg> Works

1

Author writes vector markup

Shapes and paths go inside the svg container with coordinates and styles.

Markup
2

Browser builds the graphic

The SVG viewport maps coordinates to pixels using width, height, and viewBox.

Render
3

Graphics scale cleanly

Vector paths stay sharp on retina displays and responsive layouts.

UX
=

Crisp, scalable visuals

Icons, logos, and diagrams render sharply without extra image files.

Browser Support

The <svg> tag is fully supported in all modern browsers. Legacy Internet Explorer has partial limitations in older versions.

Baseline · HTML5

Vector graphics everywhere

Modern browsers render inline SVG with full DOM access and CSS styling.

95% Core tag support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ · Partial
Partial support
Opera Fully supported
Full support
<svg> tag 95% supported

Bottom line: Use <svg> confidently in modern browsers; test legacy IE if you still support it.

Conclusion

The HTML <svg> tag lets you integrate scalable vector graphics directly into your pages with clarity and flexibility. Understanding syntax, viewBox, accessibility, and optimization helps you build sharp icons, logos, and diagrams for any screen size.

💡 Best Practices

✅ Do

  • Optimize SVGs by removing unnecessary metadata and comments
  • Minify SVG code to reduce file size in production
  • Add role="img" and aria-label for meaningful graphics
  • Use viewBox for responsive scaling

❌ Don’t

  • Embed huge unoptimized SVG files inline on every page
  • Skip accessibility labels on informative charts or icons
  • Use SVG for detailed photographs (use img instead)
  • Assume all legacy browsers support every advanced SVG feature

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <svg>

Bookmark these before you add vector graphics to your pages.

6
Core concepts
02

Shapes

rect, circle, path.

Markup
📏 03

viewBox

Responsive coords.

Sizing
⚖️ 04

vs img / canvas

Right tool choice.

Comparison
05

aria-label

Accessible graphics.

A11y
🌐 06

Modern Support

All major browsers.

Compatibility

❓ Frequently Asked Questions

It embeds scalable vector graphics in HTML. SVG stays sharp at any size and can be styled with CSS.
img shows raster files via src. svg contains vector markup that scales without quality loss.
width and height set display size. viewBox defines the internal coordinate system for responsive scaling.
No. Basic shapes render without JavaScript. Use JS only for dynamic drawing or interactions.
Yes in all modern browsers. Legacy Internet Explorer versions have partial limitations.

Draw scalable graphics

Practice <svg> shapes, viewBox, and icons in the Try It editor.

Try viewBox example →

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