The SVG element embeds scalable vector graphics with size variants, color options, and built-in animations for icons and illustrations.
Description
The <svg> element embeds Scalable Vector Graphics directly in HTML. SVG is resolution-independent, accessible, and styleable with CSS. Vanilla Breeze provides size classes, color variants, and animation utilities for inline SVG.
Unlike raster images, SVG scales to any size without quality loss, making it ideal for icons, logos, and illustrations.
When to Use
- Icons - UI icons that need to scale and change color
- Logos - Brand marks that must be crisp at any size
- Illustrations - Vector artwork and diagrams
- Charts - Accessible data visualizations
- Animations - Animated graphics via CSS or SMIL
When to Use Other Elements
- Use
<img> for photographs and complex raster images
- Use
<picture> for responsive raster images with multiple sources
- Use
<canvas> for dynamic, pixel-based graphics
- Use
<icon-wc> for the icon component with sprite support
Size Variants
Predefined size classes for consistent icon sizing across your application.
<svg class="m currentcolor" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> <circle cx="12" cy="12" r="10" /></svg>
| Class |
Size |
Use Case |
.xs |
0.75rem (12px) |
Inline indicators, badges |
.s |
1rem (16px) |
Small icons in text |
.m |
1.5rem (24px) |
Default icon size, buttons |
.l |
2rem (32px) |
Featured icons, nav items |
.xl |
3rem (48px) |
Hero icons, empty states |
.xxl |
4rem (64px) |
Large illustrations |
Color Variants
Colour classes set the icon's color and, for fill-style icons, fill: currentColor. They work for both icon styles:
- Fill-style icons (no
fill attribute on the <svg>): the shapes are filled with the colour.
- Stroke-style icons (
fill="none" stroke="currentColor" on the <svg>, the Lucide style VB uses): the class leaves fill="none" alone, so the icon stays an outline, and the stroke follows the colour.
<svg class="m currentcolor" viewBox="0 0 24 24">...</svg> <svg class="m interactive" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">...</svg> <svg class="m muted" viewBox="0 0 24 24">...</svg>
currentColor Pattern
Use currentColor in your SVG to inherit the CSS color property. Icons then match the surrounding text, change with the theme and dark mode, and follow system colours in forced-colors mode.
<svg viewBox="0 0 24 24" class="m" fill="none" stroke="currentColor"> <path d="..." /></svg> <svg viewBox="0 0 24 24" class="m currentcolor"> <path d="..." /></svg>
Dark Mode and Forced Colors
An SVG with no fill and no class is painted black, which disappears on a dark theme. Give every icon a colour source: currentColor (directly or through .currentcolor, .interactive or .muted), or a token such as var(--color-text). Hardcoded hex fills do not adapt to themes or dark mode.
In forced-colors mode (Windows High Contrast) the browser replaces text colours with system colours. Icons drawn with currentColor follow along; fills from colour tokens or literal colours may not. Draw any icon that carries meaning with currentColor.
Animations
Built-in animation classes for loading indicators and attention-grabbing icons.
<svg class="m spin" viewBox="0 0 24 24" aria-hidden="true"> <circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="2" stroke-dasharray="30 60" /></svg> <svg class="m pulse currentcolor" viewBox="0 0 24 24" aria-hidden="true"> <circle cx="12" cy="12" r="10" /></svg>
Respecting Motion Preferences
Built in: .spin and .pulse stop when the OS asks for reduced motion (prefers-reduced-motion: reduce) or when the page sets VB's own switch, data-motion-reduced on <html> (written by the settings panel). You do not need to add the media query yourself. A stopped spinner no longer signals activity on its own, so pair it with visible or announced text such as "Loading…".
Responsive SVG
.full makes an SVG fill its container's width; the viewBox sets the aspect ratio, so the height follows.
<figure> <svg class="full" role="img" aria-labelledby="flow-title" viewBox="0 0 400 100"> <title id="flow-title">Three-step flow: draft, review, publish</title> </svg> <figcaption>The diagram fills the container width.</figcaption></figure>
An SVG with no class at all is capped at its container's width (max-inline-size: 100%), with the height following the viewBox.
Icons in Context
An inline icon in running text, icons in buttons (with and without visible text), and an icon grid.
<button type="button"> <svg class="s currentcolor" aria-hidden="true" viewBox="0 0 24 24">...</svg> Favourite</button> <button type="button" aria-label="Close"> <svg class="s" aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">...</svg></button>
Security: Untrusted SVG
Inline SVG is live markup. If you insert SVG from user content, an API or a file upload with innerHTML, any <script>, on* event attribute or javascript: link inside it runs on your page.
- Show untrusted SVG with
<img src="file.svg">. An SVG loaded as an image cannot run scripts or reach the page.
- Only inline it when you need CSS or
currentColor control, and sanitize it first. VB's <icon-wc> already runs every fetched icon through its sanitizeSVG helper, which strips scripts, event handlers and script URLs.
<img src="/uploads/user-logo.svg" alt="Acme logo" />
Accessibility
Decorative Icons
Icons that don't convey information should be hidden from assistive technology.
<svg aria-hidden="true" class="m currentcolor" viewBox="0 0 24 24"> </svg>
Informative Icons
Icons that convey meaning need accessible names.
<svg role="img" aria-labelledby="warn-title" class="m currentcolor" viewBox="0 0 24 24"> <title id="warn-title">Warning</title> <path d="..." /></svg> <svg aria-label="Error" role="img" class="m currentcolor" viewBox="0 0 24 24"> <path d="..." /></svg>
Interactive Icons
Wrap icons in buttons or links with proper labels.
<button type="button" aria-label="Close dialog"> <svg aria-hidden="true" class="m" viewBox="0 0 24 24"> <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z" /> </svg></button> <a href="/settings"> <svg aria-hidden="true" class="s" viewBox="0 0 24 24">...</svg> Settings</a>
Best Practices
- Use
aria-hidden="true" on decorative icons
- Give meaningful icons
role="img" and a name: aria-labelledby pointing at a <title>, or aria-label
- Draw meaningful icons with
currentColor so they survive dark mode and forced-colors mode
- Don't rely on icons alone - pair with visible text when possible
- Ensure sufficient color contrast (3:1 against the background for icons that carry meaning)
- Spinners and pulses stop under reduced motion; never let them be the only sign of a state
- Test with screen readers
Related
<icon-wc> - VB's icon component wraps SVG with sprite support and consistent sizing
<img> - For raster images (photographs, screenshots) or external SVG files via <img src="icon.svg">
<picture> - For responsive raster images with multiple sources and art direction
<canvas> - For dynamic pixel-based graphics
<figure> - Wrap SVG diagrams with captions
CSS Reference
Styles defined in /src/native-elements/svg/styles.css
| Selector |
Properties |
svg |
display: inline-block; vertical-align: middle; overflow: visible; |
svg:not([class]) |
max-inline-size: 100%; block-size: auto; |
svg.xs |
inline-size: 0.75rem; block-size: 0.75rem; |
svg.s |
inline-size: 1rem; block-size: 1rem; |
svg.m |
inline-size: 1.5rem; block-size: 1.5rem; |
svg.l |
inline-size: 2rem; block-size: 2rem; |
svg.xl |
inline-size: 3rem; block-size: 3rem; |
svg.xxl |
inline-size: 4rem; block-size: 4rem; |
svg.currentcolor:not([fill="none"]) |
fill: currentColor; |
svg.interactive |
color: var(--color-interactive); |
svg.muted |
color: var(--color-text-muted); |
svg:is(.interactive, .muted):not([fill="none"]) |
fill: currentColor; |
svg.full |
inline-size: 100%; block-size: auto; |
svg.spin |
animation: svg-spin 1s linear infinite; |
svg.pulse |
animation: svg-pulse 2s ease-in-out infinite; |
svg:is(.spin, .pulse) in @media (prefers-reduced-motion: reduce) |
animation: none; |
:root[data-motion-reduced] svg:is(.spin, .pulse) |
animation: none; |