绝对和静态定位元素元素的z-index

时间:2014-08-07 07:27:26

标签: css position z-index

如何使绝对定位元素出现在静态元素下。

Z-index似乎不起作用。绿色框应该在线上方(将是白色)。

http://jsfiddle.net/matthewabrman/pbL52gtj/

HTML:

<h3 class="line"><span>Lorem Ipsum</span></h3>

的CSS:

h3.line {
    font-size: 24px;
    display: block;
    font-weight: 300;
    text-align: center;
    margin:30px;
    position: relative;
}

h3.line:after {
    content: '';
    position: absolute;
    left: 0;
    top: 15px;
    width: 100%;
    height: 1px;
    background-color: #e0e0e0;
}

h3.line > span {    
    background-color: #afa;
    padding: 10px 20px;
}

1 个答案:

答案 0 :(得分:2)

试试这个。希望您试图获得 FIDDLE

h3.line {
    font-size: 24px;
    display: block;
    font-weight: 300;
    text-align: center;
    margin:30px;
    position: relative;
    z-index:-1;
}

h3.line:after {
    content: '';
    position: absolute;
    left: 0;
    top: 15px;
    width: 100%;
    height: 1px;
    background-color: #e0e0e0;
}

h3.line > span {    
    background-color: #afa;
    padding: 10px 20px;
    position: relative;
    z-index:1;
}

FIDDLE

h3.line {
    font-size: 24px;
    display: block;
    font-weight: 300;
    text-align: center;
    margin:30px;
    position: relative;

}

h3.line:after {
    content: '';
    position: absolute;
    left: 0;
    top: 15px;
    width: 100%;
    height: 1px;
    background-color: #e0e0e0;
    z-index:-1;
}

h3.line > span {    
    background-color: #afa;
    padding: 10px 20px;
    }