Canvas & SVG

HTML has two ways to draw graphics. SVG describes shapes as elements — crisp at any size, styleable with CSS and accessible. Canvas is a pixel surface you paint on with JavaScript — fast for games, charts with thousands of points and image effects.

Inline SVG

Try it yourself
Result

Every shape is an element: <rect>, <circle>, <ellipse>, <line>, <polyline>, <polygon>, <path> and <text>. The viewBox defines the internal coordinate system, so the drawing scales cleanly to any displayed size.

Paths

Try it yourself
Result

The d attribute is a mini-language: M moves the pen, L draws a line, Q and C draw curves, Z closes the shape. Design tools export complex icons and illustrations as paths.

SVG works with CSS and links

Try it yourself
Result

Inline SVG can be styled, animated and made interactive like HTML. Using fill: currentColor makes an icon take on the surrounding text colour automatically. Decorative icons get aria-hidden="true"; meaningful graphics get role="img" and a <title>.

Three ways to use SVG

MethodProsCons
Inline <svg>Full CSS and script controlRepeated markup, not cached
<img src="logo.svg">Cached, simple, needs alt textCan't style the inside with page CSS
CSS background-imageGood for decorationNo alt text, not for content

Canvas

Try it yourself
Result

A <canvas> is just a blank rectangle of pixels; everything is drawn by script through its 2D context (or WebGL/WebGPU for 3D). Once drawn, the shapes don't exist as objects — to change something, you redraw.

Accessibility: canvas content is invisible to screen readers. Give the canvas a text alternative (role="img" with aria-label), or put fallback content — like a data table — between the <canvas> tags.

Animation on canvas

Try it yourself
Result

SVG or canvas?

Choose SVG for…Choose canvas for…
Logos, icons, illustrationsGames and fast animation
Charts with a modest number of elementsCharts or visualisations with thousands of points
Anything that needs to scale sharply, be styled, or be accessibleImage editing, pixel effects, video processing
Interactive diagrams with clickable partsDrawing apps

Charting libraries use both: SVG-based ones (like D3) are easy to style and inspect, canvas-based ones (like Chart.js) handle larger datasets.

Try it yourself

Try it yourself
Result