HTML Element Style How to - Darken an image with :hover








Question

We would like to know how to darken an image with :hover.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.btn-img {<!--from  w ww. j a va 2s .  co  m-->
  display: block;
  margin: 0;
  padding: 0;
  border: 0;
  cursor: pointer;
  background-color: white;
  outline: 1px dotted blue;
}

.btn-img .img-wrap {
  margin: 10px;
}

.btn-img img {
  display: block;
}

.btn-img:hover .img-wrap {
  background-color: black;
}

.btn-img:hover img {
  opacity: 0.8;
}
</style>
</head>
<body>
  <button class="btn-img">
    <div class="img-wrap">
      <img
        src="http://placehold.it/200x200" />
    </div>
  </button>
</body>
</html>

The code above is rendered as follows: