Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Embedded Content

The <svg> element

Reference for the <svg> element in HTML: inline vector graphics, viewBox and preserveAspectRatio, sizing, accessible SVG with role and title, and technical details.

The <svg> element is the root of an inline SVG graphic: shapes, paths, text and gradients described as markup and drawn by the browser. Because it is vector-based, it stays sharp at any size and zoom level, and every shape is a real element you can style with CSS and change with JavaScript.

Writing <svg> in HTML switches the parser to the SVG namespace, so the xmlns attribute is optional. Inside it you use SVG elements such as <circle>, <rect>, <path> and <text>.

HTML
<svg width="100" height="100" viewBox="0 0 100 100">
  <circle cx="50" cy="50" r="40" fill="teal"/>
</svg>

Try it

An inline icon styled with CSS
viewBox and preserveAspectRatio

Both graphics draw the same circle in a 100 ร— 100 coordinate system. By default it is scaled evenly and centered; with preserveAspectRatio="none" it is stretched to fill the box.

Attributes

Attribute Values Description
viewBox min-x min-y width height The internal coordinate system. The content is scaled from these units to the element's actual size. Without it, one unit equals one CSS pixel and nothing scales.
width Length or percentage The rendered width. CSS width overrides it. Without a width, an inline <svg> that has a viewBox usually stretches to the width of its container.
height Length or percentage The rendered height. CSS height overrides it. Without a height, a viewBox lets the browser compute the height from the width and the aspect ratio.
preserveAspectRatio none, or an alignment (xMidYMid, xMinYMin, โ€ฆ) plus meet or slice How the viewBox fits a box of a different shape. Default: xMidYMid meet (scale evenly to fit, centered). slice fills the box and crops; none stretches.
x, y Length The position of a nested <svg> inside another SVG. Ignored on the outermost <svg>.
xmlns http://www.w3.org/2000/svg The SVG namespace. Optional inline in HTML; required in standalone .svg files.

It also accepts the global attributes such as class, id, style, tabindex and the ARIA attributes, plus SVG presentation attributes such as fill, stroke and font-family, which are inherited by the shapes inside.

Usage notes

  • Meaningful graphics. Add role="img" and an accessible name, either with a <title> child referenced by aria-labelledby (as above) or with aria-label. Screen readers then announce it as a single image.
  • Decorative graphics. For icons next to visible text, add aria-hidden="true" so the graphic is skipped. Also add focusable="false" if you support very old browsers that made SVG focusable.
  • Always set a viewBox. It is what makes the graphic scale. Then control its size with CSS or width and height.
  • Inline vs. <img>. Inline SVG can be styled from the page's CSS, animated and scripted, but it is repeated in every page's HTML. An SVG file in an <img> is cached and simpler, but its insides can't be styled from the page and its scripts don't run.
  • currentColor. Using fill="currentColor" makes an icon take the text color of its surroundings, so it follows hover states and dark mode automatically.
  • IDs are global. IDs inside inline SVG (gradients, clip paths, <title>) share the page's ID space. Make them unique when the same graphic appears more than once.
  • Security. Inline SVG can contain scripts and event handlers. Never inline SVG files uploaded by users without sanitizing them.

Technical summary

Content categories Flow content, phrasing content, embedded content, palpable content
Permitted content SVG content, as defined by the SVG 2 specification
Tag omission None; both the start and end tags are required (or a self-closing <svg/> when empty)
Permitted parents Any element that accepts phrasing content
Implicit ARIA role graphics-document
DOM interface SVGSVGElement
Default display inline (a replaced element; 300 ร— 150 pixels when no size can be determined)

Browser support

Supported in every browser.