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

Styling SVG Icons with CSS

Control SVG fill, stroke, and properties with CSS including currentColor for themeable icon systems.

Styling SVG Icons with CSS

What You'll Learn

You'll learn how to control SVG icon appearance using CSS properties and leverage currentColor to build flexible, theme-aware icon systems.

Why CSS-Styled SVGs Matter

When you inline SVG icons (as covered in "SVG Basics and Inline vs External"), you unlock a powerful capability: styling them with CSS just like any other HTML element. This means your icons can adapt to themes, states, and contexts without creating multiple versions of the same graphic.

Core CSS Properties for SVGs

SVGs have unique styling properties that differ from standard HTML elements:

fill — Controls the interior color of SVG shapes (like background-color for regular elements)

.icon {
  fill: #3498db;
}

stroke — Controls the outline color of SVG paths (similar to border-color)

.icon {
  stroke: #2c3e50;
  stroke-width: 2px;
}

opacity, width, height — Work just like their HTML counterparts

The Magic of currentColor

The currentColor keyword is your secret weapon for themeable icons. It references the computed color property of the element:

.button {
  color: blue;
}

.button .icon {
  fill: currentColor; /* Automatically inherits blue */
}

When the button's text color changes (hover, dark mode, etc.), the icon automatically matches—no extra CSS needed. Think of it as a universal color variable that flows through your design system.

Targeting SVG Elements

You can style specific parts within an inline SVG:

.icon path {
  fill: currentColor;
}

.icon circle {
  stroke: currentColor;
  fill: none;
}

Key Takeaway: Use CSS properties like fill, stroke, and especially currentColor to create flexible, maintainable SVG icon systems that automatically adapt to your design tokens and themes without duplicating assets.