On this page12 sections
  1. Learning Objectives
  2. 1. Basic Shapes
  3. 2. Lines and Polygons
  4. 3. Paths: Any Shape
  5. 4. Text and Gradients
  6. 5. viewBox: Scaling
  7. 6. Styling SVG With CSS
  8. 7. SVG or Canvas?
  9. Common Beginner Mistakes
  10. Practice Questions
  11. Practice Answers
  12. Key Takeaways

Lecture 040: SVG

Category7: Interactive Elements, Graphics & Media
Lecture040 of 048
Estimated video length12 min
TargetHTML5 · any modern browser
LevelBeginner

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 fill and stroke, including from CSS
  • Compare SVG with Canvas

1. Basic Shapes

Example

html
<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>
ShapeKey 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

html
<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:

CommandMeaning
M x yMove to (start)
L x yLine to
Q cx cy x yCurve
ZClose the shape

Example: a heart:

html
<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

html
<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:

html
<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:

html
<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?

SVGCanvas
Made ofShapes (elements in the page)Pixels
Sharp when zoomedYesNo
CSS and click events per shapeYesNo (you calculate it yourself)
Best forIcons, logos, diagrams, simple chartsGames, 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

  1. What does SVG stand for?
  2. Draw a blue circle with radius 30 centred at (50, 50).
  3. What's the difference between polygon and polyline?
  4. Which path command starts a new point without drawing?
  5. Why add a viewBox?
  6. Would you choose SVG or Canvas for a company logo? Why?

Practice Answers

  1. Scalable Vector Graphics.
  2. <svg width="100" height="100"><circle cx="50" cy="50" r="30" fill="blue" /></svg>
  3. A polygon closes its shape automatically; a polyline doesn't.
  4. M (move to).
  5. It defines the drawing's coordinate system, so the SVG scales cleanly to any size.
  6. 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, path and text are the building blocks.
  • fill, stroke and CSS style each shape; viewBox makes it scale.
  • SVG for icons and diagrams; Canvas for games and pixel effects.