Numerous occurrences of identical data URL image

Is there a way to re-instantiate an already declared base64 data URL image without re-inserting the base64 code on the same page, preferably using CSS?

This is what I attempted:

<html><head>
    <style type="text/css">
        img.wink { width:15px; height:15px;
            src:"data:image/.gif;base64,R0lGODlhDwAPALMMAP/qAEVFRQAAAP/OAP/JAP6dAP+0AP/+k//9E///x//lAP//6wAAAAAAAAAAAAAAACH5BAEAAAwALAAAAAAPAA8AAARXkEkZap2Y1ZXOGRcWcAgCnEMRTEEnnDCQrtrxxjCoJSZw+y+CKnDo/WAEQ+WAwyUrvWZQGRg0TwKFcFX1xYI6zWCgEJizhBlrTGi31aKAYW4YZlgW2iQCADs";
        }
    </style>
</head><body>
    <h1>Hello</h1>
    <img class="wink"/>, and just to test my sanity <img width="15px" height="15px" src="data:image/.gif;base64,R0lGODlhDwAPALMMAP/qAEVFRQAAAP/OAP/JAP6dAP+0AP/+k//9E///x//lAP//6wAAAAAAAAAAAAAAACH5BAEAAAwALAAAAAAPAA8AAARXkEkZap2Y1ZXOGRcWcAgCnEMRTEEnnDCQrtrxxjCoJSZw+y+CKnDo/WAEQ+WAwyUrvWZQGRg0TwKFcFX1xYI6zWCgEJizhBlrTGi31aKAYW4YZlgW2iQCADs" alt=";)"/>.
</body></html>

Answer №1

src is not a valid CSS property. You must utilize content instead...

img.wink {
  content: url(data:image/gif;base64,BLAH_BLAH_BLAH);
  height: 15px;
  width: 15px;
}

See it in action: Live Demo

Answer №2

The CSS provided is inaccurate, you should set the data as the URL in the background-image property. This way, you will be able to reference it by class.

<html>
<head>
    <style type="text/css">
        div.wink  
        {
            width:15px; 
            height:15px;
            background-image: url('data:image/.gif;base64,R0lGODlhDwAPALMMAP/qAEVFRQAAAP/OAP/JAP6dAP+0AP/+k//9E///x//lAP//6wAAAAAAAAAAAAAAACH5BAEAAAwALAAAAAAPAA8AAARXkEkZap2Y1ZXOGRcWcAgCnEMRTEEnnDCQrtrxxjCoJSZw+y+CKnDo/WAEQ+WAwyUrvWZQGRg0TwKFcFX1xYI6zWCgEJizhBlrTGi31aKAYW4YZlgW2iQCADs=');
        }
    </style>
</head>
<body>
    <h1>Hello</h1>
    <div class="wink"></div>
    <br/>
    and just to test my sanity 
    <div class="wink"></div>.
</body>
</html>

Answer №3

Give this a shot:

<html><head>
    <style type="text/css>
        div.wrapper  {
            background-image: url(data:image/.gif;base64,R0lGODlhDwAPALMMAP/qAEVFRQAAAP/OAP/JAP6dAP+0AP/+k//9E///x//lAP//6wAAAAAAAAAAAAAAACH5BAEAAAwALAAAAAAPAA8AAARXkEkZap2Y1ZXOGRcWcAgCnEMRTEEnnDCQrtrxxjCoJSZw+y+CKnDo/WAEQ+WAwyUrvWZQGRg0TwKFcFX1xYI6zWCgEJizhBlrTGi31aKAYW4YZlgW2iQCADs=);
            width:15px; height:15px;
        }
    </style>
</head><body>
    <h1>Greetings</h1>
    <div class="wrapper">
    <br/>
</body></html>

Internet Explorer 8, Firefox 2 and 3, Safari, Mobile Safari (iPhone browsers), and Google Chrome all offer support for embedding binary image data in CSS files. However, this feature is not supported in IE 6 and 7.

For further information, check out:

Answer №4

When adding images to a document, consider using base64 URLs as backgrounds in CSS if the images do not add semantic value.

If you need to use the base64 string on the server-side, store it in a variable and then insert it into the src attribute where necessary.

If server-side languages are not an option, JavaScript can be used, but it's advisable not to rely solely on scripts for content accessibility. You can use a snippet like this within a domready event:

var img = document.getElementsByTagName('img'),
    len = img.length;
while (--len) { 
    if (-1 < img[len].className.indexOf('wink'))
        img[len].src = 'data:image/.gif;base64,..."; 
}

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

Angular UI grid: Arranging numbers in a straight line at the decimal point

I am interested in aligning decimal numbers in Angular UI Grid as shown below. 11.293 .89 233424 .34345 I have considered different approaches such as using a cell template with aligned divs or transparent 0s. Has anyone successfully imp ...

Adding HTML information into a MySQL database

I have developed an HTML page that enables users to input a username and password. Upon clicking the login button, I need to store this information in a MySQL database. However, when I test it by specifying the website's IP address, entering the cred ...

The Jquery animate function is not compatible with the transform property

I am facing an issue with the Jquery animate function. Despite trying various solutions, I have been unable to get it to work. Below is the HTML code that I have used: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...

Transferring the "key" and "values" data from a JSON object to a perl script

I am currently working on developing a shopping cart application. All the items stored in the shopping cart are kept within a JavaScript object called Cart, with data structured like {"sku"=quantity}. For instance: Cart={"5x123"=1,"5x125"=3} At this ...

What is the best way to position an element to the right of other elements within a div?

My goal is to style my button like this : desired look However, I'm unsure of the necessary steps to achieve this. Here is my HTML code : <div class="content" *ngIf="loading==false" > <div class="product" ...

Utilizing a dropdown list in HTML to dynamically change images

On my HTML page, I have implemented a dropdown list box. My objective is to change an image and update a label based on the selection made from the dropdown list box. I already have an array called 'temp' which represents the number of items. The ...

Improving Vue Component on Navigation

I am facing an issue with my navbar where I have two versions. Version 1 features a white text color, while Version 2 has black text. The need for two versions arises due to some pages having a white background, hence the requirement for black text. Bot ...

Learn how to incorporate PHP7 code into your HTML file through the use of .htaccess configurations

I have a Linux server with PHP 7.* installed. I want to embed PHP code into HTML files, but currently it just renders the PHP code on the webpage. I've tried adding the following code to my .htaccess file, but it doesn't seem to be working: AddH ...

Creating a webpage with a form is a simple process that involves utilizing both PHP and HTML

Just like creating a post in WordPress or asking a question on this website, I am curious about how to create a page simply by filling out a form. I understand how to build an HTML form, manage data in a database, and have some knowledge of PHP. However, ...

The anchor tag fails to function properly when nested within an image element inside an animated container

As I am currently developing a solar system webpage, my aim is to redirect users to a specific page when they click on a planet. Unfortunately, the anchor tag does not seem to be functioning as intended. I attempted to animate the anchor tag with an image, ...

Incorporating Tailwind CSS into Vue transitions reveals an issue where the fade out effect

I'm working with Tailwind CSS and Vue.js to create a modal component. Since Tailwind doesn't natively support Vue 2, I had to implement the transitions myself. You can check out the desired effect here. Below is the code snippet: <template> ...

Shift div position when clicked and employ jQuery animations

I have been attempting to add animation to a DIV element by using jQuery. The goal is to move the DIV from the center to the left when it is clicked, but I am encountering some issues. The parent div has a position of "relative", and my initial idea was to ...

The data-tooltip feature displays information as [Object object]

There seems to be an issue with displaying text in the data-tooltip. Instead of showing the intended message, it is displaying [Object object]. import React from "react"; import { FormattedMessage } from "react-intl"; const translate = (id, value={}) = ...

Is there a way to display my WhatsApp number, email, and location using icons?

[Apologies for my English not being perfect] I came across this image on and I'm curious about how to add similar WhatsApp, email, and location buttons on my own website. Can anyone assist me with this? Initially, I mistook these elements for images ...

Combining Bootstrap Vue: utilizing class names alongside HTML tags

(Bootstrap-Vue 2.0, Vue.js 2.5) Is it possible to combine traditional CSS Bootstrap 4 classes with Bootstrap-Vue? For example, can I use the following code snippet: <section id="introduction"> <b-container class="h-100"> & ...

Stop Ag grid from automatically extending to the entire width of the screen

I am encountering an issue where my table scales to the full width available even when the content is limited. Take a look at the demo here: "https://plnkr.co/edit/6L8bTAwkEV6R6Be4M1Qm?p=preview" I have attempted to address this problem by settin ...

Achieving uniform cell height distribution in Foundation XY-grid using flexbox

When utilizing the Foundation XY-grid, I encountered an issue with distributing cell heights evenly within a nested grid-y inside a cell using the .auto class on the parent cell. In a scenario with 2 cells in a grid-y, the desired outcome is 50%/50%. This ...

Problem with <meta> tag occurring when initial-scale is adjusted

Initially, in the index.html file: <meta name="viewport" content="width=device-width, initial-scale=1" /> I decided to modify it to: <meta name="viewport" content="width=device-width, initial-scale=2" /> ...

Adding Jquery content to HTML templates using Jinja2: A Simple Guide

Looking at my HTML code Base.html <title> {% block title %}{% endblock %} </title> </head> <body> <nav class="navbar navbar-expand-lg navbar-light bg-light"> <div class="collapse na ...