Skip to content
On this page

How to use icons in CSS

Using icons in CSS is easy: set icon as background or mask image, use simple <span> element in HTML to render icon.

Skip to list of available tools if you want to skip long explanation of how it all works.


There are advantages and disadvantages of using SVG in CSS over using SVG in HTML.


  • No repeating. Only one entry for each icon.
  • No deep DOM tree.
  • Can use icons from untrusted sources because if there are any scripts in SVG, they are not executed.

However, it also has disadvantages:

  • You cannot target elements inside icons, such as changing stroke-width.
  • CSS usually contains all icons, including ones not used on the current page.

How icons are rendered

There are two types of icons:

  • Icons with hardcoded palette.
  • Monotone icons that change color.

You can use both types in CSS.

Icons with palette

Icons with hardcoded palette can be rendered as background images:

html<span class="background-demo"></span>
<span class="background-demo background-demo--2"></span>
css.background-demo {
   /* Add dimensions to span */
   display: inline-block;
   width: 32px;
   height: 32px;
   /* Add background image */
   background-image: url('');
   background-repeat: no-repeat;
   background-size: 100% 100%;

.background-demo--2 {
   background-image: url('');

Rendering as content

Icons with hardcoded palette can also be rendered as content of pseudo-elements.

It is similar to using icons as background images, but with a difference: icon's size cannot be controlled with CSS. You need to set width and height in SVG in pixels.

html<span class="content-demo"></span>
<span class="content-demo content-demo--2"></span>
css.content-demo::after {
   content: url('');

.content-demo--2::after {
   content: url('');

There are no clear advantages of rendering icons as content of pseudo-elements instead of background images.

Monotone icons

Monotone icons can be rendered as mask images with background color set to currentColor:

html<span class="mask-demo"></span>
<span class="mask-demo"></span>
<span class="mask-demo mask-demo--2"></span>
<span class="mask-demo mask-demo--2"></span>
<span class="mask-demo mask-demo--3"></span>
<span class="mask-demo mask-demo--3"></span>
css.mask-demo {
   /* Add dimensions to span */
   display: inline-block;
   width: 32px;
   height: 32px;
   /* Add background color */
   background-color: currentColor;
   /* Add mask image, use variable to reduce duplication */
   --svg: url('');
   -webkit-mask-image: var(--svg);
   mask-image: var(--svg);
   -webkit-mask-repeat: no-repeat;
   mask-repeat: no-repeat;
   -webkit-mask-size: 100% 100%;
   mask-size: 100% 100%;

.mask-demo--2 {
   --svg: url('');

.mask-demo--3 {
   --svg: url('');

Using currentColor as background color makes it easy to change icon color by changing text color.


How to generate CSS for icons in the Iconify ecosystem?

There are several ways to do it:

Custom plugin

Want to build a custom plugin that generates CSS?

Iconify Utils package includes all functions you need. Process of generating CSS is simple:

Code samples that should help you:

Released under the Apache 2.0 License.