gpt4 book ai didi

css - 带有背景图像的元素上的阴影?

转载 作者:太空宇宙 更新时间:2023-11-04 01:14:04 25 4
gpt4 key购买 nike

在下面的示例中,我有两个元素。一个有内容部分的背景图像,另一个没有。两个 header 都有一个 box-shadow 属性。如何使阴影在设置了背景图像的元素上方可见?

.testcontainer {
width: 200px;
display: inline-block;
border: 1px solid gray;
height: 120px;
}

.header {
height: 20px;
background-color: cornflowerblue;
color: white;
-webkit-box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
}

.content {
height: 100px;
}

#hasbgimage {
background-image: url("https://via.placeholder.com/200x100");
background-size: cover;
}
<div class="testcontainer">
<div class="header">test with no image</div>
<div class="content"></div>
</div>
<div class="testcontainer">
<div class="header">test with image</div>
<div class="content" id="hasbgimage"></div>
</div>

最佳答案

您可以简单地将 z-index 添加到第一个元素,使其 box-shadow 也位于上方

.testcontainer {
width: 200px;
display: inline-block;
border: 1px solid gray;
height: 120px;
}

.header {
position:relative; /*And don't forget this !!!*/
z-index:2;
height: 20px;
background-color: cornflowerblue;
color: white;
-webkit-box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
}

.content {
height: 100px;
}

#hasbgimage {
background-image: url("https://via.placeholder.com/200x100");
background-size: cover;
}
<div class="testcontainer">
<div class="header">test with no image</div>
<div class="content"></div>
</div>
<div class="testcontainer">
<div class="header">test with image</div>
<div class="content" id="hasbgimage"></div>
</div>

关于css - 带有背景图像的元素上的阴影?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50592119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com