Z-index定位不正常

时间:2015-12-03 15:11:32

标签: html css z-index positioning

我有一个容器,我想让h2看起来与中心对齐,后面有一条细线。我无法做到的是让这条线落在h2框后面。无论我尝试什么,它都会一直穿过超过 h2框。

以下是我需要完成的示例: enter image description here

任何帮助表示感谢。

<div class="wrapper">
  <div class="section-header">
    <h2>H2 needs to be centered</h2>
    <div class="line-section-header"></div>
  </div>
</div>

我使用的CSS是:

h2 {
    font-family: 'Open Sans', Arial, sans-serif;
    font-family: 20px;
    font-weight: 700;
    text-transform: uppercase;
    width: 140px;
    color: #3F3F3F;
    background-color: white;
    padding: 5px 10px;
    border: 1px solid #D3D3D3;
    text-align: center;
    margin: 0 auto;
    z-index: 999;
}


.wrapper {
    margin: 0 auto;
    width: 980px;
    height: 1000px;
    background-color: #white;
}

.section-header {
    width: 100%;
    height: auto;
    background-color: white;
    float: left;
    position: relative;
}

.line-section-header {
    width: 100%;
    height: 1px;
    background-color: #D3D3D3;
    margin-top: 15px;
    position: absolute;
}

5 个答案:

答案 0 :(得分:2)

我做了一个样本。检查我的jsfiddle http://jsfiddle.net/amitv1093/r3arp2m8/

HTML

<div class="container">

  <h2>  h2 </h2>
  <div class="line"> </div>

</div>

CSS

.container
{
  position:relative;
text-align:center;
 background:#d9d9d9;
  z-index:-99;
  padding:12px 0px;
}

h2
{
width:100px;  
background:grey;
height:50px;
text-align:center;
  margin:0px auto;
  line-height:50px;

}

.line
{
  position:absolute;
  width:100%;
  height: 1px;
  background: red;
  top:50%;
  margin:1px 0px;  
  z-index:-9;

}

答案 1 :(得分:1)

  

h2 {//其他道具位置:相对; }

答案 2 :(得分:1)

对齐将是动态的容器高度

.wrapper {
    margin: 0 auto;
    width: 980px;
    height: 1000px;
    background-color: #white;
}

.section-header {
    width: 100%;
    height: auto;
    background-color: white;
    float: left;
    position: relative;
}

h2 {
    font-family: 'Open Sans', Arial, sans-serif;
    font-family: 20px;
    font-weight: 700;
    text-transform: uppercase;
    width: 140px;
    color: #3F3F3F;
    background-color: white;
    padding: 5px 10px;
    border: 1px solid #D3D3D3;
    text-align: center;
    margin: 0 auto;
    z-index: 999;
    position: relative;
}
.line-section-header {
    width: 100%;
    height: 1px;
    background-color: #D3D3D3;
    top: 50%;
    position: absolute;
    z-index: 1;
}
<div class="wrapper">
  <div class="section-header">
    <h2>H2 needs to be centered</h2>
    <div class="line-section-header"></div>
  </div>
</div>

答案 3 :(得分:0)

尝试添加此内容:

h2 {
  position: absolute;
  left:0;
  right:0;
  margin:auto;
}

.line-section-header {
  margin-top: 50px;
  position: absolute;
}

答案 4 :(得分:0)

尝试在css中更改此内容:

h2 {
    font-family: 'Open Sans', Arial, sans-serif;
    font-family: 20px;
    font-weight: 700;
    text-transform: uppercase;
    width: 140px;
    color: #3F3F3F;
    background-color: white;
    padding: 5px 10px;
    border: 1px solid #D3D3D3;
    text-align: center;
    margin: 0 auto;
    margin-left: 420px;
    z-index: 2;
    position: absolute;
}


.wrapper {
    margin: 0 auto;
    width: 980px;
    height: 1000px;
    background-color: #white;
    position: relative;
}

.section-header {
    width: 100%;
    height: auto;
    background-color: white;
    float: left;
    position: relative;
}

.line-section-header {
    width: 100%;
    height: 1px;
    background-color: #D3D3D3;
    margin-top: 70px;
    position: absolute;
    z-index: 1;
}