[英]I want to stack two DIVs on top of each other and have one be transparent so you can see through to the div behind it. Using CSS and HTML
我在 div 中有一个背景图像,我希望能够在背景图像的顶部放置另一个具有较低不透明度的黑框,以便您可以看穿它并使图像变暗。
You can use z-index
on an image in a container to push it to the back of the div
to act as a background-image
by giving it a z-index of -1 and the parent a z-index of 1. Then position it absolute and use it to fill the whole parent.您可以在容器中的图像上使用z-index
将其推到div
的后面以充当background-image
是为其提供 -1 的 z-index 和父级的 z-index 1。然后定位它是绝对的,并用它来填充整个父级。
An implementation is here below:下面是一个实现:
**My index.html
** **我的index.html
**
<div class="container">
<div class="content">
<img src="./images/img.jpg" alt="">
<h1>My content</h1>
</div>
</div>
**My style.css
** **我的style.css
**
.container {
position: relative;
display: grid;
place-content: center;
z-index: 1;
}
.container .content {
width: 100%;
height: 100%;
background-color: #00000052;
}
.container img {
position: absolute;
z-index: -1;
width: 100%;
height: 100%;
object-fit: cover;
}
The easiest way to accomplish this effect is by applying the CSS brightness filter directly on the image.实现此效果的最简单方法是直接在图像上应用CSS 亮度过滤器。
You could certainly stack two divs on top of each other with absolute/relative positioning and z-index, but those can create other issues and will require you to write more code for less performance.您当然可以使用绝对/相对定位和 z-index 将两个 div 堆叠在一起,但这些会产生其他问题,并且需要您编写更多代码以降低性能。
.dark { filter: brightness(40%); }
<img src="https://randomuser.me/api/portraits/women/68.jpg" width="200" height="200" alt="" /> <img class="dark" src="https://randomuser.me/api/portraits/women/68.jpg" width="200" height="200" alt="" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.