Styling with CSS: Using a Base64 Encoded Image in the Background URL

Can a Base64 encoded image be loaded as a background image URL without exposing the actual encoded string in the page source?

For example, if a Node API is used to GET request at "/image", it returns the serialized Base64 data.

res.json("data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//Aw.....blah blah

Is it possible to display this on the frontend as a URL only? So that the background-image shows the URL ONLY and not the actual Base64 encoded text in the frontend page source.

When you right-click and choose inspect, it will show this:

.my-image {
    background-image: url("http://exampleapi.net:8080/image");
}

But it won't show this:

.my-image {
    background-image: url('data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD.... etc.
}

Answer №1

Indeed, the process is slightly different when dealing with base64 data. Instead of serving the file as static content from the server, you can convert the base64 data to a blob on the client-side. This can then be used to generate a user-friendly URI that suits your needs.

const base64ToBlobUrl = async (base64Data) => {
    const base64Response = await fetch(base64Data);
    const blob = await base64Response.blob();
    const blobUrl = window.URL.createObjectURL(blob);
    return blobUrl;
}

With this function, you can obtain a URI suitable for use in an <img> tag or as an inline CSS background.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble with HTML - JavaScript function not processing responseText?

On my website, there is a button array that displays the position of a robot by reading a text file using a php/ajax combo. The script initially sets all buttons to the same color and changes the color of the button to represent the robot's position. ...

Error executing generateservertestreport script in npm run script

While executing an npm script through a TFS build, I encounter an issue. However, running the same script directly on the TFS build machine does not show any errors. Note: My node version is 8.12.0 and npm version is 6.4.1 Despite researching the cause o ...

What is the most optimal jQuery code to use?

Just wondering, which of the following code snippets is more efficient (or if neither, what would be the best way to approach this)? Background - I am working on creating a small image carousel and the code in question pertains to the controls (previous, ...

Troubleshooting the problem with JavaScript's week start date

I am encountering an issue with JavaScript when trying to obtain the first day of the week. It seems to be functioning correctly for most cases, but there is a discrepancy when it comes to the first day of the month. Could there be something that I am ove ...

Is it acceptable to conceal items by utilizing display:none?

When dealing with a dynamic website that includes components from various plugins, is it acceptable to hide elements temporarily or permanently using display:none? Sometimes clients may request certain items to be hidden from the page, so instead of removi ...

I've received an array of objects, but I require the objects to be consolidated into a single array using a MongoDB query

Hello, I am a newcomer to mongodb and I am attempting to generate a specific output using the mongodb aggregate function. I am aiming for a unified array of objects rather than multiple arrays of objects. Can someone assist me in achieving this desired out ...

"Utilizing FabricJs with Multiple Canvases to Display the Same Image Repeatedly

Within a loop ranging from 1 to 2, I have created two canvases on the screen and stored them in an object. However, when I load the same two images onto each canvas, only the last canvas displays the images. Why is it not loading on both canvases? Interest ...

How come this particular design is being passed down from a predecessor (and not a direct parent) div?

Web development can be frustrating at times. Hopefully, someone out there can shed some light on this issue and offer a solution. You can view the HTML/CSS code below or check out this example on JSFiddle The problem arises when I have a header div and a ...

Ensuring form field accuracy through server-side validation using Ajax - Mastering the art of Ajax

I am in need of implementing Ajax to validate my form fields, currently I have a javascript validation set up. Is it possible to reuse my existing javascript code and integrate Ajax for form validation? HTML Form <form method="post" action="ajax/valid ...

Guide on including a controller in an AngularJS directive

Does anyone know how to include a controller from one AngularJS directive into another directive? Here's an example of the code I have: var app = angular.module('shop', []). config(['$routeProvider', function ($routeProvider) { ...

Unable to properly load the Kendo Tree View based on the selected option from the combo box in the Kendo UI framework

I am encountering an issue where the Kendo treeview is not populating with different values based on a selection from the Kendo combo box. I have created a JSFiddle to showcase this problem. http://jsfiddle.net/UniqueID123/sample In the scenario provided ...

Is node-grunt-compass the equivalent of Heroku's Buildpack in the world of AWS CodeDeploy?

I am currently in the process of transitioning my nodejs app from Heroku to AWS CodeDeploy. On Heroku, we use the nodejs-compass-grunt buildpack for installing dependencies and running compass. I'm wondering if there is a similar solution on AWS Code ...

Creating PDF Files Using JSreport and Node.Js

Looking to create a PDF file on a Node.js server using HTML and CSS code? I recently set up JSreport on a local Nginx server and it appears to be the solution I was seeking: just input the HTML and CSS code, and voila - a PDF file is generated. https://i ...

Styling your Vuetify V-data-table with CSS

Struggling to add extra spacing between table rows, despite inspecting the page and verifying that my styles are not being overridden. Have read other Vuetify posts on applying CSS but still no luck. Any suggestions for why I can't style my table as d ...

Enhancing Your Website with Interactive Highlighting Tags

Looking at the following html: Let's see if we can <highlight data-id="10" data-comment="1"> focus on this part only </highlight> and ignore the rest My goal is to emphasize only the highlight section. I know how to emphasize a span ...

Is there a way to adjust the label elevation for a Material UI TextField component?

I am trying to enhance the appearance of a textfield label, but I am facing some challenges with my code. textStyle: { backgroundColor: '#1c1a1a', border: 0, borderRadius: 0, width: 400, height: 66, display: 'flex&a ...

The standard date format used in Javascript/Jquery programs

I have a kendo date picker set to display dates in the format "MM/dd/yyyy". I need to use jquery or javascript to ensure that the selected date is not in the future and is greater than '01/01/1900'. The problem I'm encountering is handling ...

Struggling with retrieving nested mongoose queries using populate in graphQL

I'm currently setting up a graphql backend using mongoose and I've encountered some difficulties understanding the conventions related to nested queries in mongoose. For instance, let's say I have the following field defined in the root quer ...

When updating a table in MySQL, use foreign key constraints to automatically populate another table with corresponding data

My situation involves working with two tables: users and login. Here's how they are structured: Create table users( id int not null, name varchar not null, email varchar not null, password varchar not null, entries int default 0, primary key(id) ...

Whenever I select a menu item, my intention is to make all the other main menu options disappear

I've been grappling with this issue for quite some time now. My goal is to hide all other main menus whenever I click on one. For instance, if I click on the Discord menu, I want all other menus to disappear except for the sub-menu. Check out the cod ...