SVG
Chapter 2 ended by contrasting canvas with SVG. This chapter delivers on that promise — SVG embedded directly in HTML, where every shape is a real DOM element you can select, style, and animate exactly like any other HTML element covered throughout this entire series.
Inline SVG — Genuinely Part of the Document
Unlike canvas's pixel buffer, every <rect>, <circle>, and <text> here is a genuine, individually inspectable element in the DOM — visible and selectable in DevTools exactly like an ordinary <div> or <p>.
viewBox — The Key to Genuine Scalability
viewBox="0 0 200 100" defines the internal coordinate system (here, 200×100 units) independent of the actual rendered width/height — this is what lets an SVG scale to any size with zero blurring or pixelation, unlike a raster image or canvas content.
Styling SVG with CSS — Exactly Like Any Other Element
Note fill as the CSS property controlling colour, not color or background — SVG has its own specific set of presentation properties (fill, stroke, stroke-width) that CSS can target directly, alongside the standard properties already familiar from the CSS courses.
Manipulating SVG with JavaScript
This per-element interactivity — a click handler on one specific shape, independent of every other shape on the page — is exactly what canvas content cannot offer at all, since canvas pixels have no individual identity once drawn.
SVG <img> vs Inline SVG
<img src="icon.svg"> is simpler and perfectly adequate. For an icon that needs a hover colour change, an animated logo, or any shape-level interactivity, inline SVG is the only option that actually allows it.
Common SVG Elements Beyond the Basics
- <path> — the most powerful and common SVG element, drawing arbitrary curves and lines via a compact "d" attribute path-data string (the same underlying mechanism used by icon libraries and the kanji stroke animations elsewhere on osztromok.com).
- <g> — groups multiple shapes together, letting transforms and styles apply to the whole group at once.
- <use> — references and reuses a shape defined elsewhere, avoiding duplicating the same path data repeatedly.
Chapter 3 Quick Reference
- Inline <svg> — every shape is a real, inspectable DOM element, unlike canvas's opaque pixels
- viewBox — defines internal coordinates independent of rendered size; the key to genuine scaling without distortion
- SVG presentation properties (fill, stroke, stroke-width) — styled via CSS exactly like standard properties
- setAttribute/style/addEventListener — full per-shape JavaScript manipulation, impossible with canvas
- <img src="x.svg"> for simple decorative icons; inline SVG when individual shapes need styling/scripting
- <path>/<g>/<use> — the building blocks behind most real-world icon sets and complex SVG graphics
- Next chapter: Drag and drop API