Explain Codes LogoExplain Codes Logo

How to modify the fill color of an SVG image when being served as background image?

css
css-masks
css-filters
svg-color-modification
Anton ShumikhinbyAnton Shumikhin·Sep 11, 2024
TLDR

To modify the fill color of an SVG served as a background image, embed the SVG code directly into the CSS url function:

<div style="background-image: url('data:image/svg+xml;utf8,<svg xmlns=&quot;http://www.w3.org/2000/svg&quot;><rect fill=&quot;#HEX_COLOR&quot; width=&quot;100%&quot; height=&quot;100%&quot;/></svg>');"></div>

Simply replace #HEX_COLOR with your color of choice. This keeps JavaScript out of the picture and empowers you to style SVGs as a CSS background-image. Ensure you URL encode the SVG data before usage.

Mastering SVG color control with CSS

Harnessing CSS masks and filters

Use CSS masks to alter the color of shapes inside a background SVG. This technique is equivalent to casting a colored spotlight on the SVG. Cast your spotlight with the appropriate color!

Furthermore, level up your color transformation by applying CSS filters such as sepia, hue-rotate, brightness, and saturation:

.element-with-svg-background { -webkit-mask-image: url('path/to/your.svg'); /* Mask-in, fashionably late! */ mask-image: url('path/to/your.svg'); background-color: red; /* Apply background color */ filter: hue-rotate(90deg); /* Spin the color wheel */ }

Embedding SVG with data URI

For finer control, you can even embed the SVG directly within your CSS using a data URI. Now that, is some real Inception-level hacking:

@mixin svg-background($color) { background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg"><rect fill="#{$color}" width="100%" height="100%"/></svg>'); /* Color INSIDE the SVG! */ } .element { @include svg-background(#ff0000); /* Paint it red! */ }

Server-side dynamic SVGs

If you operate on a larger scale, consider serving SVGs with a server-side script that customizes color based on GET parameters. It allows you to have a dynamically controlled color palette, much like a chameleon.

https://example.com/icon.svg?color=ff0000 /* Now serving: Red SVGs */

Mastering layering with z-index

Position your SVGs absolutely and manipulate the z-index parameter to gain complete control over SVG layering:

.svg-background { position: absolute; z-index: 1; /* "I'll take the bottom bunk" */ } .another-element { z-index: 2; /*"Top bunk for me! No smelly feet in my face." */ }

Visualization

Consider this illustration of applying a new layer behind the SVG akin to a colored filter:

Initial 🧴: Transparent and uncolored SVG Sticker 🏷️: SVG image with default color

Adding a colored layer 🏷️:

🧴 + 🔵🏷️ = 🧴 with Blue-tinted SVG

You can think of this trick like adding a colored ::before pseudo-element behind your SVG, mirroring a change in color fill.

Understanding browser compatibility

One important caveat is that browser compatibility can be a party-pooper. Not all methods work universally across browsers, drown your sorrows on CanIuse.

Playing with unique URL outputs

Make each URL output a unique SVG color based on the GET parameters input.

Animating SVG colors

Make your SVGs dynamic by adding CSS keyframes and transitions. CSS can turn your dull image into a party!

Learning from resources and tutorials

Don't forget to explore outside. There is a sea of knowledge available on SVG color modification that could provide you insights and best-practices that could elevate your SVG manipulation prowess!