Is there a way to set background-image as a base64 encoded image?
Asked Answered
H

11

159

I want to change background dynamically in JS and my set of images is in base64 encoded. I try:

document.getElementById("bg_image").style.backgroundImage = 
   "url('http://amigo.com/300107-2853.jpg')"; 

with perfect result,

yet I fail to do the same with:

document.getElementById("bg_image").style.backgroundImage = 
   "url('data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAYAAAAUYybjAAAgAElE...')";

nor

document.getElementById("bg_image").style.backgroundImage = 
   "data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAYAAAAUYybjAAAgAElE...";

Is there any way to do it?

Heinrike answered 13/6, 2013 at 15:7 Comment(2)
url(' should work, my problem was that the ActionScript dataURL actually had newlines, and I had to replace(/\n/g, '')Keon
yourimageelement.setAttribute('style', 'background-image:url('+ yourbase64data +')');Tonjatonjes
C
164

I tried to do the same as you, but apparently the backgroundImage doesn't work with encoded data. As an alternative, I suggest to use CSS classes and the change between those classes.

If you are generating the data "on the fly" you can load the CSS files dynamically.

function change() {
  if (document.getElementById("test").className == "backgroundA") {
    document.getElementById("test").className = "backgroundB";
    document.getElementById("test2").className = "backgroundA";
  } else {
    document.getElementById("test").className = "backgroundA";
    document.getElementById("test2").className = "backgroundB";
  }
}

btn.onclick = change;
.backgroundA {
  background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHR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QcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVrApTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KTkpFvZj4ZbYeCiXaOiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxMAF15XEFRXgZWWdciuM8GCmdSQ84lLQfY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=");
}
<div id="test" height="20px" class="backgroundA">
  div test 1
</div>
<div id="test2" name="test2" height="20px" class="backgroundB">
  div test2
</div>
<input type="button" id="btn" />

I fiddled it here, press the button and it will switch the divs' backgrounds: http://jsfiddle.net/egorbatik/fFQC6/

Creolized answered 13/6, 2013 at 16:17 Comment(4)
thank you, yet i work with big quantity of elements, so it's not practical to exchange classes.Heinrike
@IgorSavinkin and whomever reads this in the future. If you are changing lots of elements to the same background simultaneously, you can use css to cascade the rendering, by putting a class against each element that you want to be changed, but changing the class of a common ancestor element. For example, the css would be #ancestor.backgroundA .Change{background-image:...} and #ancestor.backgroundB .Change{background-image...}, where #ancestor is the id of the common ancestor, and .Change is the class applied to all elements to which to apply one of the backgrounds.Stichter
It's funny to see everyone voting this answer as the best before even thinking if it would be a good practice to create a css with a million classes... or even better: a css containing a million base64 encoded images (tip: just think of a folder with a million maybe HD, images in it). But yeah, this works for a few images.Actino
Also, how would you solve the problem of adding new images to your system available images? is this approach even scalable?Actino
C
50

Had the same problem with base64. For anyone in the future with the same problem:

url = "data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAYAAAAUYybjAAAgAElE...";

This would work executed from console, but not from within a script:

$img.css("background-image", "url('" + url + "')");

But after playing with it a bit, I came up with this:

var img = new Image();
img.src = url;
$img.css("background-image", "url('" + img.src + "')");

No idea why it works with a proxy image, but it does. Tested on Firefox Dev 37 and Chrome 40.

Hope it helps someone.

EDIT

Investigated a little bit further. It appears that sometimes base64 encoding (at least in my case) breaks with CSS because of line breaks present in the encoded value (in my case value was generated dynamically by ActionScript).

Simply using e.g.:

$img.css("background-image", "url('" + url.replace(/(\r\n|\n|\r)/gm, "") + "')");

works too, and even seems to be faster by a few ms than using a proxy image.

Cathleencathlene answered 2/2, 2015 at 22:57 Comment(3)
Removing the line breaks fixed it for me.Aldin
Good investigation.Quadrinomial
This answer might be really old, but its still a solid valid answer! And it stopped me from banging my head against the wall.Maryrosemarys
M
36

Try this, I have got success response ..it's working

$("#divId").css("background-image", "url('data:image/png;base64," + base64String + "')");
Modestine answered 10/10, 2014 at 8:46 Comment(1)
Seems if data:image/png;base64 is long, and the string define as jquery variable, then does not work. But works if string defined as php variable like PHClaus example.... Just info about what i testedAretina
P
13

Adding this trick to gabriel garcia's following solution -

var img = 'data:image/png;base64, ...'; //place ur base64 encoded img here
document.body.style.backgroundImage = 'url(' + img + ')';

However, in my case this wasn't working. Moving url into the img variable did the trick. SO the final code looked like this

var img = "url('data:image/png;base64, "+base64Data + "')";
document.body.style.backgroundImage = img; 
Price answered 3/9, 2016 at 9:14 Comment(1)
@Amit Kumar Rai you're right. I was missing the single quotes on my code, which you actually wrote so your code works.Actino
A
11

I tried this (and worked for me):

var img = 'data:image/png;base64, ...'; //place ur base64 encoded img here
document.body.style.backgroundImage = 'url(\'' + img + '\')';

ES6 syntax:

let img = 'data:image/png;base64, ...'
document.body.style.backgroundImage = `url('${img}')`

A bit better:

let setBackground = src => {
    this.style.backgroundImage = `url('${src}')`
};

let node = nodeIGotFromDOM, img = imageBase64EncodedFromMyGF;
setBackground.call(node, img);
Actino answered 17/2, 2016 at 16:11 Comment(2)
Funny, because this is exactly the code that people are saying does not work.Mou
This works for me too. Tested on Microsoft Edge and IE 11 as wellEngen
Z
4

This is the correct way to make a pure call. No CSS.

<div style='background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAFCAYAAABW1IzHAAAAHklEQVQokWNgGPaAkZHxPyMj439sYrSQo51PBgsAALa0ECF30JSdAAAAAElFTkSuQmCC)repeat-x center;'></div>
Zahara answered 19/12, 2015 at 18:46 Comment(2)
<div style='background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAFCAYAAABW1IzHAAAAHklEQVQokWNgGPaAkZHxPyMj439sYrSQo51PBgsAALa0ECF30JSdAAAAAElFTkSuQmCC)repeat-x center;'></div>Zahara
To post code, indent it by four spaces or select it and press Ctrl-K.Manicurist
S
3

I think this will help, Base64 is addressed by CSS in a different way, you should set the data type of the image to base64, this will help the CSS to change the base64 to image and display it to the user. and you can use this from javascript by assigning the background image using the jquery script, it will automatically change the base64 to mage and display it

url = "data:image;base64,"+data.replace(/(\r\n|\n|\r)/gm, "");
$("body").css("background-image", "url('" + url.replace(/(\r\n|\n|\r)/gm, "") + "')");
Setting answered 22/1, 2019 at 0:50 Comment(1)
Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. I can't see any real difference between this and one of the previously posted answers.Minorite
N
3

Do not use quotes inside url when using data URIs.

li {
  background:
    url(data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7)
    no-repeat
    left center;
  padding: 5px 0 5px 25px;
}
Nephelinite answered 19/5, 2021 at 19:21 Comment(2)
Is it against CSS / data URIs to use quotes? Your link doesn't mention the issue.Bondy
Apparently it should work too, but for some reason, the data URI only worked without quotes for me.Nephelinite
C
2

What I usually do, is to store the full string into a variable first, like so:

<?php
$img_id = 'data:image/png;base64,iVBORw0KGgoAAAAAAAAyCAY...';
?>

Then, where I want either JS to do something with that variable:

<script type="text/javascript">
document.getElementById("img_id").backgroundImage="url('<?php echo $img_id; ?>')";
</script>

You could reference the same variable via PHP directly using something like:

<img src="<?php echo $img_id; ?>">

Works for me ;)

Crump answered 7/8, 2013 at 1:52 Comment(0)
M
0

In my case, it was just because I didn't set the height and width.

But there is another issue.

The background image could be removed using

element.style.backgroundImage=""

but couldn't be set using

element.style.backgroundImage="some base64 data"

Jquery works fine.

Monnet answered 10/1, 2019 at 8:46 Comment(0)
R
0

Also make sure the target element which you aim to set the background image has the following CSS settings:

display: block; /* might be 'inline-block' as well */
width: 24px; /* define the image width here */
height: 24px; /* define the image width here */

Otherwise, the image may not appear properly.

Rotherham answered 9/4, 2024 at 15:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.