Course contentsShow
← Web Development
Lesson 2045 of 2,98237. Image and Asset OptimizationFree lesson

Multi-Color and Interactive SVG Icons

Build icons with multiple colors and CSS classes for hover states and dynamic styling requirements.

Multi-Color and Interactive SVG Icons

You'll learn how to create SVG icons with multiple colors and add interactive hover effects using CSS classes for dynamic styling.

The Core Idea

While simple icons might use a single fill color, real-world interfaces often need icons with multiple color parts—think a colorful notification badge, a dual-tone illustration, or icons that change color on hover. By structuring your inline SVG with classes and IDs on specific paths or groups, you can target individual parts with CSS for multi-color rendering and interactive states.

Think of it like coloring different parts of a line drawing: each section can have its own color, and you can change those colors when users interact with them.

Building Multi-Color Icons

Instead of applying fill to the entire <svg>, assign classes to individual paths or groups:

<svg width="24" height="24" viewBox="0 0 24 24">
  <path class="icon-body" d="M12 2L2 7v10c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V7z"/>
  <path class="icon-accent" d="M10 17l-4-4 1.41-1.41L10 14.17l6.59-6.59L18 9z"/>
</svg>
.icon-body {
  fill: #2196F3;
}

.icon-accent {
  fill: #FFC107;
}

Adding Interactive States

Use CSS pseudo-classes to change colors on interaction:

.icon:hover .icon-body {
  fill: #1976D2;
}

.icon:hover .icon-accent {
  fill: #FFA000;
}

.icon {
  transition: all 0.2s ease;
}

You can also rotate, scale, or transform individual parts on hover using transform on specific paths or groups.

Key Takeaway: Multi-color, interactive SVG icons are created by applying CSS classes to individual SVG paths or groups, allowing precise control over colors, hover states, and dynamic styling for each component part.