How to use SVG icons in CSS
Quick steps
- Pick the techniquebackground-image for fixed colours, mask-image when the icon must change colour on hover or in dark mode.
- Turn the SVG into a URLReference a file with url("/icons/search.svg"), or inline it as a URL-encoded data URI (SVG Creator's Data URI export does this).
- Size the elementGive the element a width and height and use background-size or mask-size: contain.
- Attach itApply the class to an empty element, or to ::before on a button or link so no extra markup is needed.
1. As a background image
The simplest way. This is SVG Creator's CSS background export for the search icon, with the colour baked in:
.icon-search {
display: inline-block;
width: 24px;
height: 24px;
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='10.5' cy='10.5' r='6.5'/%3E%3Cline x1='15.5' y1='15.5' x2='21' y2='21'/%3E%3C/svg%3E") no-repeat center / contain;
}Background images cannot be recoloured with color: any currentColor inside the SVG renders as black, because a background image is a separate document. Use this when the icon always has the same colour.
2. As a mask, so it follows the text colour
A mask uses the icon's shape as a stencil and paints it with the element's background colour. Set that to currentColor and the icon now changes colour with color, on hover and in dark mode:
.icon-search {
display: inline-block;
width: 1.25em;
height: 1.25em;
background-color: currentColor;
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='10.5' cy='10.5' r='6.5'/%3E%3Cline x1='15.5' y1='15.5' x2='21' y2='21'/%3E%3C/svg%3E") no-repeat center / contain;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' width='24' height='24' fill='none' stroke='currentColor' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='10.5' cy='10.5' r='6.5'/%3E%3Cline x1='15.5' y1='15.5' x2='21' y2='21'/%3E%3C/svg%3E") no-repeat center / contain;
}
a:hover .icon-search { color: #5b4cf0; }All current browsers support mask; the -webkit- line covers older Safari and Chromium versions. The colours inside the SVG do not matter for a mask, only its shape and opacity.
3. In a pseudo-element
To add an icon to existing links or buttons without touching the HTML, put either technique on ::before:
.external::after {
content: "";
display: inline-block;
width: 0.9em;
height: 0.9em;
margin-left: 0.25em;
vertical-align: -0.1em;
background-color: currentColor;
mask: url("/icons/external.svg") no-repeat center / contain;
}Pseudo-element icons are invisible to screen readers when content is empty, which is right for decorative icons. If the icon carries meaning, put the meaning in visible or visually hidden text.
Data URI encoding rules
Inline SVG in CSS must be URL-safe. A few rules avoid most bugs:
- Encode
#as%23. An unencoded#infill="#5b4cf0"ends the URL early and the icon silently disappears. - Wrap the URL in double quotes and use single quotes inside the SVG.
- Also encode
<,>and%. You do not need base64: URL-encoded SVG is usually smaller, because base64 adds about a third to the size. - Keep
xmlns="http://www.w3.org/2000/svg"on the root; without it the image does not render.
The search icon above is 267 bytes as SVG and 302 bytes as a URL-encoded data URI.
File or data URI?
A data URI saves a request and ships with the stylesheet, which suits a handful of small icons. For dozens of icons, separate files (or an SVG sprite) cache better and keep the CSS small. Either way, set the element's size in CSS so nothing shifts while it loads.
Frequently asked questions
Can I change the colour of an SVG background image with CSS?
Not directly. A background image is rendered as a separate image, so color and fill do not reach it. Use mask-image with background-color instead, or edit the colour inside the SVG.
Why does my SVG data URI not show up?
The most common causes are an unencoded # in a colour, double quotes inside a double-quoted url(), or a missing xmlns attribute.
Is mask-image supported in all browsers?
Yes, in all current versions of Chrome, Edge, Firefox and Safari. Keep the -webkit-mask fallback for older Safari and Chromium.