SVG (Scalable Vector Graphics) describes pictures as shapes instead of pixels: “a circle here, a line there”. Because the browser draws the shapes, an SVG stays perfectly sharp at any size, from a 16-pixel icon to a full-screen banner, and the file is usually tiny. SVG code can be written straight into an HTML page, where CSS can colour and animate it. This lesson covers the basic shapes, the viewBox, icons that follow the text colour, and a simple bar chart.
In this lesson you will learn
- The SVG coordinate system and the
viewBoxattribute - Basic shapes:
rect,circle,ellipse,line,polyline,polygonandtext - Paths and icons that use
currentColor - Styling SVG shapes with CSS
- Making SVGs accessible
You should know HTML elements and attributes and CSS selectors.
Basic Shapes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SVG shapes</title>
<style>
body { font-family: sans-serif; margin: 12px; }
svg { border: 1px dashed #b0bec5; }
</style>
</head>
<body>
<!-- viewBox sets the drawing's own coordinate system: 0,0 is the top-left corner -->
<svg width="420" height="160" viewBox="0 0 420 160">
<rect x="10" y="20" width="100" height="60" rx="8" fill="#42a5f5" />
<circle cx="170" cy="50" r="30" fill="#ef5350" stroke="#b71c1c" stroke-width="4" />
<ellipse cx="260" cy="50" rx="45" ry="25" fill="#ffca28" />
<line x1="320" y1="20" x2="410" y2="80" stroke="#2e7d32" stroke-width="5" stroke-linecap="round" />
<polyline points="10,140 60,110 110,130 160,100 210,120" fill="none" stroke="#6a1b9a" stroke-width="3" />
<polygon points="270,95 300,150 240,150" fill="#26a69a" />
<text x="320" y="135" font-size="18" fill="#37474f">SVG text</text>
</svg>
</body>
</html>
Result
widthandheightset the size on the page.viewBox="0 0 420 160"sets the drawing’s own coordinates: x runs from 0 to 420 and y from 0 to 160. If you change the width, the drawing scales to match.- The origin (0, 0) is the top-left corner, and y grows downwards, unlike graphs in maths class.
rectusesx,y,widthandheight;rxrounds its corners.circleuses a centre (cx,cy) and radiusr, andellipsehas two radii.linejoins two points.polylinejoins a list of points into an open line, andpolygoncloses the shape.fillpaints the inside andstrokethe outline. For open lines, setfill="none"so the area under the line is not filled.textdraws real, selectable text at the given position.
Icons, Paths and CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SVG icons and a chart</title>
<style>
body { font-family: sans-serif; margin: 12px; }
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border: 0;
border-radius: 6px; font-size: 15px; background: #1565c0; color: white; margin-right: 6px; }
.btn.alt { background: #e3f2fd; color: #0d47a1; }
.icon { width: 18px; height: 18px; }
.chart rect { fill: #66bb6a; transition: fill .2s; }
.chart rect:hover { fill: #2e7d32; } /* SVG shapes can be styled with CSS */
.chart text { font-size: 12px; fill: #37474f; }
</style>
</head>
<body>
<!-- currentColor makes the icon use the button's text colour -->
<button class="btn">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 12l5 5L20 7" fill="none" stroke="currentColor" stroke-width="3" />
</svg>
Save
</button>
<button class="btn alt">
<svg class="icon" viewBox="0 0 24 24" aria-hidden="true">
<path d="M5 12l5 5L20 7" fill="none" stroke="currentColor" stroke-width="3" />
</svg>
Save draft
</button>
<svg class="chart" width="300" height="140" viewBox="0 0 300 140" role="img" aria-label="Marks: Rina 92, Arif 70, Tania 85">
<rect x="20" y="20" width="60" height="92" />
<rect x="110" y="42" width="60" height="70" />
<rect x="200" y="27" width="60" height="85" />
<text x="50" y="130" text-anchor="middle">Rina</text>
<text x="140" y="130" text-anchor="middle">Arif</text>
<text x="230" y="130" text-anchor="middle">Tania</text>
</svg>
</body>
</html>
Result
<path d="...">can draw any shape.M5 12moves the pen to (5, 12), andl5 5draws a line 5 units right and 5 down from there. Icon libraries and design tools generate these paths for you.stroke="currentColor"makes the icon use the CSScolorof its parent. The same icon is white in the blue button and dark blue in the light button, with no change to the SVG.- Decorative icons next to a text label get
aria-hidden="true"so that screen readers do not announce them. - The chart is made of three
rectbars. CSS rules such as.chart rect:hoverwork on SVG shapes, andfillcan be set from CSS and transitioned. - A meaningful graphic gets
role="img"and anaria-labelthat describes it, so screen-reader users get the same information.
Ways to Put SVG on a Page
| Method | Style with page CSS? | Best for |
|---|---|---|
Inline <svg> in the HTML | Yes | Icons, charts, anything you want to colour or animate |
<img src="logo.svg" alt="..."> | No | Logos and illustrations that never change colour |
CSS background-image: url(shape.svg) | No | Decorative patterns |
Common Mistakes
- Leaving out
viewBox, so the drawing does not scale when you change its size. - Forgetting
fill="none"on lines and outline icons, which fills them with black. - Pasting huge SVGs from design tools without removing editor data. Tools such as SVGO can shrink them.
- Mixing up
widthandviewBox: width is the displayed size, viewBox is the drawing’s coordinate range.
Practice Exercises
Exercise 1. Draw a 100 × 100 SVG with a green circle filling most of it and the letter “A” in white in the middle.
Show answer
<svg width="100" height="100" viewBox="0 0 100 100">
<circle cx="50" cy="50" r="45" fill="#2e7d32" />
<text x="50" y="62" font-size="36" fill="white" text-anchor="middle">A</text>
</svg>
text-anchor="middle" centers the text horizontally on x = 50.
Exercise 2. Make the check icon from the example red inside a .error message.
Show answer
.error { color: #c62828; }
Because the icon uses currentColor, changing the text colour is enough.
Frequently Asked Questions
When should I use SVG instead of PNG or JPEG?
Use SVG for logos, icons, diagrams and charts: anything made of shapes and flat colours. Use JPEG, PNG or WebP for photos, which would be enormous as vector shapes.
Can SVG be animated?
Yes. Inline SVG shapes accept CSS transitions and animations, such as changing fill on hover or rotating an icon with transform. See CSS transitions and animations.
Summary
- SVG draws shapes that stay sharp at any size.
viewBoxsets the coordinate system; y grows downwards.fillpaints inside,strokepaints outlines;currentColorfollows the text colour.- Inline SVG can be styled with CSS; label meaningful graphics for screen readers.
Next: Styling HTML Forms with CSS.
