I want to display the image on mouse-over and keep the border.
However, when I add opacity, the border disappears as well. How can I maintain the border while displaying the image on mouse-over?"
Here is my code:
<div class="finishedprojects">
<img src="https://shorturl.at/bR589">
</div>
.finishedprojects img {
width: 100%;
max-width: 350px;
height: 250px;
object-fit: cover;
border: solid yellow 1em;
background-color: black;
opacity:0;
}
.finishedprojects img:hover {
cursor: pointer;
opacity: 1;
}
I tryed.finishedprojects img {
border: solid yellow 1em}
But it doesn’t work.
3
Answers
You can use the ::before pseudo-element to create a border on the image container that will not be affected by the opacity change on hover.
You only have to set
width: max-content;
in your container class.finishedprojects
and also move the border styling there. See my snippet:If you also want to have a black background if no image is displayed, move the background property
background-color: black;
from.finishedprojects img
class to the.finishedprojects
class.To keep the border visible when the image is displayed on mouse-over with opacity, you can wrap the image inside another div and apply the border to that div instead of the image