我正在我的页面中尝试将div
显示在::before
和::after
CSS选择器上,所以我正在努力结果:
但是我得到了JSFIDDLE
这是我的代码:
<div class="box"></div>
.box{
width: 350px;
height: 350px;
background: #555;
position: absolute;
top: 50px;
left: 200px;
z-index: 10;
}
.box::before{
content: "";
background: #A60000;
width: 300px;
height: 300px;
position: absolute;
top: 25px;
left: -150px;
z-index: 1;
}
.box::after{
content: "";
background: #02047A;
width: 300px;
height: 300px;
position: absolute;
top: 25px;
left: 200px;
margin: auto;
z-index: 1;
}
我知道在其内容上显示div
似乎是因为 Chrome 向我显示 HTML源代码,如下所示:
但我希望有办法解决这个问题..
提前致谢...
答案 0 :(得分:8)
更改您的css:
.box{
width: 350px;
height: 350px;
background: #555;
position: absolute;
top: 50px;
left: 200px;
}
.box::before{
content: "";
background: #A60000;
width: 300px;
height: 300px;
position: absolute;
top: 25px;
left: -150px;
z-index: -1;
}
.box::after{
content: "";
background: #02047A;
width: 300px;
height: 300px;
position: absolute;
top: 25px;
left: 200px;
margin: auto;
z-index: -1;
}
您所要做的就是将z-index
:after
和:before
设置为-1
,然后从.box
z-index
中删除。
答案 1 :(得分:2)
添加或替换以下属性(使:before
和:after
元素显示在.box
申请z-index:-1
后面,并使用默认z-index
代替.box
}):
.box{
position: absolute;
}
.box::before{
z-index: -1;
-webkit-transform: rotate(-90deg);
-moz-transform: rotate(-90deg);
-o-transform: rotate(-90deg);
-ms-transform: rotate(-90deg);
transform: rotate(-90deg);
}
.box::after{
z-index: -1;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
-o-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
}