-1

When I use the opacity on image hover. It only getting lighter or dimmer but not darker.

I have tried to increase the number of opacity to make it darker, but not success. Below is the code block that I have tried to make the image become darker on hover:

<style>
.category-product img {
    display: block;
    margin-left: auto;
    margin-right: auto
}

.category-product img:hover {
    opacity: 0.5;   
}
</style>
<div class="category-product">
    <img src="hinh/paris.jpg" data-img="product-image-1">
</div>

Image of opacity applied on my current try enter image description here I expect the image will become darker on hover.

Expected result

enter image description here

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62

3 Answers3

1

They way to use opacity is by the other side you are trying. 1 opacity means that element is full opaque, "opacity: .5" means half opacity, so you will see you element half transparent and the color of the element behind the image and thats your result.

A quick, better and easy way to get that effect is using filter propierties of css.

<style>
.category-product img {
    display: block;
    margin-left: auto;
    margin-right: auto;
    transition: all .3s;/* I put this to get smooth transition */
}

// Then at hover, you can do a filter: brightness and set a lower value

.category-product img:hover {
   filter: brightness(0.4);
}
</style>

<div class="category-product">
    <img src="http://lorempixel.com/output/sports-q-c-640-480-7.jpg" data-img="product-image-1">
</div>

Here is the example at Codepen:

https://codepen.io/ChemaAlfonso/pen/ROJNev

hope it helps you

Chema
  • 365
  • 3
  • 6
0

Style opacity = 0.5 will make the image become dimmer but not darker. To make the picture become darker on hover, we set the background div containing the picture to dark. Thus, when hovered the picture will dimmer and with the black background, it will make the picture looks darker.

<style>
.category-product {
    background: black;
}

.category-product img {
    display: block;
    margin-left: auto;
    margin-right: auto
}

.category-product img:hover {
    opacity: 0.5;   
}
</style>
<div class="category-product">
    <img src="hinh/paris.jpg" data-img="product-image-1">
</div>

In the code above, I just set the containing div with black background.

0

On mouse hover use background-color: rgba(black, 0.5); instead of opacity: 0.5;

<style>
    .category-product img {
        display: block;
        margin-left: auto;
        margin-right: auto
    }

    .category-product img:hover {
       background-color: rgba(black, 0.5);  
    }
    </style>
    <div class="category-product">
        <img src="hinh/paris.jpg" data-img="product-image-1">
    </div>
Prakash Mhasavekar
  • 574
  • 1
  • 5
  • 16