向图像添加覆盖

时间:2018-06-20 08:56:34

标签: html css

您好,我想在图片上添加叠加层, 我用叠加层创建了一个类,并期望它在图像上重复,但是不起作用。

我在下面添加了html和css并包括了我正在使用的图像

echo pt2sec("PT3M52S");

function pt2sec($str) {
      preg_match("@PT((?P<hour>[0-9]+)H)?((?P<min>[0-9]+)M)?((?P<sec>[0-9]+)S)?@", $str, $m);
      return (isset($m["hour"])?$m["hour"]*3600:0)+(isset($m["min"])?$m["min"]*60:0)+(isset($m["sec"])?$m["sec"]:0);
}
.overlay-pattern {
  content: "";
  background: url('//unsplash.it/400/300') no-repeat;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  position: absolute;
  z-index: 2;
}

image overlay

image to add overlay

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

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

.overlay-pattern {
  content: "";
  background: url('https://i.stack.imgur.com/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.stack.imgur.com/ZKPqo.png">
  <div class="overlay-pattern"></div>
</div>

答案 1 :(得分:0)

尝试这个

<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>

答案 2 :(得分:0)

尝试一下

<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>