How to Change Svg Fill Color When Used as Base-64 Background Image Data

how to change svg fill color when used as base-64 background image data?

I want to post an answer to my own question. No exactly an answer, since it doesn't imply svg as background and base64, but it's my current alternative way, much more effective if you just need mono-color icons: just use svg as css mask, and change background color. Then you can also apply transitions on mouse events. I mean something like this:

mask-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" xml:space="preserve"><path d="M11.432 9.512a.636.636 0 0 1 0 .918L2.12 19.742a.636.636 0 0 1-.458.201.637.637 0 0 1-.46-.201l-1-.998C.068 18.609 0 18.459 0 18.285s.068-.329.199-.461l7.854-7.852L.199 2.118A.638.638 0 0 1 0 1.66c0-.174.068-.327.199-.46l1-.998A.634.634 0 0 1 1.66 0c.172 0 .325.068.458.201l9.314 9.311z"/></svg>');
mask-size: auto 12px;
mask-repeat: no-repeat;
transition: background-color 200ms;
background-color: #ff0000;

Please see this fiddle for come more code and demo:

https://jsfiddle.net/o25beLqj/

Also note a couple of things:

  • mask-image does not have base64 code inside. It's just preceeded by data:image/svg+xml;utf8,

  • mask css properties are quite about the same of css background properties, take a look to docs, and you'll see you can do really a lot and they can substitute background images in a variety of use cases

how can I change color of an svg img in base64 format?

As you do not clarified if can't modify the original file or if must do it on client-side, I propose a

Programmatically approach:

  1. Lookup the <img>
  2. From img.src strip "data:image...", to extract Base64 encoded SVG.
  3. Decode it using atob
  4. Create an element, just for find the <path>
  5. Set fill on path.
  6. Append the new Image or Replace current one.

Example:



// Lookup the <img> by Id

var img = document.getElementById("target");

// Strip "data:image/svg+xml;base64," to get encoded SVG

var encoded = img.src.substring(26);

// Decode base64

var decoded = atob(encoded);

// Create an HTML element from decoded SVG

var wrapper = document.createElement('div');

wrapper.innerHTML = decoded;

var newSvg = wrapper.firstChild;

// Lookup the <path> and get a ref

var innerPath = newSvg.getElementsByTagName('path')[0];

// Set up new color

innerPath.setAttribute("fill", "#FF0000");

// Show modified image

document.body.appendChild(newSvg);
<img id="target" src="data:image/svg+xml;base64,P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IEFyaWFsLCBTYW5zLXNlcmlmIiBmaWxsPSIjMDAwMDAwIj5mcm9tIHRoZSBOb3VuIFByb2plY3Q8L3RleHQ+PC9zdmc+">

Setting the fill color of base 64 encoded SVG on Image element?

You can base64 decode the image to svg markup, then amend it adding your style rule, base64 encode it again and set the src of the image to the new one.

I'm haven't tested it but can't see a reason why it wouldn't work.

CSS background url SVG fill color not working (not base64) when compiling from SASS

In svg, # needs to be encoded, and replaced with %23.

So you need to create a function to do the replacement. I.E.:

@function url-encoded-color($color) {
@return '%23' + str-slice('#{$color}', 2, -1)
}

And then for svg, place it instead of variable directly:

   background: {
image: url('data:image/svg+xml;utf8, <svg xmlns="http://www.w3.org/2000/svg" width="2" height="1"><rect width="1" height="1" fill="#{url-encoded-color($body-color)}" /></svg>');
repeat: repeat;
}

Full example:
https://jsfiddle.net/niklaz/26Ljsmdu/3/

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

I needed something similar and wanted to stick with CSS. Here are LESS and SCSS mixins as well as plain CSS that can help you with this. Unfortunately, it's browser support is a bit lax. See below for details on browser support.

LESS mixin:

.element-color(@color) {
background-image: url('data:image/svg+xml;utf8,<svg ...><g stroke="@{color}" ... /></g></svg>');
}

LESS usage:

.element-color(#fff);

SCSS mixin:

@mixin element-color($color) {
background-image: url('data:image/svg+xml;utf8,<svg ...><g stroke="#{$color}" ... /></g></svg>');
}

SCSS usage:

@include element-color(#fff);

CSS:

// color: red
background-image: url('data:image/svg+xml;utf8,<svg ...><g stroke="red" ... /></g></svg>');

Here is more info on embedding the full SVG code into your CSS file. It also mentioned browser compatibility which is a bit too small for this to be a viable option.

Base64 Encoded SVG Image as Background-Image CSS Property

I've changed the value for the viewBox to viewBox='-166 -589 532 71' since yours (viewBox='0 0 141 19') will put the shape far away outside the svg canvas. I hope it helps.

.borderCurve{

/*position: absolute;

bottom: -20px;*/

background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='-166 -589 532 71'%3E%3Cpath d='M366-589c-6 1-11 5-16 9-6 7-10 16-10 26 0 20-15 36-34 36h-411c-9 0-18-4-24-10-7-7-10-16-10-26 0-17-12-31-27-35h532z' fill='red' /%3E%3C/svg%3E");

background-size: contain;

background-position: center top;

background-repeat: no-repeat;

width: 158px;

height: 20px;

opacity: 0.7;

outline:1px solid;

}
<div class="borderCurve"></div>

Changing the fill color of SVG image loaded by data-URL inside a stylesheet

The only way I've found is to generate my svgs with a small (self written) tool, that converts the colors to fit my needs. But there is no solution that can change the colors on runtime. So each colors doubles size for each image. However the tool adds the ability to generate something like an imagemap as css. Often this is used for png icons, that are arranged on a large picture to avoid multiple http requests.



Related Topics



Leave a reply



Submit