Position one image directly on top of another without any transparency

I am looking to create a layout where one image is displayed above another image. When the user hovers over the below image, the opacity of that image should change while the above image remains fully visible.

However, currently both images have reduced opacity when the user hovers over the lower image.

HTML :

<div class="contents">
            <div align="center">
                <p align="right">Sort by <select name=""><option value="">----- Select -----</option></select></p>
                <div class="items">
                    <p><img src="https://i.kinja-img.com/gawker-media/image/upload/cg0vk7wvosjaxlji7jhr.jpg" alt="" /></p>
                    <p>Microsoft Lumia 950</p>
                    <p><span class="strike offer_price">Rs.1000/-</span> <b>Rs.500/-</b></p>
                    <hr/>
                    <p><input type="checkbox" /> Add to compare</p>
                    <div class="offer_per">
                        <p><b>50% Off</b></p>
                    </div>
                    <div class="add_cart">
                        <p align="center"><img src="http://png-1.findicons.com/files/icons/1672/mono/32/shoppingcart.png" alt="" /></p>
                    </div>
                </div>
            </div>
        </div>

CSS :

.contents .items {
    width:24%;
    height:auto;
    display:inline-block;
    box-shadow: 0 0 2px 2px #CCCCCC;
    margin:10px 0 10px 5px;
    position:relative;
}
.contents .items:hover {
    box-shadow: 0 0 2px 2px #FD6123;
    opacity: 0.5;
    filter: alpha(opacity=50);
}
.contents .items p img {
    width:100%;
}
.contents .items .offer_per {
    position:absolute;
    top:0;
    background:#52C8D2;
    padding:5px;
    color:#FFFFFF;
    font-size:12px;
}
.contents .items .add_cart {
    left: 0;
    position: absolute;
    right: 0;
    top: 50%;
    display:none;
}
.contents .items .add_cart p img {
    width:auto;
}

jquery :

    $('.items').mouseenter(function(e) {
        $('.add_cart').show();
    });

    $('.items').mouseleave(function(e) {
        $('.add_cart').hide();
    });

I've showcased my code on this JSFiddle link, feel free to take a look!

Answer №1

violin

https://jsfiddle.net/MusicMaker/kcngmvcj/3/

JQuery Solution

The initial code was designed so that when a single element with the class of .items was clicked, all elements with the class of .add_cart on the page were toggled while maintaining opacity only for the hovered .items (as per the CSS).

I made the following changes:

    $('.items').mouseenter(function(e) {
         $(this).find('.add_cart').show();
    });

    $('.items').mouseleave(function(e) {
        $(this).find('.add_cart').hide();
    });

This adjustment allows the functionality to work on multiple blocks with the class of .items.

CSS Update

Replace the existing CSS with the following:

.contents .items:hover {
    box-shadow: 0 0 2px 2px #FD6123;
}
.contents .items:hover img {
    opacity: 0.5;
    filter: alpha(opacity=50);
}
.contents .items:hover .add_cart img {
    opacity: 1;
    filter: alpha(opacity=100);
}

The CSS modification targets specific elements within .items and separates the red box shadow border effect from the opacity adjustments. This solution provides more granular control over the styling.

There are various approaches to achieving this functionality, including CSS-only options.

Answer №2

Check out the working fiddle

As recommended by @Sougata in the comment section, consider moving the image outside of the items div and adjusting its position using the CSS attribute margin-top:

<div class="contents">
    <div align="center">
        <p align="right">Sort by <select name=""><option value="">----- Select -----</option></select></p>
        <div class="items">
          <p><img src="https://i.kinja-img.com/gawker-media/image/upload/cg0vk7wvosjaxlji7jhr.jpg" alt="" /></p>
          <p>Microsoft Lumia 950</p>
          <p><span class="strike offer_price">Rs.1000/-</span> <b>Rs.500/-</b></p>
          <hr/>
          <p><input type="checkbox" /> Add to compare</p>
          <div class="offer_per">
            <p><b>50% Off</b></p>
          </div>
        </div>
    </div>

    <div class="add_cart" style='display: none'>
      <p align="center"><img src="http://png-1.findicons.com/files/icons/1672/mono/32/shoppingcart.png" alt="" /></p>
    </div>
</div>

Adjust the margin-top property for the .add_cart class:

.add_cart{
  margin-top: -150px;    
}

Hopefully, this solution proves helpful.


$('.items').mouseenter(function(e) {
  $('.add_cart').show();
});

$('.items').mouseleave(function(e) {
  $('.add_cart').hide();
});
.contents .items {
width:24%;
height:auto;
display:inline-block;
box-shadow: 0 0 2px 2px #CCCCCC;
margin:10px 0 10px 5px;
position:relative;
}
.contents .items:hover {
box-shadow: 0 0 2px 2px #FD6123;
opacity: 0.5;
    filter: alpha(opacity=50);
}
.contents .items p img {
width:100%;
}
.contents .items .offer_per {
position:absolute;
top:0;
background:#52C8D2;
padding:5px;
color:#FFFFFF;
font-size:12px;
}
.contents .items .add_cart {
left: 0;
position: absolute;
right: 0;
top: 50%;
display:none;
}
.contents .items .add_cart p img {
width:auto;
}

      .add_cart{
         margin-top: -150px;    
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="contents">
    <div align="center">
        <p align="right">Sort by <select name=""><option value="">----- Select -----</option></select></p>
        <div class="items">
          <p><img src="https://i.kinja-img.com/gawker-media/image/upload/cg0vk7wvosjaxlji7jhr.jpg" alt="" /></p>
          <p>Microsoft Lumia 950</p>
          <p><span class="strike offer_price">Rs.1000/-</span> <b>Rs.500/-</b></p>
          <hr/>
          <p><input type="checkbox" /> Add to compare</p>
          <div class="offer_per">
            <p><b>50% Off</b></p>
          </div>
        </div>
    </div>

    <div class="add_cart" style='display: none'>
      <p align="center"><img src="http://png-1.findicons.com/files/icons/1672/mono/32/shoppingcart.png" alt="" /></p>
    </div>
</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

Looping through alert items

Below is the code snippet I am working with: <tr ng-repeat="sce in users"> <td> <a href="/test/delete?id={{sce.id}}" onclick="return confirm('You really want to delete'+ {{sce.name}} + 'from list');" > ...

Having difficulty getting the fixed top and left side to work on this scrollable table

Currently, I am working on a project where I have designed a page layout consisting of a header and a table. My goal is to create a fixed top row in the table as well as a fixed left column when scrolling horizontally. While I have successfully implemented ...

Enhance the design of a div with a stylish border using CSS

Thank you very much for your help, I am looking to design a progress div with borders using HTML and CSS. The design should resemble the image provided, but with rounded corners. https://i.sstatic.net/6HMGS.png ...

When incorporating SQL statements into JavaScript code, it fails to function properly if the NVARCHAR attribute contains a line break

I am currently utilizing a PHP app generator (Scriptcase if that is relevant), while developing a webpage that involves a mix of vanilla JavaScript and SQL queries. After encountering an issue, I have finally identified the root cause. The problem arises ...

Insert a line below the active tab button

I made three buttons for three different tabs. Is there a way to include an underline below the active tab button (under the red "zone")? I'm aiming for something similar to mat-tab. Any assistance is appreciated! DEMO Code viewMode = 'tab ...

Incorporating an item into an array based on a specific condition

I am looking to dynamically add or remove an object within an array based on a certain condition. The goal is to exclude the object completely if the condition is not met, while leaving the rest of the objects intact. Consider the following scenario: const ...

Removing the `&` sign and any text that appears after it from a URL is a simple process that

Help Needed with URL Manipulation https://myApp-ajj.com/sp?id=cat_item&sys_id=cf9f149cdbd25f00d080591e5e961920&sys_id1=cf9f149cdbd25f00d080591e5e961920&sysp_Id=a691acd9dbdf1bc0e9619fb&sysparm_CloneTable=sc_request&sysparm_CloneTable=sc ...

How can I prevent displaying server-side validation messages if the client-side validation message is already being shown?

In my webpage, I am dealing with client side validation failures displayed within divs. The text inside these divs can vary from one to another. <div class="validation-failure">At least one of the above 2 fields needs to be entered</div> Addi ...

Data-id attribute fails to appear when applied to an element hidden with display:none property

Creating a notification popup feature using bootstrap popover. Each notification is represented by a div with the same classes, but different text content. <% Loop through notifications and create a div for each %> <div class="media n-media n ...

I am having an issue where my Bootstrap 4 col-sm-6 rows are not properly stacking on mobile devices

Currently facing issues with Bootstrap 4 as my columns are not stacking on top of each other in mobile view. I have two col-sm-6 within each row. Any guidance on how to resolve this would be greatly appreciated, thank you! Feel free to visit the website a ...

Tips on incorporating a fresh item into a expansive tree view using a recurring function or action - specifically geared towards the MUI React JS Tree View component

I am looking to implement a function or action that allows for the dynamic addition of new items to a tree view in MUI. The goal is to be able to click on a tree item and have it add another item, repeating this process as needed. <TreeView ...

"Ensure div remains at the bottom of the page even while

In order to implement a feature where the menu sticks to the top when scrolling down, you can use the following JS code. You can view a live example of this functionality on this Plunker by widening the preview window to see the columns side by side. wi ...

Loading logos dynamically using Jquery upon selection of an option

In the form I created, there is a dropdown logo selection at the bottom. The options in the dropdown are populated from folders in the root directory using the following code: $dirs = glob('*', GLOB_ONLYDIR); Each of these directories contain ...

Separate modules in the Webpack.mix.js file don't produce any output files in the public folder

I've recently been tackling a Laravel project with an extensive webpack.mix.js file residing in the root directory, boasting nearly 5000 lines of code. In an effort to enhance organization and maintainability, I've opted to break it down into ind ...

Is it possible to extract information from a website if the body or row does not contain any attributes?

As a beginner in the world of coding and this website, I kindly ask for some patience with me. I have successfully created code to scrape a particular website, extract data, and store it in a MySQL database. Everything is working smoothly. Now, my goal i ...

What is the best way to incorporate dynamic data into dynamic HTML in Angular 1?

I am trying to achieve a scenario similar to this: https://i.sstatic.net/LLTcK.png After researching, I found out about $compile, $trustAsHtml, and finally directives. However, in $compile and $trustAsHtml, I can only append static templates or plain HTM ...

Transforming HTML tables into JSON using JQuery

My table contains rows with specific classes, and I need to convert it into JSON for server-side usage. Below is the content of the table: <table class="tableContent"> <thead> <tr class="header"> <th class="col1">RoleNa ...

Combine the points of individual letters in Scrabble using jQuery

Hello, I'm currently working on creating a Scrabble game. My goal is to assign a value to each letter and then calculate the sum of these values when the input box changes. After some research, I was able to find information on how to add numbers on ...

What is the best way to ensure that my search box is responsive in HTML and CSS while also horizontally centering the search_box div?

Question 1: I need assistance in making this search box responsive. Every time I try to adjust the screen width, the button ends up below the input field. How can I resolve this issue? Question 2: Is there a way to horizontally center the div class=" ...

"Troubleshooting: Why isn't the AJAX Live Search displaying any

When I try using the live search page in livesearch.php by typing entries into the input box, I am not seeing any results displayed below it. Below is the HTML code and script in search.php. search.php <!DOCTYPE HTML> <html> <he ...