Get AI summaries of any video or article — Sign up free
Make Awesome SVG Animations with CSS // 7 Useful Techniques thumbnail

Make Awesome SVG Animations with CSS // 7 Useful Techniques

Fireship·
5 min read

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

TL;DR

Design SVG artwork in a vector tool and export with stable IDs so CSS can target individual shapes and groups reliably.

Briefing

CSS-driven SVG animation can turn simple vector shapes into interactive, themeable icons and looping UI sequences—without heavy libraries or complex tooling. The core workflow is to design the artwork in a vector editor, export clean SVG with stable element IDs, then use CSS variables plus transforms/opacity to animate colors and motion. The payoff is twofold: icons become easy to restyle (duotone themes) and motion becomes controllable both on hover/click and on an automatic loop.

The tutorial starts by grounding what SVG actually is: unlike raster formats such as PNG or JPEG (fixed pixel grids), SVG stores geometry and math. That means a viewBox defines a coordinate grid, and shapes scale cleanly because the browser recomputes the math at any size. The most flexible SVG primitive is the path element, but the practical advice is to avoid hand-writing graphics code. Instead, build shapes in tools like Figma, Illustrator, or Inkscape and export to SVG.

To recreate an animated duotone triangle icon, the process begins with reverse-engineering a real example using Chrome DevTools’ Animations panel, which reveals keyframes and which DOM elements they affect. A key detail emerges: what looks like two triangles is actually four—two are positioned off-canvas so the animation can slide them in and out.

In Figma, the author sets a frame sized to match the SVG viewBox (120 by 100) and draws the triangles with the polygon tool. Each triangle gets a custom layer name (e.g., dark 1, dark 2, light 1) so the exported SVG uses those names as element IDs. For duotone styling, triangles are grouped by color, producing SVG <g> containers (e.g., dark group and light group). Export settings matter: include IDs on export and remove the frame fill so the SVG contains only the intended artwork.

Back in code, the SVG is inlined into HTML so CSS can target it directly. The tutorial then removes hardcoded fill attributes from the SVG paths and replaces them with CSS variables defined on the root element. Those variables are assigned to the dark and light groups via their IDs, making the icon themeable. Hover animation uses CSS transitions: triangles are shifted with translateX and faded with opacity. The “hidden” triangle starts off-screen (translateX(-100%)) and slides into place on hover, while the top dark triangle moves and fades out.

Interactivity goes further with JavaScript: on click, a function picks a random color from an array and updates the CSS variables on document.documentElement. Because the elements already have transitions, the duotone colors shift smoothly rather than snapping.

Finally, the tutorial builds a looping animated sequence (phone outline, skeleton text, and falling bolt icons) using keyframes. Skeleton text fades upward via a simple fade-in-up animation. The bolt icons use a staggered drop-in keyframe over an 8-second cycle, with animation delays computed from per-icon CSS variables. Each bolt group gets an inline variable order, and the stylesheet sets animation-delay using calc(order * 200ms), ensuring consistent staggering even in a looping timeline. The result is a reusable pattern for interactive duotone icons and background UI animations powered by SVG plus CSS.

Cornell Notes

SVG stores shapes as geometry, so a viewBox-based coordinate grid lets artwork scale without pixelation. After designing triangles in Figma and exporting SVG with stable element IDs, the icon becomes themeable by removing inline fills and using CSS variables assigned to color groups (<g> elements). Hover motion is handled with CSS transitions by moving triangles with translateX and fading with opacity, including triangles that start off-canvas. Click interactivity updates the CSS variables in JavaScript, and the existing transitions animate the color change. For looping sequences, keyframes drive fade-in and drop-in effects, while staggered timing is achieved by assigning an order variable per icon and computing animation-delay with calc(order * 200ms).

Why does SVG scale cleanly compared with PNG or JPEG?

SVG is based on geometry and math rather than a fixed pixel grid. A viewBox defines a coordinate system, and shapes (like rect, circle, polygon, and especially path) are drawn using coordinates and curves. When the image is resized, the browser recomputes the math at the new resolution, so the artwork stays crisp without needing a larger pixel matrix.

How do element IDs in exported SVG make CSS animation practical?

Figma layer names can be exported as IDs on SVG elements. By naming triangles (e.g., dark 1, dark 2, light 1) and grouping them by color (e.g., dark group, light group), the exported SVG contains predictable IDs. Those IDs let CSS target specific triangles and groups reliably for transitions, opacity changes, and variable-based theming.

What’s the duotone theming trick for SVG icons?

Hardcoded fill attributes inside the SVG paths are removed, then CSS variables (e.g., --dark and --light) are defined on the root. CSS targets the SVG groups by ID and assigns fills using those variables. Changing the variables updates the icon’s colors without editing the SVG markup again.

How does the hover animation work without complex keyframes?

The icon uses CSS transitions. Each triangle’s transform and opacity are changed on svg:hover: the light triangle shifts right, the primary dark triangle shifts right while fading to opacity: 0, and the second dark triangle moves from off-screen (translateX(-100%)) back to translateX(0). Because transitions are set (duration 1s, ease), the motion interpolates smoothly.

How can click events randomize the icon’s colors smoothly?

JavaScript listens for a click and selects a random color from an array. It updates the CSS variables on document.documentElement (the same variables used for fills). Since transitions apply to properties on the targeted elements, the color change animates rather than snapping instantly.

How is staggered looping animation achieved for multiple bolt icons?

Each bolt group gets an inline CSS variable order that represents its appearance sequence. A keyframes animation (drop in) runs over 8 seconds, and animation-delay is set in CSS using calc(order * 200ms). The keyframes are designed so icons remain hidden for the first portion of the cycle, drop in during a specific window, and then hold their final state through 100% so the loop stays visually coherent.

Review Questions

  1. When exporting from Figma, what settings and naming choices ensure CSS can target the right SVG elements?
  2. How do CSS variables enable both theming and animated color transitions in the duotone icon?
  3. What keyframe timing considerations matter when staggering multiple elements in a looping animation?

Key Points

  1. 1

    Design SVG artwork in a vector tool and export with stable IDs so CSS can target individual shapes and groups reliably.

  2. 2

    Use viewBox-based SVG scaling to keep icons crisp at any size, then animate with transforms and opacity for smooth motion.

  3. 3

    Create duotone themes by removing inline fills from SVG paths and driving group fills via CSS variables.

  4. 4

    Implement hover interactions with CSS transitions by shifting triangles with translateX and fading with opacity, including off-canvas starting positions.

  5. 5

    Add click interactivity by updating the same CSS variables in JavaScript; existing transitions will animate the color change.

  6. 6

    For background motion, use keyframes for fade-in and drop-in effects, and stagger multiple elements using per-element order variables plus calc-based animation delays.

Highlights

Chrome DevTools’ Animations panel can reveal keyframes and the exact DOM elements involved, making it easier to replicate complex SVG motion.
What looks like two triangles in the animated icon is actually four—two are positioned off-canvas so CSS can slide them into view.
Duotone theming becomes straightforward once SVG fills are replaced with CSS variables assigned to color groups by ID.
Staggered looping animations can be cleanly managed by assigning an order variable per icon and computing animation-delay as calc(order * 200ms).

Topics

  • SVG Basics
  • Duotone Icons
  • CSS Transitions
  • Keyframe Animations
  • Staggered Timing