HTML / CSS: Background image from raw image data, not URL

I get raw image data of a png image from a web server and want to use it as a background image of a div element. Is this possible without saving data as an image file in the first place?

+1
javascript html css background-image
source share
2 answers

Use the data URI :

document.getElementById('e').src = 'data:image/png;base64,ton_of_crap'; 

This Wikipedia article has code for PHP and Python that displays base-64 encoded images. You can simply paste this into the url() the CSS file (or the src attribute of the <img /> ), and it should work.

+4
source share

Have you tried (with jQuery):

 $('#divID').css("background-image", "url(/myimage.png)"); 

From Switching a DIV Background Image Using jQuery

+1
source share

All Articles