SVG (Scalable Vector Graphics) describes pictures as shapes, such as circles, lines and paths, instead of a grid of pixels. Because the shapes are mathematical, an SVG stays perfectly sharp at any size, from a tiny icon to a full-screen illustration, and the file is often far smaller than an equivalent PNG.
SVG is written in a markup language very like HTML, and you can put it directly inside your HTML as an <svg> element. Each shape is then a real element on the page: you can style it with CSS, change it with JavaScript, animate it and make it accessible to screen readers. That's why most modern icon sets, logos and simple charts on the web are SVG.
In this lesson you'll draw basic shapes, understand the all-important viewBox, write simple paths, colour icons with currentColor so they match the text around them, and make SVG graphics accessible. You'll also learn when to use an SVG file with <img> instead of inline code.
Shapes and the coordinate system
<svg width="200" height="100" viewBox="0 0 200 100">
<rect x="10" y="10" width="80" height="80" rx="10" fill="tomato" />
<circle cx="150" cy="50" r="40" fill="gold" stroke="black" stroke-width="3" />
</svg>| Element | Draws | Key attributes |
|---|---|---|
<rect> | Rectangle | x, y, width, height, rx |
<circle> | Circle | cx, cy, r |
<ellipse> | Oval | cx, cy, rx, ry |
<line> | Straight line | x1, y1, x2, y2 |
<polygon> | Closed shape | points="x,y x,y ..." |
<path> | Any shape | d (drawing commands) |
<text> | Text | x, y |
Like canvas, (0, 0) is the top-left. fill colours the inside and stroke the outline.
viewBox and scaling
The viewBox="minX minY width height" attribute defines the SVG's internal coordinate system. The shapes are drawn in those units, then the whole picture is scaled to whatever size the <svg> is displayed at.
- With
viewBox="0 0 24 24", an icon designed on a 24-unit grid can be shown at 16px, 24px or 200px, always sharp. - Remove fixed
widthandheightand setwidth: 100%in CSS to make an SVG fully responsive. preserveAspectRatiocontrols how the picture fits when the box has a different shape; the default keeps it centred without distortion.
Paths, styling and accessibility
- A path's
dattribute is a list of commands:Mmove to,Lline to,HandVhorizontal and vertical lines,Ccurves,Aarcs,Zclose. Design tools export paths for you; you rarely write long ones by hand. - CSS can style SVG:
fill,stroke,stroke-width,opacity, transitions and:hoverall work on inline SVG. fill="currentColor"makes an icon use the text colour of its parent, so icons match buttons and links automatically.- Accessibility: a meaningful graphic needs
role="img"and a<title>(oraria-label). A purely decorative icon next to visible text should be hidden witharia-hidden="true".
HTML SVG Syntax
SVG syntax
<svg viewBox="0 0 100 100" width="100" height="100" role="img" aria-labelledby="t">
<title id="t">Green circle</title>
<circle cx="50" cy="50" r="40" fill="green" />
</svg>
<svg viewBox="0 0 24 24" width="24" height="24" aria-hidden="true">
<path d="M5 12h14M12 5l7 7-7 7" fill="none" stroke="currentColor" stroke-width="2" />
</svg>
<img src="logo.svg" alt="Company logo" width="120" height="40">HTML SVG Summary
- SVG draws scalable vector shapes that stay sharp at any size.
- Basic elements: rect, circle, ellipse, line, polygon, path and text.
viewBoxdefines internal units so the graphic scales to any display size.- Inline SVG can be styled with CSS;
currentColormatches the surrounding text. - Give meaningful SVGs
role="img"and a title; hide decorative ones witharia-hidden.
HTML SVG Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Basic shapes
<svg viewBox="0 0 320 140" width="320" height="140" role="img" aria-labelledby="shapes-title">
<title id="shapes-title">A red square, a yellow circle, a blue triangle and a line</title>
<rect x="10" y="20" width="90" height="90" rx="12" fill="#ef4444" />
<circle cx="160" cy="65" r="45" fill="#facc15" stroke="#a16207" stroke-width="4" />
<polygon points="230,110 275,20 320,110" fill="#3b82f6" />
<line x1="10" y1="130" x2="310" y2="130" stroke="#111827" stroke-width="3" stroke-linecap="round" />
</svg>Line-by-line explanation
- Line 1The viewBox defines a 320 by 140 drawing area.
- Line 2The title gives the graphic an accessible name.
- Line 3
rxrounds the rectangle's corners. - Line 4A filled circle with an outline.
- Line 5A triangle from three points.
- Line 6A line needs a stroke colour to be visible.
Icons that follow the text colour
<style>
.btn { display: inline-flex; align-items: center; gap: 8px; padding: 10px 16px; border: 0; border-radius: 8px; background: #1d4ed8; color: white; font-size: 16px; }
.btn:hover { background: #1e40af; }
.btn svg { width: 20px; height: 20px; }
.link { color: #15803d; display: inline-flex; align-items: center; gap: 4px; }
</style>
<button class="btn" type="button">
<svg viewBox="0 0 24 24" aria-hidden="true"><path d="M5 12h14M12 5l7 7-7 7" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"/></svg>
Continue
</button>
<a class="link" href="#">
<svg viewBox="0 0 24 24" width="18" height="18" aria-hidden="true"><path d="M12 2l3 7h7l-5.5 4 2 7-6.5-4.5L5.5 20l2-7L2 9h7z" fill="currentColor"/></svg>
Add to favourites
</a>Line-by-line explanation
- Line 4CSS sets the icon size, so the same icon can be reused at any size.
- Line 8
currentColormakes the arrow white, like the button text. - Line 8The icon is decorative because the button has visible text, so it's hidden from screen readers.
- Line 12The same technique makes the star green, matching the link.
A responsive SVG chart
<style>.chart { width: 100%; max-width: 420px; height: auto; } .bar { fill: #16a34a; } .bar:hover { fill: #15803d; }</style>
<svg class="chart" viewBox="0 0 220 120" role="img" aria-labelledby="ct cd">
<title id="ct">Weekly visitors</title>
<desc id="cd">Monday 40, Tuesday 70, Wednesday 55, Thursday 90, Friday 65.</desc>
<rect class="bar" x="10" y="60" width="30" height="40" />
<rect class="bar" x="52" y="30" width="30" height="70" />
<rect class="bar" x="94" y="45" width="30" height="55" />
<rect class="bar" x="136" y="10" width="30" height="90" />
<rect class="bar" x="178" y="35" width="30" height="65" />
<line x1="5" y1="100" x2="215" y2="100" stroke="#6b7280" />
<text x="10" y="115" font-size="9">Mon</text>
<text x="178" y="115" font-size="9">Fri</text>
</svg>Line-by-line explanation
- Line 1The SVG fills its container up to 420px and keeps its shape.
- Line 1CSS hover styles work on each bar.
- Lines 3 to 4A title and description make the chart's data available to screen readers.
- Lines 5 to 9Each bar's height is the value; y starts lower for shorter bars.
Common HTML SVG Mistakes
No viewBox on a scaled SVG
<svg width="48" height="48"><circle cx="12" cy="12" r="10" /></svg> <!-- drawn for 24 units, but not scaled --><svg viewBox="0 0 24 24" width="48" height="48"><circle cx="12" cy="12" r="10" /></svg>Without a viewBox, the circle stays small in the corner instead of filling the larger box.
Hard-coded icon colours
<svg viewBox="0 0 24 24"><path d="..." fill="#000000"/></svg><svg viewBox="0 0 24 24"><path d="..." fill="currentColor"/></svg>A fixed black icon disappears in dark mode; currentColor follows the text.
A meaningful SVG with no name
<svg viewBox="0 0 24 24"><path d="..."/></svg> <!-- a "warning" icon with no text nearby --><svg viewBox="0 0 24 24" role="img" aria-label="Warning"><path d="..."/></svg>HTML SVG in Real Projects
SVG everywhere
Icon libraries like Lucide, Heroicons and Font Awesome provide SVG icons, company logos are delivered as SVG files, and chart libraries such as D3 draw SVG charts. Codevra's own course logos and icons are inline SVG, coloured with CSS so they work in both light and dark mode.
HTML SVG Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Complete the circle's radius attribute.
<svg viewBox="0 0 100 100"><circle cx="50" cy="50" ="40" fill="teal" /></svg>
r is the radius; cx and cy are the centre.What's the main advantage of SVG over PNG for icons?
What does viewBox="0 0 24 24" define?
How do you make an icon use the same colour as the text around it?
The icon is decorative (the button has text) but screen readers announce it, and its colour is fixed black. Hide it from assistive technology and make it follow the text colour.
<button type="button" style="color: white; background: #15803d; padding: 8px 12px; border: 0">
<svg viewBox="0 0 24 24" width="18" height="18"><path d="M4 12l5 5L20 6" fill="none" stroke="#000000" stroke-width="3"/></svg>
Done
</button>- The SVG is hidden from screen readers
- The path uses currentColor
HTML SVG Quiz
5 questions to check what you learned.
What does SVG stand for?
- Scalable Vector Graphics
- Simple Vector Graphics
- Styled Visual Graphics
- Static Vector Grid
Which attribute colours the inside of an SVG shape?
- color
- background
- fill
- stroke
How should a decorative SVG icon next to visible text be marked?
- role="img"
- aria-hidden="true"
- alt=""
- title=""
When is an SVG file in an img tag better than inline SVG?
- When you need CSS hover effects on parts of it
- For icons that change colour
- Never
- For large, unchanging graphics that benefit from browser caching
What does the path command M do?
- Moves the pen to a point without drawing
- Draws a curve
- Closes the path
- Mirrors the shape
HTML SVG Challenge
A traffic light
Draw an SVG traffic light:
- An
<svg>with aviewBoxandrole="img"plus anaria-label - A dark
<rect>for the body - Three
<circle>elements filled red, orange and green
- SVG has a viewBox, role and label
- Has a rect body
- Three circles
- Red, orange and green fills