为什么这张图片不会变暗?

时间:2018-01-22 21:08:36

标签: html css

有人能指出为什么我的图像不会变暗吗?我正在教自己一些HTML和CSS,不能让这个图像变得更暗。



.carousel-content {
  min-height: 100%;
  background-position: center;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.5);
}

.image1 {
  width: 100%;
  background: rgba(0, 0, 0, 0.5);
}

<div class="carousel-content">
  <img class="image1" src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" />
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:3)

背景在图像后面,如果你想让它变暗,你需要在它上面加一个覆盖层,并在那里设置透明背景。

&#13;
&#13;
.carousel-content {
    position: relative;
}

.overlay {
  position: absolute;
  left: 0;
  top: 0;
  height: 150px;
  width: 275px;
  background: rgba(0,0,0,0.5);
  border-right: 2px dashed #fff;
}
&#13;
<div class="carousel-content">
  <img class="image1" src="http://via.placeholder.com/350x150/ff0000/ffffff" />
  <div class="overlay"></div>
</div>
&#13;
&#13;
&#13;

或者您可以在现代浏览器中使用css过滤器:

&#13;
&#13;
img {
  filter: brightness(50%);
}
&#13;
<img src="http://via.placeholder.com/350x150" />
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您也可以按照它的方式保持它(假设是黑色背景),只需将图像的不透明度设置为稍微淡化并显示纯色背景。

.carousel-content {
  min-height: 100%;
  background-position: center;
  background-size: cover;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 1);
}

.image1 {
  width: 100%;
  opacity: .5;
}
<div class="carousel-content">
  <img class="image1" src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" />
</div>