在以下示例中,我有两个元素。一个有内容部分的背景图像,另一个没有。两个标头都有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>
答案 0 :(得分:1)
您只需为#hasbgimage
.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;
-webkit-box-shadow: inset 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: inset 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);
}
&#13;
<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>
&#13;
答案 1 :(得分:1)
您只需将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;
}
&#13;
<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>
&#13;
答案 2 :(得分:0)
添加子div并在其上设置背景图像,因此它不会覆盖框阴影。
.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/aabbcc/");
background-size: cover;
height:100%;
width:100%;
}
<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">
<div id="hasbgimage"></div>
</div>
</div>