Is it possible to overwrite an image with a background-image using CSS?

Can you replace an existing image with a new one using background-image?

I am interested in changing the image based on whether it is retina or not using CSS.

For example:

HTML:

<img id="foo" src="foo.png">

CSS:

#foo { background: url("bar.png") }

Answer №1

Personally, I find this strategy a bit complex. Allow me to propose a more efficient approach for handling high-quality images. A straightforward method that I have found effective (and in my opinion, the most optimal at this point) is to create the image in Photoshop at twice the necessary width and height, then save it with lower quality to minimize file size. In your HTML or CSS, specify the desired dimensions for the image. This way, the image will display beautifully on both standard and retina screens while still keeping the file size small. Plus, you only need to use one image, which is a huge plus.

Answer №2

Experiment with CSS3 :before selector:

img{
  display:none;
}
img:before{
  content: "";
  width: 20px;
  height: 20px;
  background: url("bar.png");
}

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

"In the shadows, the .toLowerCase() method of undefined is failing without making a sound, yet the code

It seems that letting things fail in the background is necessary for this example to work consistently. Is there a way to work around this issue? There are instances where I need to check a div with a data-attribute on certain pages and add a class if it ...

Utilizing the Mouse Hover feature to target various <div id> elements within a single Vue.js function

I have created two div IDs in an HTML file where I have specified the mouseover property. <div id="app"> <img class="img-responsive img-full" v-bind:src="imgData" @mouseover="imgData = imgData_1"> </div> <div id="app1"> <img cl ...

Using CSS transition on the height property does not produce the desired effect

I'm currently working on a menu interaction that activates on click. Here is the code snippet for reference: let btn = document.querySelector('.trigger'); let icons = document.querySelector('.icons'); let labels = document.query ...

Unable to Retrieve Modified Content with $().text();

In the process of developing an app, I am encountering a challenge where I need to transfer user input to another element after processing it. However, I am facing an issue with accessing the updated value of the <textarea> using .text(), as it only ...

Struggling to align a div container in the center

I'm having trouble centering my main container on the page. No matter what I try, it still seems to be floating to the left... I'm wrapping all my content in its own Div because I'm using a jQuery cycle background and I want to keep my conte ...

Boundaries on Maps: A guide to verifying addresses within a boundary

User provides address on the website. If the address falls within the defined boundary, it is marked as "Eligible". If outside the boundary, labeled as "Ineligible". Are there any existing widgets or code snippets available to achieve this functio ...

Fixed Sidebar Position Changes when Bootstrap Modal Closes

We are facing a peculiar issue in our React application involving a sidebar and a bootstrap modal. The problem arises when we click the "X" button in the modal, causing the sidebar to momentarily drop down the page before the modal closes. This strange be ...

I am unable to activate Wicket's onchange event

I've been trying to automate the population of three dropdown selection elements using a Chrome extension. I'm able to change the value of the first dropdown, but it doesn't trigger the necessary action that populates the second and third dr ...

Dynamically add a Font Awesome icon to the background of an input text field with React and react-fa

Utilizing ReactJs and react-fa to access Font Awesome icons, I am attempting to dynamically place one of the icons inside a text input. The following is my code snippet: import React, { Component } from 'react'; import PropTypes from &a ...

What is the most effective way to iterate through an array of objects and retrieve the results in a key-value format?

I am dealing with an array of objects that may seem a bit complex initially, but I will simplify it as much as possible. Each object in the array has properties like Engineering, Environment, and others, each containing a sub-object called radars. The rada ...

Verify if the loading spinner is currently visible on the tab

I've included a download button on my website for users to easily download a file. When the user clicks on the button, the page begins loading, as indicated by the tab: https://i.sstatic.net/9DkM8.png Is there a way to determine the state of this p ...

Bootstrap's innovative design for a data grid

I'm currently working on designing a Grid using bootstrap 3. https://i.sstatic.net/DZzcq.png My tools include html5, css, and bootstrap integrated with React.js. The main feature of my project is a data grid (specifically, a Fixed Data Table). ...

The autocomplete feature in Google Maps is not functioning properly when used within a modal

I've integrated the Google Maps API to enable autocomplete address functionality for an input box on my page. However, I'm encountering an issue where everything works fine on the page itself, but it doesn't work when the input box is placed ...

Firefox's handling of collapsing margins

Summary: Visit this codepen for an example that works smoothly on Chrome, but shows misalignment in Firefox. I have been working on a custom jQuery plugin that enhances a text input by adding a dropdown button on the left side. To ensure proper positionin ...

The customizing of font color with Fancytree's "extraClasses" feature is not being properly applied

In my web application, I have a tree that utilizes fancytree. Everything is functioning properly, except for the fact that I need to set the font color to red for certain nodes. I have implemented the use of extraClasses in my JSON, which is successfully a ...

Issue with click function not activating in Chrome when using Angular 6

I am facing an issue where the (click) function is not triggering in my select tag when I use Google Chrome, but it works fine in Mozilla. Below is my code: <div class="col-xl-4 col-lg-9"> <select formControlName="deptId" class="form-control ...

AJAX: Bringing in new content to display beneath the triggering page

Here is the code for my main page: <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <style type="text/css"> div { width:100%; ...

Send an array of data using AngularJS multipart form

I am struggling with an issue where I have an array of colors and sizes, along with other data such as product name and images. Despite being able to send image files and product names without any problems, attaching the color and size array leads to a 500 ...

Tips for setting up a due date alert system

As a novice, I'm attempting to develop a function that can switch between texts based on a set due date. The text displayed will change to "Expired" once the due date is reached, and it will remain as "Active" if the date hasn't been reached yet. ...

What location is most ideal for incorporating JavaScript/Ajax within a document?

Sorry for the seemingly simple question, but I would appreciate some clarification from the experts. When it comes to the three options of placing JavaScript - head, $(document).ready, or body, which would be the optimal location for ajax that heavily rel ...