Trouble with concealing <img src> and showing background image on top

I am struggling to hide the <img src="/"> and layer the background image on a 480px media version.

Despite attempting to implement the solution provided in this Stack Overflow thread, I have not been successful in achieving the desired outcome.

For a visual representation, check out the JSFIDDLE.

Here is the HTML code snippet:

<div class="j_img-overlay">
    <img src="https://image.flaticon.com/teams/slug/freepik.jpg">
</div>

And here is the corresponding CSS code:

.j_img-overlay {
   width: 100px;
   height: 100px;
}

.j_img-overlay img {
   background-size: auto;
   background: url(http://icons.iconarchive.com/icons/graphicloads/100-flat/256/home-icon.png) no-repeat;
   position: relative;
   box-sizing: border-box;
   width: 100px;
   max-width: 100px;
   z-index: 100;
   }

Answer №1

First and foremost, it is important to note that the <img/> element should not be used to display a background-image. The only way to adjust all background style options at once is by using the background property. Unfortunately, the background-image property can only accept image URLs and not repeating values.

For more information on the Background Image and Background CSS properties, click here.


In order to properly implement an overlay, you can use pseudo elements (such as :after, :before) with absolute positioning to fill the container of the image element. It is necessary to use relative positioning for the container to prevent any overflow of the pseudo element (which has absolute positioning).

Learn more about Pseudo Elements - CSS here.

See a working example below:

.j_img-overlay {
  position: relative;
  width: 100px;
}
.j_img-overlay::after {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url(http://icons.iconarchive.com/icons/graphicloads/100-flat/256/home-icon.png) no-repeat;
  background-size: cover;
}

.j_img-overlay img {
  display: block;
  width: 100%;
}
<div class="j_img-overlay">
  <img src="https://image.flaticon.com/teams/slug/freepik.jpg">
</div>

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

What is the best way to duplicate the table header?

I'm looking to have the table header repeat twice using ng-repeat, like this: a b | a b | Instead of aa | bb. Currently, my code only displays a b | <table class="table table-striped table-bordered"> <thead> ...

Step by step guide on how to connect a stylesheet in CSS

Hey there, I recently attempted to link a style sheet in my HTML for the first time. Unfortunately, I encountered an issue where it didn't work. Here's the code I used: <link href="css/StyleSheet.css" rel="stylesheet" type="text/css" /> W ...

What is the procedure for automatically playing the next audio track in HTML5 after the current one finishes playing

When trying to play a single MP3 file, the code below is designed to skip to a specific part of the track and then start playing from that position. However, despite the cursor moving to the correct spot in the MP3, it fails to play upon clicking the Sta ...

Showing database table entry that corresponds to WordPress post ID

Seeking guidance on a project. I have created a website for a volleyball club using Wordpress and am collecting player profile data through a Google form/spreadsheet, which I then save as a .csv file and import into a custom table named 'profiles&apos ...

Tips for preloading an ENTIRE webpage

I am looking for a way to preload an entire web page using JavaScript in order to have it cached in the user's browser. While I know how to preload images with JS, my goal is to also preload the entire page itself. For example, on my website, there ...

How to position an empty Div next to images

I am currently working on a project to familiarize myself with Japanese Hiagana. I plan to incorporate more images and eventually sound into the post. To reduce the number of images used, I attempted to replace empty .pngs with a div element. However, I am ...

"Shuffle the Order of Divs Each Time the Page Loads

I have multiple divs labeled as "gallerycard". I want them to appear in a different random order each time the page is loaded. <div id="gallerycard"> <div id="name">Akulina</div> <div id="info">N/A</div> </div> ...

Widget for navigating through Youtube videos

I am currently working on creating a widget that allows users to navigate a YouTube video using buttons. For instance, if the video is of a car race, there would be buttons labeled Lap 1, Lap 2, and so forth. My idea involves adding an extension to the vi ...

Update the content of a text area with AJAX requests

Just starting out with AJAX, I'm looking to requery or perform a select *from on my database when a button is clicked in order to change the content of a textarea. How can I achieve this using AJAX? Below is the code snippet I have so far: <text ...

CSS is not referred to as animation

Every time we hit a key, the dinosaur receives a jump class that should activate an animation. Unfortunately, the animation does not play. animation not functioning <!DOCTYPE html> <html lang="en"> <head> <meta c ...

Is it possible to adjust the default zoom level of my website by utilizing HTML or CSS?

After creating my website with a 50% zoom setting, I've encountered an issue where it appears too zoomed in for users whose browser default is set at 100%. How can I fix this problem? I attempted to use *{zoom:50%} in CSS to set the default zoom leve ...

Issues with jQueryUI sortable causing flickering and erratic movement while attempting to reorder and position items within the list

When using jQueryUI sortable with a long list of items, I encounter an issue where the items flicker and jump around the screen as I try to change their order by dragging them. This makes it nearly impossible to organize the items effectively. It seems li ...

Is there a way to convert a URL into a user-friendly json format displayed on a web page

Can someone please help me figure out the proper way to convert this URL into a JSON format that is easily readable using jQuery on an HTML page? ...

Limits in exporting data from an html table to a pdf document

The output on the page only displays 17 out of 60+ rows of data. I've searched online for a javascript function to help with this, but unfortunately, I couldn't find one. Here is a sample code snippet: <script type="text/javascript"> ...

Switch between CSS and jQuery styling

Here is a code snippet I'm using to toggle the visibility of content: $(function(){ $('#slider').css('display',''); $('#hideslider').click(function(){ $('#slider').slideToggle('slow&apos ...

The PHP form in an HTML file fails to function properly if multiple forms are included

When working with multiple forms in a PHP file that are called using the 'include' command, I encountered an issue where placing the include command before the forms on the HTML page made it work. However, I needed to call the include command bel ...

What is the best way to place a semi-transparent black overlay on an image?

<html> <head> <style> #contain_main {background-color:black; width:100%; height:auto;} #main {width:100%; height:700px; background-image:url("https://www.sappun.co.kr/shopimages ...

CSS - Customizing the appearance of consecutive child divs

I'm struggling with adjusting the margin between two child divs when they follow each other. The current margin is set at 3rem, but I want it to be 1rem if both child containers have the class "narrow": Is there a way to achieve this without changing ...

Liquid Static Content

What is needed to fix the fluid-fixed layout issue? HTML: <div class="header"> <div class="title">Title</div> <div class="options">Opt</div> </div> CSS: .header{margin:0;padding:0;} .title{margin-right:50px; ...

How can we extract text from a div containing a Mark tag using Selenium WebDriver?

When using Selenium Webdriver, how can I extract the complete text from a div with a Mark tag? I am trying to confirm if the word 'correctly' appears within it. Below is an example of HTML code: <div class="faq-node-body"> blah ...