【问题标题】:Horizontal line behind a heading in HTML and CSS [duplicate]HTML 和 CSS 中标题后面的水平线 [重复]
【发布时间】:2023-02-13 17:49:17
【问题描述】:

我正在尝试在标题后面形成一条水平线,如下所示。

我可以很容易地用标题周围的 div 来做到这一点。就像是:

  .holder{
    height:4px;
    background:red;
    width:100%;
    text-align:center;
  }
  h4{
    display:inline-block;
    text-align:center;
    background:white;
    line-height:50px;
    padding: 0 20px;
    margin-top:-23px;
    font-size:30px;
  }
<div class="holder">
  <h4>
    TESTING
  </h4>
</div>

但我希望能够在不添加外部 div 的情况下做同样的事情。我得到的最接近的是:

  h3{
    text-align:center;
    line-height:50px;
    position:relative;
    font-size:30px;
  }
  
  h3::before{
    display:block;
    background:red;
    width:100%;
    height:4px;
    top:50%;
    margin-top:-3px;
    position:absolute;
    content:'';
  }
<h3>
  TESTING
</h3>

结果如下:

谁能建议我如何在不添加 div 的情况下执行此操作?

【问题讨论】:

    标签: html css


    【解决方案1】:

    您可以使用 ::before::after 和 CSS 网格布局实现您想要的效果,如下所示:

    h3 {
      display: grid;
      grid-template-columns: 1fr auto 1fr;
      align-items: center;
      gap: 1rem;
      font-size: 30px;
    }
    
    h3::before,
    h3::after {
      background: red;
      height: 4px;
      content: "";
    }
    <h3>
      TESTING
    </h3>

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多