Adjusting the transparency of an image when hovering over a button

Recently, I've encountered a situation where I have an image with a button on top. My current task is to write some CSS code that will adjust the opacity of the image when hovering over the button.

This is what I came up with:

.wwdimg:hover {
  opacity: 0.5;
}
<div>
  <img src="http://via.placeholder.com/350x150">
  <div><button class="wwdimg"> clickme </button></div>
</div>

However, after implementing this code, I noticed that instead of the image's opacity changing, it was actually the button's opacity that was affected.

Answer №1

To achieve the desired effect with your current markup, CSS alone is not sufficient. Some changes need to be made to the HTML structure, such as moving the image after the button.

.wwdimg:hover+img {
  opacity: 0.5;
}
<div>
  <button class="wwdimg">clickme</button>
  <img src="http://via.placeholder.com/350x150">
</div>

If altering the HTML is not an option, JavaScript will be needed to achieve the desired result.

var button = document.querySelector(".wwdimg");
var img = document.querySelector(".image");
button.onmouseover = function() {
  img.style.opacity = ".5";
}
button.onmouseout = function() {
  img.style.opacity = "1";
}
<div>
  <img class="image" src="http://via.placeholder.com/350x150">
  <div><button class="wwdimg">clickme</button></div>
</div>

Answer №2

Check out the following fiddle link to see it in action:
https://jsfiddle.net/Netven/akaqoaz0/17/

    $('.wwdimg').mouseover(function(){
        $('#img_holder').css("opacity", "0.2");
    });
      $('.wwdimg').mouseout(function(){
        $('#img_holder').css("opacity", "1");
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<img id="img_holder" width="200px" height="auto" src="https://images.dreamstream.com/prodds/prddsimg/OM_pasteIt8_2_2018_10_0_27495346.jpeg"> 
<div><button class="wwdimg"> clickme </button></div>
</div>

Answer №3

To achieve this effect, two conditions must be met: a) the button and the image must have the same parent element, and b) the image should appear after the button.

.image-button {
  display: inline-block;
  position: relative;
}

.image-button button {
  position: absolute;
  right: 5px;
  bottom: 5px;
  padding: 10px;
  z-index: 99;
}

.image-button img {
  display: block;
  position: relative;
  transition: opacity .4s ease-in-out;
}

.image-button button:hover + img {
  opacity: .5;
}
<div class="image-button">
  <button type="button">Hover me</button>
  <img src="http://placebear.com/400/180"/ >
</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

Resizing Images Responsively Using Bootstrap

I'm working on specifying image sizes for different screen sizes. I'm wondering if there are any specific Bootstrap classes that only work at certain screen sizes. If so, it would be great because then I could use something like <img class = " ...

What is the reason for this only functioning with the "!important" declaration?

Can anyone help me troubleshoot this code? It seems to only work with the !important rule, which I prefer not to use. I believe there must be a way to achieve the desired outcome without relying on it. What's intriguing is that the CSS line in questi ...

The execution of JQuery/Javascript is restricted to only the initial condition within a Visualforce page utilizing the apex:outputpanel tag

After using only JavaScript for some time, I decided to try out jQuery. However, I'm facing an issue with executing a jQuery function. It seems that only the first condition in my code (the first IF) is being executed, while the second one (the second ...

Turn off the annoying right-click context menu in Firefox

I'm currently working on an HTML 5 game that requires the player to use right click for control. To disable the right click context menu, I have used the following code: <body oncontextmenu="return(false);"> However, I discovered that in Fire ...

Styling text using SVG in HTML

I'm trying to underline a text element in SVG using the text-decoration attribute, but I'm running into an issue with the color of the line not changing. Here is the code snippet I am working with: <svg id="svg" viewBox="0 0 300 ...

Exploring HTML elements using CSS Selectors in Scrapy with Python

Currently, I am working on a personal project to create a web crawler that extracts information from Yahoo Finance. However, I have encountered an issue while trying to pull a specific value from the analysis page on Yahoo Finance. The complexity of the HT ...

When attempting to specify the path in the angular.json file, Angular encounters difficulty accessing Bootstrap from the node_modules directory

I have been attempting to integrate Bootstrap into my Angular project. Firstly, I used npm install --save bootstrap to add Bootstrap to my project. Following that, I installed jQuery as well. I then specified the path for Bootstrap. Displayed below is an ...

MVC.NET: Offering User-Friendly Loading of Initial x Items with an Option to Load More

What would be the optimal approach to efficiently load only the initial 25 elements from an IEnumerable within an ASP.NET MVC Index view? Upon employing scaffolding, a controller and views have been constructed. Within the index page, there is a creation ...

Positioning of the calendar icon in the date input field is problematic when in RTL direction on Chrome and various other

My date input field is causing a problem on a Hebrew (RTL) website. Strangely, in Firefox, the calendar icon displays correctly on the left side, but in Chrome and other browsers, it does not. The code I am using is: <input type="date" class= ...

Effective Ways to Redirect During or After Executing the onClick Function of Button

I am currently working on implementing a feature for my Next.js website. The functionality involves allowing users to create a new group by clicking a button, and then being redirected to an "Invite members" page with the auto-generated group_id included i ...

Firefox is not rendering HTML5 elements properly

While my website performs well in Chrome and Safari, there seems to be an issue with how elements are displayed in Firefox or IE. Some elements are being pushed to the right, and I am unsure of how to fix this problem. You can view the site here To aid i ...

Using Highcharts to showcase a marker icon for a specific value

I am looking to add individual markers to my Highcharts graph. I want the outcome to resemble the example here: http://jsfiddle.net/m1mqv4z2/ { x: Date.UTC(2014, 6, 31) y: 26.5, marker: { symbol: 'u ...

The ready/load frame event fails to trigger when a new frame URL is loaded

I currently have a website that uses frames, allowing users to change the content of a frame with a simple link that loads the new page in that frame. My goal is to determine when this loading process is complete. $(top.frames['mainFrame'][&a ...

Changing pages in Django with a single click of a line: A step-by-step guide

As a beginner in Django, I am looking to create views that change when a line in an HTML tag is clicked. The idea is to open a new view named after the hash inside the <code>Hash tag. {% load staticfiles %} <!DOCTYPE html> <html lang="fr"&g ...

A new WordPress plugin that includes a custom designed stylesheet featuring a hidden display property within a specific

After reloading the page, my plugin displays a constructed stylesheet. However, when I access it for the first time or reload with ctrl + f5, the constructed stylesheet does not appear. I have meticulously searched through all of the plugin code looking f ...

Resizable item within a text box (similar to an HTML editing tool)

After extensive searching online, I have not been able to find a solution. What I need is a draggable input element within a textarea in a text-editable div. The draggable textarea should be integrated seamlessly into the text, and upon submitting, it shou ...

What could be causing the float left and right to not function properly in this situation?

Having trouble positioning a search box on the left and arrows on the right side of a container. The float property isn't working as expected and the arrows are too close to the search box. I've also tried using left:0px and right:0px but no luck ...

Web server experiencing issues with loading scripts and CSS files

After successfully building my project using CodeIgniter on localhost, I encountered an issue when trying to run it on a webhost. The site was functional but the design elements such as scripts and stylesheets were not loading properly. Before uploading t ...

Value of PHP checkboxes

Similar Question: Submitting an HTML form with unchecked checkboxes I am attempting to send some information to the database. I am struggling to send the checkbox value in the form to the database using the following code snippet: <input type=&apo ...

Encountered a style property reading error

I am currently using nginx as my server and Chrome as the browser. I am attempting to change the background color of a div element. The JavaScript code works fine in a fiddle, but when I implement it into the complete markup, it does not work. When inspec ...