Last updated: Apr 4, 2024
Reading time·3 min
To set the background-image
property as a base64 encoded image using
JavaScript:
style.backgroundImage
property to the result of calling the url()
function with the data URL, e.g. url('${dataURL}')
.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box">bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const dataURL = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const box = document.getElementById('box'); box.style.backgroundImage = `url('${dataURL}')`;
We used the document.getElementById()
method to select the div
that has an ID of box
.
The next step is to set the style.backgroundImage
CSS property of the div to
the result of calling the
url() CSS function with
the data URL.
The url()
function takes an absolute URL, a relative URL, a blob URL or a data
URL as a parameter.
If your base64 encoded string doesn't contain the prefix, make sure to prepend it.
const dataURLWithoutPrefix = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const box = document.getElementById('box'); const prefix = 'data:image/png;base64, '; box.style.backgroundImage = `url('${prefix}${dataURLWithoutPrefix}')`;
The code sample adds the data:IMAGE_TYPE;base64,
prefix to construct the data
URL.
The same approach can be used to set the background-image property to a base64 encoded image when a button is clicked.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box">bobbyhadz.com</div> <br /> <br /> <button id="btn">Click</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const button = document.getElementById('btn'); button.addEventListener('click', () => { const dataURL = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const box = document.getElementById('box'); box.style.backgroundImage = `url('${dataURL}')`; });
We
added a click event listener
to the button element to set the background-image
property on the div
to a
base64 encoded image when the button gets clicked.
The same approach can be used to toggle the background-image
of the element.
const button = document.getElementById('btn'); button.addEventListener('click', () => { const dataURL = 'data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='; const box = document.getElementById('box'); if (box.style.backgroundImage) { box.style.backgroundImage = ''; } else { box.style.backgroundImage = `url('${dataURL}')`; } });
You can also use a class to set the background-image
property to a base64
encoded image.
Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="stylesheet" href="style.css" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box">bobbyhadz.com</div> <script src="index.js"></script> </body> </html>
Here is the code for the style.css
file.
.background-red-dots { background-image: url('data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='); }
And here is the related JavaScript code.
const box = document.getElementById('box'); box.classList.add('background-red-dots');
Make sure to load the .css
file in your HTML file.
<link rel="stylesheet" href="style.css" />
The CSS file consists of a single class that defines the background-red-dots
class.
.background-red-dots { background-image: url('data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='); }
The JavaScript file simply selects the div
element and uses the
classList.add() method to add
the class to the element.
const box = document.getElementById('box'); box.classList.add('background-red-dots');
The same approach can be used to set the background-image
to a base64 encoded
image when a button is clicked.
Here is the updated HTML code.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <link rel="stylesheet" href="style.css" /> </head> <body> <h2>bobbyhadz.com</h2> <div id="box">bobbyhadz.com</div> <button id="btn">Click</button> <script src="index.js"></script> </body> </html>
And here is the updated JavaScript code.
const button = document.getElementById('btn'); button.addEventListener('click', () => { const box = document.getElementById('box'); box.classList.add('background-red-dots'); });
When the button gets clicked, the event handler function is invoked.
The function uses the classList.add()
method to add the background-red-dots
class to the div
element.
The same approach can be used if you need to toggle the background-image
property.
const button = document.getElementById('btn'); button.addEventListener('click', () => { const box = document.getElementById('box'); box.classList.toggle('background-red-dots'); });
I've also written an article on how to convert an image or an image URL to base64 in Node.js.
You can learn more about the related topics by checking out the following tutorials: