👀 Live Preview
Inline SVG with rectangle and circle:

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.
Vector graphics in HTML markup.
Rectangles, circles, lines, paths.
Sharp graphics at any size.
Responsive coordinate scaling.
Choose the right format.
Optimize, label, and test SVGs.
<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.
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.
Wrap your vector graphic content between opening and closing svg tags:
<svg width="100" height="100">
<!-- Your SVG content here -->
</svg><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>xmlns is optional for inline SVG; it is required in standalone SVG files.rect and circle use XML-style self-closing tags.width and height (or CSS) so the graphic has a visible box.viewBox when you want internal coordinates to scale responsively.| Topic | Code Snippet | Notes |
|---|---|---|
| Basic container | <svg>...</svg> | Vector root |
| Size | width="200" height="150" | Display box |
| viewBox | viewBox="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 |
| Accessibility | role="img" aria-label="..." | Screen readers |
<svg> vs <img>| Feature | <svg> | <img> |
|---|---|---|
| Format | Vector markup (DOM) | Raster file (PNG, JPEG, WebP) |
| Scaling | Stays sharp at any size | Can pixelate when enlarged |
| Source | Inline markup or external .svg file | src attribute points to image file |
| Styling | CSS can target SVG elements | Limited to image-level CSS |
| Best for | Icons, logos, diagrams | Photos and complex bitmap art |
<svg> vs <canvas>Choose the right graphics technology for your project:
| Feature | SVG | <canvas> |
|---|---|---|
| Type | Vector XML markup | Bitmap pixels |
| Drawing | Markup + optional JavaScript | JavaScript Canvas API required |
| Best for | Icons, logos, accessible diagrams | Games, dynamic charts, filters |
| Accessibility | Native DOM elements + ARIA | Needs aria-label and alternatives |
The <svg> element supports sizing, coordinate, and accessibility attributes plus global HTML attributes.
<svg width="200" height="150" viewBox="0 0 200 150" role="img" aria-label="Decorative chart">
<!-- Your SVG content here -->
</svg>width / height SizingSets the displayed dimensions of the SVG viewport in CSS pixels.
width="200" height="150"viewBox ScalingDefines min-x, min-y, width, and height of the internal coordinate system.
viewBox="0 0 200 150"role / aria-label A11yDescribe meaningful graphics for assistive technologies.
role="img" aria-label="Sales chart"class / id GlobalHook for CSS to style the SVG container or child elements.
class="site-logo"viewBox layouts, basic shapes, and icon-style graphics with copy-ready code and live previews.
Inline SVG with rectangle and circle:
Use <svg> for icons, logos, charts, diagrams, and any graphic that must scale cleanly across screen sizes.
Control responsive scaling with viewBox while drawing shapes inside a fixed coordinate space:
<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>The svg tag enables rectangles, circles, lines, and paths as a foundation for more complex graphics:
<svg width="200" height="200">
<rect width="100" height="100" fill="blue" />
<circle cx="150" cy="150" r="50" fill="red" />
</svg>SVGs are ideal for icons and logos because they remain sharp at any display size:
<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>role="img" with aria-label or aria-labelledby for informative graphics.aria-hidden="true" when the graphic adds no information beyond decoration.Shapes and paths go inside the svg container with coordinates and styles.
The SVG viewport maps coordinates to pixels using width, height, and viewBox.
Vector paths stay sharp on retina displays and responsive layouts.
Icons, logos, and diagrams render sharply without extra image files.
The <svg> tag is fully supported in all modern browsers. Legacy Internet Explorer has partial limitations in older versions.
Modern browsers render inline SVG with full DOM access and CSS styling.
Bottom line: Use <svg> confidently in modern browsers; test legacy IE if you still support it.
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.
role="img" and aria-label for meaningful graphicsviewBox for responsive scalingimg instead)<svg>Bookmark these before you add vector graphics to your pages.
Scales without blur.
Purposerect, circle, path.
MarkupResponsive coords.
SizingRight tool choice.
ComparisonAccessible graphics.
A11yAll major browsers.
Compatibilityimg 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.Practice <svg> shapes, viewBox, and icons in the Try It editor.
6 people found this page helpful