【问题标题】:Z-index positioning not working properlyZ-index 定位无法正常工作
【发布时间】:2015-12-04 13:48:35
【问题描述】:

我有一个容器,我想让h2 看起来与中心对齐,后面有一条细线。我不能做的是让线在h2 框后面。无论我尝试什么,它都会越过 h2 框。

以下是我需要完成的示例:

任何帮助表示赞赏。

<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;
}

【问题讨论】:

  • 要使z-index 工作,您需要提供元素position: relative|absolute|fixed。
  • 请HTML然后我们可以帮助:)
  • 请发布您当前拥有的 HTML 代码,并让我们知道您尝试的一些步骤。然后,我们可以努力让您朝着正确的方向前进。
  • 嗨@AjayMakwana 有一段html和css。 @user3262111 我试图绝对定位h2 后面的线并将h2 索引设置为一个高数字,这样它就会超过细线,但它没有用,线一直穿过@ 987654331@块。
  • 大家好,你们真的帮助了我。太感谢了!祝大家有美好的一天。

标签: html css z-index positioning


【解决方案1】:

我做了一个样本。检查我的 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;

}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      对齐将随着容器高度动态变化

      .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>

      【讨论】:

      • 非常感谢!瓦卢,卡拉! :-DD
      【解决方案4】:

      尝试添加这个:

      h2 {
        position: absolute;
        left:0;
        right:0;
        margin:auto;
      }
      
      .line-section-header {
        margin-top: 50px;
        position: absolute;
      }
      

      【讨论】:

        【解决方案5】:

        尝试在 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;
        }
        

        【讨论】:

          猜你喜欢
          • 2012-01-08
          • 2022-01-23
          • 2017-09-29
          • 1970-01-01
          • 2015-08-04
          • 1970-01-01
          • 1970-01-01
          • 2018-11-08
          • 1970-01-01
          相关资源
          最近更新 更多