SVG Background Color in CSS — Mask, Image, or Inline
Published August 13, 2026 · 6 min read
“SVG background color” is three different requests. People want to paint a color behind an icon, recolor a monochrome glyph from CSS, or keep a multicolor illustration as a background image. The CSS looks similar. The failure modes do not.
| What you actually want | Use | Avoid |
|---|---|---|
| A fill behind the artwork | background-color on the box, or a <rect> in the SVG | A mask, unless the icon itself should change color |
| One icon, many theme colors | mask-image + currentColor | Hard-coded fills inside a background-image |
| Keep original brand colors | Encoded SVG as background-image | A mask — it flattens every path |
| Per-path color, hover, or motion | Inline SVG (or a React component) | Any CSS image/mask. Paths are not in the DOM. |
1. Paint a color behind the SVG
If the artwork already has the right fills, you only need a box color. The SVG stays an image. CSS never touches its paths.
.hero-mark {
width: 3rem;
height: 3rem;
background-color: #0f172a;
background-image: url("/logo.svg");
background-repeat: no-repeat;
background-position: center;
background-size: 1.75rem;
}The same idea works with an inline data URI. Encode the SVG, keep its original fills, and let background-color show through transparent regions. That is the right move for badges, avatars, and logos that already have a palette.
Do not reach for mask-image here. A mask replaces those fills with a single color. That is a different product.
2. Recolor a one-color icon with a CSS mask
Icons in a design system usually need to follow color: default, hover, disabled, and dark mode. A CSS mask turns the SVG into a stencil. The element's background-color is the paint.
.icon {
width: 1.5rem;
height: 1.5rem;
color: #818cf8;
background-color: currentColor;
-webkit-mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='black' d='M12 2 2 7v10l10 5 10-5V7L12 2Z'/%3E%3C/svg%3E");
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='black' d='M12 2 2 7v10l10 5 10-5V7L12 2Z'/%3E%3C/svg%3E");
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
-webkit-mask-position: center;
mask-position: center;
-webkit-mask-size: contain;
mask-size: contain;
}The fill inside the SVG does not matter. Masks read alpha, not hue. Black, white, or currentColor in the source all produce the same stencil as long as the shape is opaque.
With background-color: currentColor, one encoded asset covers every state:
.icon:hover { color: #c4b5fd; }
.icon:disabled { color: #64748b; }
@media (prefers-color-scheme: dark) {
.icon { color: #a5b4fc; }
}Generate that snippet from the SVG color changer. Paste the markup, pick currentColor or a token like var(--icon), and copy the mask rule. Encoding stays in the browser.
3. Keep a multicolor SVG as a background image
Brand marks, flags, and illustrations are not stencils. Use background-image so the original fills survive:
.logo {
width: 8rem;
height: 2.5rem;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 96 32'%3E%3Crect width='96' height='32' rx='6' fill='%230f172a'/%3E%3Ccircle cx='18' cy='16' r='6' fill='%2338bdf8'/%3E%3Crect x='30' y='12' width='50' height='8' rx='2' fill='%23e2e8f0'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-size: contain;
}Hash characters in hex colors must be encoded as %23. Forget that and the rule dies silently. The SVG to CSS converter emits a complete background-image declaration with the encoding already done.
This is the technique covered in Use SVG as a CSS background image. Use it when the artwork's colors are the product, not a theme token.
When a mask is the wrong tool
A mask is a one-way flatten. Skip it when:
- Two or more colors must remain visible at once.
- A gradient, shadow, or photographic embed is part of the graphic.
- Only one path should change on hover.
- You need to animate or focus an internal shape.
Those cases need the SVG in the DOM. Inline the markup, or convert it to a component with the SVG to JSX converter and color paths through currentColor, props, or CSS variables.
<svg viewBox="0 0 24 24" aria-hidden="true">
<path className="fill-slate-900 dark:fill-slate-100" d="..." />
<path className="fill-sky-400" d="..." />
</svg>currentColor on the SVG itself
If the SVG is already inline, you often do not need a mask at all. Set fill="currentColor" or stroke="currentColor" on the paths and change the element color. That is the cheapest recolor for icons that live in HTML or React.
It does not work for CSS background-image. An image has no inherited color. That is why masks exist: they move the color back onto the CSS box.
FAQ
How do I change an SVG background color in CSS?
Behind the artwork: background-color on the container. The icon itself: mask-image plus background-color: currentColor, a hex value, or var(--token). Generate the mask from the color changer.
Why did my logo become a silhouette?
You applied a mask. Masks ignore fills and keep alpha. Switch to background-image or inline the SVG.
Can I recolor only one path from CSS?
Not from a background or mask. Inline the SVG and target that path, or ship a React component and pass the color as a prop.
Is filter: hue-rotate a good substitute?
No. Filters shift every pixel, including the ones you wanted to leave alone, and they do not map cleanly onto design tokens. Use a mask for one-color icons and inline SVG for anything with a palette.
Further reading
- Change SVG color — paste markup, pick a color or token, copy a mask rule.
- SVG to CSS — background-image and mask data URIs.
- Use SVG as a CSS background image
- Inline SVG in React
Need a recolorable icon today? Open the SVG color changer, paste the SVG, and copy the CSS. Nothing is uploaded.