Get AI summaries of any video or article — Sign up free
I finally get how SVGs work thumbnail

I finally get how SVGs work

Theo - t3․gg·
5 min read

Based on Theo - t3․gg's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

Inline SVG turns shapes into DOM elements, enabling CSS selectors and JavaScript to target and animate individual parts of the illustration.

Briefing

SVGs stand out because they’re not just images—they’re XML-based drawing instructions that become part of the DOM. That means SVG artwork can be styled and animated with the same CSS and JavaScript techniques used for HTML, letting developers build interactive, resolution-independent graphics without turning everything into a single flattened “blob.” The practical payoff is huge: instead of fighting pixel-based assets (or recalculating geometry by hand), teams can treat shapes like real, addressable elements—circles, paths, strokes—and animate their attributes smoothly.

The core mental shift starts with format. Unlike JPEG or GIF, SVG is written in XML syntax, so it can be embedded inline directly inside HTML. The shapes inside—like <circle>, <rect>, <line>, <polygon>, and <path>—are “primitives” for illustrations rather than document structure. Once inline, SVG nodes become first-class DOM citizens. CSS can target specific parts of the illustration, and hover/focus states can change attributes such as fill, size, or position. Transitions can interpolate those changes, producing smooth effects that would otherwise require custom rendering logic.

From there, the transcript walks through how to think about SVG geometry and styling. Basic shapes use geometry attributes (coordinates, radii, point lists), while visual styling uses presentational attributes like fill and stroke. A key nuance: strokes are drawn centered on the shape’s outline, not purely inside or outside—so the “border box” behavior differs from CSS borders and can’t be configured to move the stroke inward or outward. Another spec detail matters for edge cases: when a rectangle collapses into a degenerate shape (e.g., width or height hits zero), modern browsers follow the spec and don’t render it.

Scalability is where many people get stuck. SVGs can be clipped when their internal coordinates don’t match the rendered element size. The fix is the viewBox attribute, which defines an internal coordinate system (top-left and bottom-right) and effectively controls zoom and panning within an “infinite” SVG canvas. Width and height still set the rendered size, while viewBox decides what portion of the coordinate space is visible—so the artwork scales cleanly without recalculating cx/cy values in JavaScript.

The most “SVG magic” in the transcript comes from strokes. Stroke customization goes far beyond color and width: line caps (butt/round/square), dash arrays (dash length and gap patterns), and dash offsets enable animated dashed lines, spinners, and drawing illusions. Smooth stroke animations can be driven by CSS transitions, and there’s also discussion of embedding CSS inside SVGs for environments that accept animated SVGs—though direct SVG animation approaches are broadly deprecated in modern standards. For the classic “it’s being drawn” effect, the technique uses a single dash whose length matches the shape’s circumference, then animates stroke-dashoffset from a hidden starting position to zero. The transcript highlights two ways to get the needed circumference: calculating it with JavaScript via getTotalLength(), or using the pathLength attribute to override the browser’s internal scaling.

Overall, the takeaway is that SVGs become far less intimidating once they’re treated like DOM elements with a coordinate system (viewBox) and a powerful stroke model (dash patterns + offsets). With those fundamentals, interactive graphics—hover states, smooth transitions, and animated “draw” effects—become practical tools rather than mysterious hacks.

Cornell Notes

SVGs are XML-based graphics that can be embedded inline in HTML, making their shapes addressable like DOM elements. That enables CSS and JavaScript to target specific SVG nodes (e.g., a circle’s fill or size) and animate them with transitions. The viewBox attribute defines an internal coordinate system so artwork scales without clipping, avoiding the need to recalculate coordinates in JavaScript. Stroke styling is especially powerful: stroke-linecap, stroke-dasharray, and stroke-dashoffset support dashed patterns and smooth animations, including spinners and “draw-on” illusions using path length. These capabilities turn SVG from a static image format into a dynamic, resolution-independent graphics system.

Why does inline SVG feel fundamentally different from using an SVG file as an image source?

Inline SVG can be dropped directly into an HTML document because it’s written in XML syntax. Once inline, SVG elements become first-class DOM nodes, so CSS selectors and JavaScript can target specific shapes (like a <circle>) and modify their attributes. That’s how hover/focus styles can change fill, size, or position, and how CSS transitions can animate those attribute changes smoothly—something that’s much harder when SVG is treated as a static binary image.

What problem does viewBox solve, and how does it differ from width/height?

viewBox prevents clipping and scaling breakage caused by mismatched coordinate systems. Width and height control the rendered size of the SVG element, while viewBox defines the internal coordinate system (top-left and bottom-right) that determines what portion of the SVG’s infinite canvas is visible—effectively setting zoom/pan. For example, an SVG rendered at 300×300 can show a 150×150 region by using a viewBox of 0 0 150 150, without changing the element’s pixel dimensions.

How do stroke and fill work in SVG, and what’s a key difference from CSS borders?

SVG shapes can be filled with the fill attribute and outlined with the stroke attribute. A crucial difference: the stroke is drawn centered on the shape’s outline, not strictly inside or outside like many border mental models. As a result, increasing stroke width reduces interior space and expands outward, and the behavior isn’t configurable to force the stroke to sit only inside or only outside.

What are “degenerate” shapes in SVG, and what happens when dimensions hit zero?

When a 2D shape collapses across one dimension—like a rectangle with width or height reduced to zero—it becomes a degenerate shape. Modern browsers follow the SVG spec and don’t render these collapsed shapes, even though older browser behavior differed. This matters when animating or dynamically computing geometry, because a zero dimension can make the element disappear entirely.

How do stroke-dasharray and stroke-dashoffset enable animated dashed lines and “draw” effects?

stroke-dasharray defines the repeating pattern of dash lengths and gaps along a path (e.g., “10 20” means a 10px dash followed by a 20px gap). stroke-dashoffset shifts that pattern along the path, which can be animated to create motion. For the “draw-on” illusion, a common approach sets a dash length equal to the path’s total circumference (or a scaled override) and uses an offset animation so the dash slides into view, making the shape appear to be traced over time.

How can the required circumference for a draw animation be determined?

One precise method uses JavaScript: select the SVG shape and call getTotalLength() to compute the path length. Then set stroke-dasharray to that length and animate stroke-dashoffset from a hidden starting value to zero. Another method is the pathLength attribute, which overrides the browser’s internal path length scaling (e.g., treating a real 763-length path as length 100), letting CSS use a simpler normalized circumference—though it can feel “funky” because it changes the browser’s scaling assumptions.

Review Questions

  1. When should a developer use viewBox instead of recalculating coordinates in JavaScript, and what does viewBox control versus width/height?
  2. Describe how stroke-dasharray and stroke-dashoffset work together to create a moving dashed pattern.
  3. What happens to SVG shapes when their dimensions create degenerate cases, and why does that matter for animations?

Key Points

  1. 1

    Inline SVG turns shapes into DOM elements, enabling CSS selectors and JavaScript to target and animate individual parts of the illustration.

  2. 2

    SVG is XML-based, so it can be embedded directly in HTML; the “magic” is that drawing instructions become part of the document tree.

  3. 3

    Use viewBox to define an internal coordinate system; it prevents clipping and enables responsive scaling without recomputing geometry.

  4. 4

    Strokes are centered on the shape outline and can’t be configured to draw only inside or outside, which changes how “border-like” sizing behaves.

  5. 5

    Degenerate shapes (e.g., rectangles with zero width or height) don’t render in modern browsers, so animations must avoid collapsing dimensions.

  6. 6

    stroke-linecap, stroke-dasharray, and stroke-dashoffset provide fine-grained control over dashed strokes and enable smooth animated effects.

  7. 7

    The “draw” illusion typically relies on path length (via getTotalLength() or pathLength) and animating stroke-dashoffset to slide a full-length dash into view.

Highlights

SVG’s real power comes from treating artwork as DOM nodes: CSS and JavaScript can target specific shapes and animate their attributes.
viewBox separates “what you render” from “what you see”: width/height set output size, while viewBox controls zoom/panning within an infinite canvas.
Stroke styling in SVG is unusually deep—dash patterns, dash offsets, and line caps can be animated to create spinners and draw-on effects.
The draw-on trick works by matching a dash length to the path’s circumference and animating stroke-dashoffset so the dash appears to trace the shape.

Topics

  • SVG Fundamentals
  • Inline SVG
  • viewBox Scaling
  • Stroke Dash Animations
  • Draw-On Effects

Mentioned

  • Josh Comeo
  • SVG
  • DOM
  • CSS
  • JS
  • XML
  • GIF
  • JPEG
  • AI