I would like to use mix-blend-mode:darken on the image of the pineapple so that it will have a blue overlay and the darkened image on hover. The trouble is that I have an absolute positioned div on top of the image. I tried applying the blue background to the absolute positioned .overlay div and applying the blend mode to that as well as wrapping the image in a div and applying the blend mode to that div but because the overlay is positioned on top, the hover does not apply the darken style. Any help would be greatly appreciated, and please let me know if my details aren’t clear. I added how I would like the image to display on hover in a second div #result. Is it possible to do this with just CSS?
<style>
.container {
width: 300px;
position: static;
}
.darken img:hover {
mix-blend-mode: darken;
background-color: #007bff;
}
.overlay {
position: absolute;
top: 7px;
bottom: 0;
left: 7px;
right: 0;
height: 300px;
width: 300px;
opacity: 0;
transition: .5s ease;
background-color: #007bff;
cursor: pointer;
}
.overlay:hover {
opacity: .8;
}
.text {
color: #fff;
font-size: 18px;
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%,-50%);
-ms-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
text-align: center;
}
#result {
width: 300px;
position: relative;
background-color: #007bff;
}
#result img:hover {
mix-blend-mode: darken;
}
</style>
<div class="container">
<div class="darken">
<img src="https://www.w3schools.com/cssref/pineapple.jpg" alt="Pineapple" width="300" height="300">
</div>
<div class="overlay">
<span class="text">Click Here</span>
</div>
</div>
<p>Below is how I would like the image to display on hover but with the text on top:</p>
<div id="result">
<img src="https://www.w3schools.com/cssref/pineapple.jpg" alt="Pineapple" width="300" height="300">
</div>
3
Answers
::after
You could use an
::after
element.This would allow you to use the same
#result
example you provided, and achieve the effect very simply.Use another element for the blue overlay. Below I am relying on the container pseudo element.
Hi dear you can modify your CSS as follows:
In this solution, the blue overlay and darken blend mode are applied to the
.overlay
div inside the.darken
container. Themix-blend-mode: darken;
property is applied to the.overlay
itself, and the opacity changes on hover to achieve the effect you desire. The image inside the#result
div also has the darken blend mode applied directly on hover.Here’s the modified HTML and CSS:
This will achieve the desired overlay and darken blend mode effect on hover while keeping the text on top of the image.