<svg>
Vector graphics written directly in the page code: icons, logos, charts.
- Display
- inline
- Closing tag
- Required
- Permitted content
- SVG elements, for example
<rect>,<circle>,<path>,<text>,<g>
Example
#<svg width="260" height="120" viewBox="0 0 260 120" role="img" aria-labelledby="badge-title">
<title id="badge-title">Badge for completing the HTML course</title>
<rect x="4" y="4" width="252" height="112" rx="18" fill="#1e1b4b" stroke="#a78bfa" stroke-width="4" />
<circle cx="60" cy="60" r="34" fill="#f97316" />
<path d="M44 61 l11 11 l22 -24" fill="none" stroke="#fff" stroke-width="7" stroke-linecap="round" stroke-linejoin="round" />
<text x="112" y="56" fill="#f5f3ff" font-size="20" font-family="system-ui, sans-serif">HTML course</text>
<text x="112" y="82" fill="#c4b5fd" font-size="15" font-family="system-ui, sans-serif">completed</text>
</svg>Definition and usage
#The <svg> element lets you write vector graphics straight into HTML. Inside you draw with SVG shapes such as <rect> (rectangle), <circle> (circle), <line> (line), <path> (any shape), and <text> (text). Vector graphics stay sharp at any zoom level and usually weigh very little.
SVG is a separate XML-based language with its own elements and attributes. Unlike <canvas>, every shape remains an element in the DOM tree, so you can style it in CSS (fill, stroke), animate it, and handle a click like on an ordinary HTML element.
The viewBox attribute defines the internal coordinate system, and width and height the size on the page. This way the same icon drawn on a 24 by 24 grid can be 16 or 64 pixels on the page. Draw icons that should take on the text color with fill="currentColor".
Syntax
#<svg width="β¦" height="β¦" viewBox="0 0 β¦ β¦">
β¦
</svg>Attributes
#width / height
length, e.g. `24` or `100%`
The size of the graphic on the page. It is worth always providing them, because without them the size depends on the browser and the container, and the graphic may unexpectedly take up the full width of the page.min-x min-y width height
The internal coordinate system in which you draw shapes. WithviewBox="0 0 24 24"the point (12, 12) is always the center of the graphic, no matter how large you display it. In the example the same star has three different sizes.color, `none`, or `currentColor`
The fill and outline color of shapes. When set on the<svg>itself, all shapes inside inherit them. ThecurrentColorvalue takes the text color of the surroundings, so the icon changes together with it.xmlns
http://www.w3.org/2000/svg
The SVG namespace. In HTML code you can leave it out, but in a standalone.svgfile it is required.
The element also supports all global attributes, such as class, id, style and hidden.
Attributes in practice
#viewBox
min-x min-y width heightThe internal coordinate system in which you draw shapes. With viewBox="0 0 24 24" the point (12, 12) is always the center of the graphic, no matter how large you display it. In the example the same star has three different sizes.
<svg width="16" height="16" viewBox="0 0 24 24" aria-hidden="true">
<polygon points="12,2 14.9,8.6 22,9.3 16.6,14 18.2,21 12,17.3 5.8,21 7.4,14 2,9.3 9.1,8.6" fill="#facc15" />
</svg>
<svg width="32" height="32" viewBox="0 0 24 24" aria-hidden="true">
<polygon points="12,2 14.9,8.6 22,9.3 16.6,14 18.2,21 12,17.3 5.8,21 7.4,14 2,9.3 9.1,8.6" fill="#facc15" />
</svg>
<svg width="64" height="64" viewBox="0 0 24 24" aria-hidden="true">
<polygon points="12,2 14.9,8.6 22,9.3 16.6,14 18.2,21 12,17.3 5.8,21 7.4,14 2,9.3 9.1,8.6" fill="#facc15" />
</svg>fill / stroke
color, `none`, or `currentColor`The fill and outline color of shapes. When set on the <svg> itself, all shapes inside inherit them. The currentColor value takes the text color of the surroundings, so the icon changes together with it.
<style>
.lesson-time { display: flex; align-items: center; gap: 6px; color: #38bdf8; }
</style>
<p class="lesson-time">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
<circle cx="12" cy="12" r="9" />
<path d="M12 7 v5 l3 3" />
</svg>
Lesson time: 12 minutes
</p>Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Mark graphics that convey something with
role="img"and give them a name with a<title>element or anaria-labelattribute. Hide decorative icons from screen readers witharia-hidden="true". - Draw icons with
fill="currentColor"orstroke="currentColor". They will change color together with the text, including on hover and in dark mode. - SVG files from graphics programs often contain unnecessary data. Before pasting them into a page, run them through an optimizer, for example SVGO.
Related entries
#- <canvas>A canvas for drawing bitmap graphics with JavaScript.
- <img>An image embedded in the page.
- <picture>A container for several versions of an image. The browser picks the one that best fits the screen and format.
- <figure>A self-contained illustration with an optional caption: an image, chart, code snippet, or quote.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.