css位置伪元素堆叠

时间:2017-04-25 20:12:39

标签: javascript jquery html css css3

您好我之前使用&在我的元素之后它工作得很好,但问题是当我为之前的部分设置背景颜色时&之后会消失我知道这个问题出现是因为z-index: -1而我知道我们不能将子元素(之前和之后)堆叠在父元素之上,那么解决方案是什么,我不会&#39 ; t需要创建任何新元素才能做到这一点:


这就是我的需要: enter image description here



section{
    height:400px;
    padding:50px 0;
    background-color:#00fb8f;
}
.box-shadow-1{
    height:200px;
    background-color:#e8e8e8;
    position:relative;
}
.box-shadow-1:before,
.box-shadow-1:after {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 25px;
  left: 10px;
  width: 50%;
  top: 80%;
  max-width: 300px;
  background-color:#ff0000;
  -moz-box-shadow: 0 20px 20px #777;
  -webkit-box-shadow: 0 20px 20px #777;
  box-shadow: 0 20px 20px #777;
  -webkit-transform: rotate(-8deg);
  -moz-transform: rotate(-8deg);
  -o-transform: rotate(-8deg);
  -ms-transform: rotate(-8deg);
  transform: rotate(-8deg);
}
.box-shadow-1:after {
  -webkit-transform: rotate(8deg);
  -moz-transform: rotate(8deg);
  -o-transform: rotate(8deg);
  -ms-transform: rotate(8deg);
  transform: rotate(8deg);
  right: 10px;
  left: auto;
}

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-11 col-lg-offset-1">
                <div class="box-shadow-1">
                    Hello World
                </div>
            </div>
        </div>
    </div>
</section>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:3)

您需要为box-shadow-1的父母z-index提供一个{/ 1}},就像这样

.col-lg-11.col-lg-offset-1 {
    position:relative;
    z-index: 0;
}

我还调整了你的伪元素,所以你得到像张贴的图像一样的阴影

Stack snippet

&#13;
&#13;
section{
    height:400px;
    padding:30px 0;
    background-color:#e8e8e8;
}
.col-lg-11.col-lg-offset-1 {        /*  added rule  */
    position:relative;
    z-index: 0;
}
.box-shadow-1{
    height:150px;
    background-color:#00fb8f;
    position:relative;
}
.box-shadow-1:before,
.box-shadow-1:after {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 25px;
  left: 10px;
  width: 50%;
  height: 20px;
  max-width: 300px;
  background-color:#ff0000;
  -moz-box-shadow: 0 30px 20px #777;
  -webkit-box-shadow: 0 30px 20px #777;
  box-shadow: 0 30px 20px #777;
  -webkit-transform: rotate(-8deg);
  -moz-transform: rotate(-8deg);
  -o-transform: rotate(-8deg);
  -ms-transform: rotate(-8deg);
  transform: rotate(-8deg);
}
.box-shadow-1:after {
  -webkit-transform: rotate(8deg);
  -moz-transform: rotate(8deg);
  -o-transform: rotate(8deg);
  -ms-transform: rotate(8deg);
  transform: rotate(8deg);
  right: 10px;
  left: auto;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-11 col-lg-offset-1">
                <div class="box-shadow-1">
                    Hello World
                </div>
            </div>
        </div>
    </div>
</section>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

z-index的父级设置box-shadow-1值。

section{
    height:400px;
    padding:50px 0;
    background-color:#00fb8f;
}
.box-shadow-1-parent {
  position: relative;
  z-index: 0;
}
.box-shadow-1 {
    height:200px;
    background-color:#e8e8e8;
    position:relative;
}
.box-shadow-1:before,
.box-shadow-1:after {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 25px;
  left: 10px;
  width: 50%;
  top: 80%;
  max-width: 300px;
  background-color:#ff0000;
  -moz-box-shadow: 0 20px 20px #777;
  -webkit-box-shadow: 0 20px 20px #777;
  box-shadow: 0 20px 20px #777;
  -webkit-transform: rotate(-8deg);
  -moz-transform: rotate(-8deg);
  -o-transform: rotate(-8deg);
  -ms-transform: rotate(-8deg);
  transform: rotate(-8deg);
}
.box-shadow-1:after {
  -webkit-transform: rotate(8deg);
  -moz-transform: rotate(8deg);
  -o-transform: rotate(8deg);
  -ms-transform: rotate(8deg);
  transform: rotate(8deg);
  right: 10px;
  left: auto;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-11 col-lg-offset-1 box-shadow-1-parent">
                <div class="box-shadow-1">
                    Hello World
                </div>
            </div>
        </div>
    </div>
</section>

答案 2 :(得分:0)

这是你在找什么?

&#13;
&#13;
section{
    height:400px;
    padding:50px 0;
    background-color:#00fb8f;
}
.box-shadow-1{
    height:200px;
    position:relative;
}
.box-shadow-1 > div {
    background-color:#e8e8e8;
    height: 100%;
    position: relative;
    z-index: 10;
}
.box-shadow-1:before,
.box-shadow-1:after {
  z-index: 1;
  position: absolute;
  content: "";
  bottom: 25px;
  left: 10px;
  width: 50%;
  top: 80%;
  max-width: 300px;
  background-color:#ff0000;
  -moz-box-shadow: 0 20px 20px #777;
  -webkit-box-shadow: 0 20px 20px #777;
  box-shadow: 0 20px 20px #777;
  -webkit-transform: rotate(-8deg);
  -moz-transform: rotate(-8deg);
  -o-transform: rotate(-8deg);
  -ms-transform: rotate(-8deg);
  transform: rotate(-8deg);
}
.box-shadow-1:after {
  -webkit-transform: rotate(8deg);
  -moz-transform: rotate(8deg);
  -o-transform: rotate(8deg);
  -ms-transform: rotate(8deg);
  transform: rotate(8deg);
  right: 10px;
  left: auto;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-11 col-lg-offset-1">
                <div class="box-shadow-1">
                    <div>Hello World</div>
                </div>
            </div>
        </div>
    </div>
</section>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

我认为实现此目标的唯一方法是将<section>发送到后面,然后将.box-shadow1的父级转发给此,

section{
    height:400px;
    padding:50px 0;
    background-color:#00fb8f;
    z-index: -2;
}

.col-lg-11 {
  z-index: 1;
}

<小时/> 如您所见,您可以实现您的目标。

&#13;
&#13;
section{
    height:400px;
    padding:50px 0;
    background-color:#00fb8f;
    z-index: -2;
}

.col-lg-11 {
  z-index: 1;
}

.box-shadow-1{
    height:200px;
    background-color:#e8e8e8;
    position:relative;
}

.box-shadow-1:before,
.box-shadow-1:after {
  z-index: -1;
  position: absolute;
  content: "";
  bottom: 25px;
  left: 10px;
  width: 50%;
  top: 80%;
  max-width: 300px;
  background-color:#ff0000;
  -moz-box-shadow: 0 20px 20px #000;
  -webkit-box-shadow: 0 20px 20px #000;
  box-shadow: 0 20px 20px #777;
  -webkit-transform: rotate(-8deg);
  -moz-transform: rotate(-8deg);
  -o-transform: rotate(-8deg);
  -ms-transform: rotate(-8deg);
  transform: rotate(-8deg);
}

.box-shadow-1:after {
  -webkit-transform: rotate(8deg);
  -moz-transform: rotate(8deg);
  -o-transform: rotate(8deg);
  -ms-transform: rotate(8deg);
  transform: rotate(8deg);
  right: 10px;
  left: auto;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section>
    <div class="container">
        <div class="row">
            <div class="col-lg-11 col-lg-offset-1">
                <div class="box-shadow-1">
                    Hello World
                </div>
            </div>
        </div>
    </div>
</section>
&#13;
&#13;
&#13;