HTML SVG
SVG (Scalable Vector Graphics) is a way to describe pictures with markup instead of pixels. Rather than storing the colour of every dot, an SVG file says things like "draw a circle here, with this radius and this colour". Because the browser redraws the shapes at whatever size is needed, SVG graphics stay perfectly sharp on a phone, a 4K monitor or a printed page.
You can put SVG directly inside an HTML document with the <svg> element. Each shape inside it is an element of its own, so you can style it with CSS, give it an id, and even change it with JavaScript.
Your first SVG
<!DOCTYPE html>
<html>
<body>
<svg width="220" height="120" style="border:1px solid #ccc">
<circle cx="60" cy="60" r="40" fill="tomato" />
<rect x="120" y="20" width="80" height="80" rx="12" fill="steelblue" />
</svg>
</body>
</html>The width and height attributes give the drawing area its size. The coordinate system starts at the top-left corner (0, 0); x grows to the right and y grows downwards, which surprises many people who remember maths class.
Basic shapes
| Element | What it draws | Key attributes |
|---|---|---|
<rect> |
Rectangle (optionally rounded) | x, y, width, height, rx, ry |
<circle> |
Circle | cx, cy (centre), r (radius) |
<ellipse> |
Oval | cx, cy, rx, ry |
<line> |
Straight line | x1, y1, x2, y2 |
<polyline> |
Connected line segments | points="x,y x,y …" |
<polygon> |
Closed shape from points | points |
<path> |
Anything, using drawing commands | d |
<text> |
Text at a position | x, y |
Fill, stroke and text
Every shape can have a fill (inside colour) and a stroke (outline). stroke-width controls the outline thickness.
<!DOCTYPE html>
<html>
<body>
<svg width="300" height="160">
<polygon points="150,10 190,90 110,90" fill="gold" stroke="darkorange" stroke-width="4" />
<line x1="20" y1="120" x2="280" y2="120" stroke="gray" stroke-width="2" stroke-dasharray="6 4" />
<ellipse cx="60" cy="60" rx="45" ry="25" fill="none" stroke="purple" stroke-width="3" />
<text x="150" y="150" text-anchor="middle" font-family="sans-serif" font-size="18" fill="#333">
Shapes are elements!
</text>
</svg>
</body>
</html>Tip:
fill="none"makes a shape hollow so only its stroke shows.
The viewBox attribute
viewBox="minX minY width height" defines the coordinate system inside the SVG independently of its displayed size. Draw once in a 100×100 grid, then scale the SVG to any size — the drawing scales with it.
<!DOCTYPE html>
<html>
<body>
<p>The same drawing at three sizes:</p>
<svg viewBox="0 0 100 100" width="40">
<circle cx="50" cy="50" r="45" fill="#4caf50" />
<path d="M30 52 L45 67 L72 36" fill="none" stroke="white" stroke-width="10" stroke-linecap="round" />
</svg>
<svg viewBox="0 0 100 100" width="80">
<circle cx="50" cy="50" r="45" fill="#4caf50" />
<path d="M30 52 L45 67 L72 36" fill="none" stroke="white" stroke-width="10" stroke-linecap="round" />
</svg>
<svg viewBox="0 0 100 100" width="160">
<circle cx="50" cy="50" r="45" fill="#4caf50" />
<path d="M30 52 L45 67 L72 36" fill="none" stroke="white" stroke-width="10" stroke-linecap="round" />
</svg>
</body>
</html>The <path> above uses two commands: M (move to a point without drawing) and L (draw a line to a point). Other common commands are H/V (horizontal/vertical line), C (cubic curve), A (arc) and Z (close the shape).
Styling and interacting with SVG
Since SVG shapes live in the page's DOM, CSS and JavaScript work on them just like on a <div>. Hover over the bars, then click one.
<!DOCTYPE html>
<html>
<head>
<style>
.bar { fill: #90caf9; transition: fill .2s; cursor: pointer; }
.bar:hover { fill: #1e88e5; }
</style>
</head>
<body>
<svg width="260" height="130">
<rect class="bar" x="10" y="70" width="40" height="50" />
<rect class="bar" x="70" y="30" width="40" height="90" />
<rect class="bar" x="130" y="50" width="40" height="70" />
<rect class="bar" x="190" y="10" width="40" height="110" />
</svg>
<p id="out">Click a bar.</p>
<script>
document.querySelectorAll('.bar').forEach(function (bar, i) {
bar.addEventListener('click', function () {
document.getElementById('out').textContent =
'Bar ' + (i + 1) + ' is ' + bar.getAttribute('height') + ' units tall.';
});
});
</script>
</body>
</html>SVG vs Canvas
| SVG | Canvas | |
|---|---|---|
| Model | Shapes are DOM elements | A bitmap you paint pixels on |
| Scaling | Sharp at any size | Gets blurry when enlarged |
| Events | Each shape can have its own handlers | You must work out what was clicked |
| Best for | Icons, logos, charts, diagrams | Games, many moving objects, image filters |
Note: You can also use an SVG file as a normal image:
<img src="logo.svg" alt="Logo">. That is convenient, but shapes inside an<img>cannot be styled by your page's CSS or scripted.
Accessibility
Give meaningful SVGs a text alternative: add role="img" and a <title> as the first child, or an aria-label. Purely decorative graphics can use aria-hidden="true" so screen readers skip them.
Test yourself
In SVG, where is the point (0, 0)?
- Bottom-left corner
- Centre of the drawing
- Top-left corner
- Top-right corner
Which attributes position a <circle>?
xandycx,cyandrleftandtopx1andy1
What does viewBox let you do?
- Add a border
- Define an internal coordinate system so the drawing scales to any size
- Hide the SVG
- Load an external file