Lab 11: SVG in HTML

Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm node:20-alpine sh

Overview

Master SVG integration in HTML: viewBox, inline SVG, symbol/use icon systems, currentColor theming, CSS animations on SVG paths, SVG filters, and making SVGs accessible.


Step 1: Inline SVG & viewBox

<!-- Inline SVG: styled via CSS, interactive via JS -->
<svg
  xmlns="http://www.w3.org/2000/svg"
  viewBox="0 0 24 24"
  width="48"
  height="48"
  fill="none"
  stroke="currentColor"
  stroke-width="2"
  stroke-linecap="round"
  stroke-linejoin="round"
  aria-hidden="true"
>
  <circle cx="12" cy="12" r="10"/>
  <path d="M12 6v6l4 2"/>
</svg>

viewBox="minX minY width height":

  • Defines the coordinate system

  • viewBox="0 0 24 24" = 24×24 unit coordinate space

  • SVG scales to fill its container, coordinates remain 0–24

  • preserveAspectRatio controls alignment and overflow


Step 2: SVG Symbol System (Icon Library)

💡 focusable="false" prevents SVG from receiving focus in IE/Edge. Always include it on decorative SVG icons.


Step 3: currentColor for Theming


Step 4: CSS Animations on SVG


Step 5: SVG Filters


Step 6: SVG Accessibility


Step 7: Responsive SVG


Step 8: Capstone — SVG Generator + Validator

📸 Verified Output:


Summary

Feature
Technique
Purpose

viewBox

"0 0 24 24"

Scalable coordinate system

Icon system

<symbol> + <use href>

Reusable icon library

Theming

stroke: currentColor

Inherit text color

Draw animation

stroke-dasharray/offset

Path draw effect

Blur filter

<feGaussianBlur>

CSS-accessible blur

Decorative

aria-hidden="true"

Hide from screen readers

Meaningful

role="img" aria-label

Accessible SVG

Responsive

width: 100%; height: auto

Fluid SVG

CSS animation

@keyframes on SVG elements

Animated icons

Drop shadow

filter: drop-shadow()

CSS filter on SVG

Last updated