On this page12 sections
Lecture 040: SVG
| Category | 7: Interactive Elements, Graphics & Media |
| Lecture | 040 of 048 |
| Estimated video length | 12 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
SVG (Scalable Vector Graphics) describes pictures with shapes instead of pixels, so they stay perfectly sharp at any size. Logos, icons and simple charts are often SVG, and you can write SVG directly inside HTML.
Learning Objectives
- Write inline SVG with
<svg> - Draw rectangles, circles, ellipses, lines, polygons, paths and text
- Style shapes with
fillandstroke, including from CSS - Compare SVG with Canvas
1. Basic Shapes
Example
<svg width="320" height="120" style="border: 1px solid #dee2e6;">
<rect x="10" y="10" width="90" height="90" rx="12" fill="#4c6ef5" />
<circle cx="160" cy="55" r="45" fill="#ff922b" stroke="#c2255c" stroke-width="5" />
<ellipse cx="265" cy="55" rx="45" ry="25" fill="#51cf66" />
</svg>| Shape | Key attributes |
|---|---|
<rect> | x, y, width, height, rx (rounded corners) |
<circle> | cx, cy (centre), r (radius) |
<ellipse> | cx, cy, rx, ry |
2. Lines and Polygons
Example
<svg width="320" height="130">
<line x1="10" y1="120" x2="110" y2="10" stroke="#495057" stroke-width="4" stroke-linecap="round" />
<polygon points="160,10 200,120 120,120" fill="#ffd43b" stroke="#f08c00" stroke-width="3" />
<polyline points="220,110 245,60 270,90 300,20" fill="none" stroke="#1c7ed6" stroke-width="4" />
</svg>polygon closes itself; polyline stays open.
3. Paths: Any Shape
A <path>'s d attribute is a list of drawing commands:
| Command | Meaning |
|---|---|
M x y | Move to (start) |
L x y | Line to |
Q cx cy x y | Curve |
Z | Close the shape |
Example: a heart:
<svg width="120" height="110" viewBox="0 0 24 22">
<path d="M12 21 L3 12 Q0 9 1.5 5.5 Q4 1 8.5 2.5 Q11 3.5 12 6 Q13 3.5 15.5 2.5 Q20 1 22.5 5.5 Q24 9 21 12 Z" fill="#e03131" />
</svg>4. Text and Gradients
Example
<svg width="320" height="90">
<defs>
<linearGradient id="sunset" x1="0" x2="1">
<stop offset="0" stop-color="#f76707" />
<stop offset="1" stop-color="#ae3ec9" />
</linearGradient>
</defs>
<rect width="320" height="90" rx="12" fill="url(#sunset)" />
<text x="160" y="55" font-size="28" font-family="Arial" fill="white" text-anchor="middle">Hello SVG</text>
</svg>5. viewBox: Scaling
viewBox="0 0 100 100" sets the drawing's own coordinate system. Then the SVG can be any
size and scales perfectly.
Example: the same icon at three sizes:
<svg width="24" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="#12b886" /></svg>
<svg width="64" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="#12b886" /></svg>
<svg width="160" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" fill="#12b886" /></svg>6. Styling SVG With CSS
Example: hover over the stars:
<style>
.star { fill: #dee2e6; transition: fill 0.2s; cursor: pointer; }
.star:hover { fill: #fab005; }
</style>
<svg width="200" height="40" viewBox="0 0 120 24">
<polygon class="star" points="12,1 15,9 23,9 17,14 19,22 12,17 5,22 7,14 1,9 9,9" />
<polygon class="star" points="12,1 15,9 23,9 17,14 19,22 12,17 5,22 7,14 1,9 9,9" transform="translate(24)" />
<polygon class="star" points="12,1 15,9 23,9 17,14 19,22 12,17 5,22 7,14 1,9 9,9" transform="translate(48)" />
</svg>7. SVG or Canvas?
| SVG | Canvas | |
|---|---|---|
| Made of | Shapes (elements in the page) | Pixels |
| Sharp when zoomed | Yes | No |
| CSS and click events per shape | Yes | No (you calculate it yourself) |
| Best for | Icons, logos, diagrams, simple charts | Games, photo effects, thousands of moving objects |
Common Beginner Mistakes
1. Forgetting fill="none" on lines and polylines. They get filled black.
2. No viewBox. The SVG can't scale cleanly.
3. Huge exported SVGs. Design tools add a lot of extra code. Tools like SVGOMG shrink them.
4. Missing text alternative. Add role="img" and a <title> inside meaningful SVGs.
Practice Questions
- What does SVG stand for?
- Draw a blue circle with radius 30 centred at (50, 50).
- What's the difference between
polygonandpolyline? - Which path command starts a new point without drawing?
- Why add a
viewBox? - Would you choose SVG or Canvas for a company logo? Why?
Practice Answers
- Scalable Vector Graphics.
<svg width="100" height="100"><circle cx="50" cy="50" r="30" fill="blue" /></svg>- A polygon closes its shape automatically; a polyline doesn't.
M(move to).- It defines the drawing's coordinate system, so the SVG scales cleanly to any size.
- SVG: it stays sharp at every size and can be styled with CSS.
Key Takeaways
- SVG draws with shapes, so it's sharp at any size.
rect,circle,ellipse,line,polygon,polyline,pathandtextare the building blocks.fill,strokeand CSS style each shape;viewBoxmakes it scale.- SVG for icons and diagrams; Canvas for games and pixel effects.