简体   繁体   English

我想将两个 DIV 堆叠在一起,并使一个透明,这样您就可以看到它后面的 div。 使用 CSS 和 HTML

[英]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.

相关问题 我可以让一个 div 只显示 svg 图像的内容,否则是透明的,这样我就可以将两个 svg 堆叠在一起吗? - Can I make a div display only the contents of a svg image and otherwise be transparent, so I can stack two svgs on top of each other? CSS-定位多个相邻的div,以便在2个div之间彼此堆叠 - CSS - Positioning of multiple adjacent divs to have one stack on top of each other between 2 divs 如何使用Float而不是绝对定位将多个Div堆叠在一起? - How can I have multiple Divs stack on top of each other using Float and not absolute positioning? CSS堆栈div彼此叠置 - css stack divs on top of each other 在一个div旁边对齐两个div(彼此顶部) - Align two divs (on top of each other) next to one div CSS 如何让两个 div 在一个 div 的右侧但不相邻 - CSS how to have two divs to the right of one div but not beside each other 使用CSS将一个TR上的两个TD堆叠在一起 - Using CSS to stack two TDs from one TR on top of each other 使用CSS在彼此上方显示两个div - Have two divs display on top of one another using CSS 如何使3个div彼此并排,以使其位于底部和顶部div之间? - How to have 3 divs aligned up next to each other so that they are in between a bottom and top div? 我可以将多个图像堆叠在一起并在它们之间切换吗? HTML、CSS、Java 脚本 - Can I stack multiple images on top of each other and switch between them? HTML, CSS, Java Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM