SW2702
SW2702

Reputation: 199

Adding overlay to image

Hello I would like to add an overlay to an image, I have created a class with the overlay and expected it to repeat over the image but it does not work.

I have added the html and css below and included the images that I am using

.overlay-pattern {
  content: "";
  background: url('//unsplash.it/400/300') no-repeat;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: 2;
}
<img src="//unsplash.it/400/300" class="overlay-pattern">

image overlay

image to add overlay

Upvotes: 2

Views: 645

Answers (3)

Prabhat-VS
Prabhat-VS

Reputation: 1530

Try this,

<style>
.container {
    position: relative;
    width: 50%;
}

.image {
  opacity: 1;
  display: block;
  width: 100%;
  height: auto;
  transition: .5s ease;
  backface-visibility: hidden;
}

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

.container:hover .image {
  opacity: 0.3;
}

.container:hover .middle {
  opacity: 1;
}

.text {
  background-color: #4CAF50;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}
</style>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<h2>Opacity with Box</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="../img/image-to-add-overlay.png" alt="Prabhat" class="image" style="width:100%">
  <div class="middle">
    <div class="text"><img src="/img/image-to-add-overlay.png" class="overlay-pattern"></div>
  </div>
</div>
  
</body>
</html>

Upvotes: 0

user9960496
user9960496

Reputation:

try This

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.container {
    position: relative;
    width: 50%;
}

.image {
  opacity: 1;
  display: block;
  width: 100%;
  height: auto;
  transition: .5s ease;
  backface-visibility: hidden;
}

.middle {
  transition: .5s ease;
  opacity: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  text-align: center;
}

.container:hover .image {
  opacity: 0.3;
}

.container:hover .middle {
  opacity: 1;
}

.text {
  background-color: #4CAF50;
  color: white;
  font-size: 16px;
  padding: 16px 32px;
}
</style>
</head>
<body>

<h2>Opacity with Box</h2>
<p>Hover over the image to see the effect.</p>

<div class="container">
  <img src="img_avatar.png" alt="Avatar" class="image" style="width:100%">
  <div class="middle">
    <div class="text">John Doe</div>
  </div>
</div>

</body>
</html>

Upvotes: 0

vishnu
vishnu

Reputation: 2948

Try something like this:

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

.overlay-pattern {
  content: "";
  background: url('https://i.sstatic.net/6BD07.png') repeat;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: 2;
  opacity: 0.2;
}
<div class="block">
  <img src="https://i.sstatic.net/ZKPqo.png">
  <div class="overlay-pattern"></div>
</div>

Upvotes: 1

Related Questions