【问题标题】:Build line-through style with flexbox and pseudo elements使用 flexbox 和伪元素构建 line-through 样式
【发布时间】:2018-03-20 17:38:58
【问题描述】:

我正在构建一个可以跨越多行的直通标题。使用下面的示例代码,是否可以以不需要左右 div 的方式编写我的 CSS?在哪里可以将它们作为伪类添加到我的头类中?

CodePen

.container {
  box-sizing: border-box;
  display: flex;
  place-content: center space-evenly;
  align-items: center;
}

.line {
  flex: 1;
  height: 2px;
  background: black;
}

.header {
  font-size: 50px;
  margin: 0 30px;
  text-align: center;
}

.header-broken:after {
  content: '';
  display: -webkit-inline-flex;
  display: -ms-inline-flexbox;
  display: inline-flex;
  width: 50px;
  height: 5px;
  flex: auto;
  width: 100%;
  height: 2px;
  background: black;
}
<div class="container">
  <div class="line"></div>
  <div class="header">Normal Title<br>fdasfsaf</div>
  <div class="line"></div>
</div>

【问题讨论】:

    标签: html css flexbox pseudo-element


    【解决方案1】:

    只用一个div就可以了,看下面的例子,根据需要给伪元素添加一些margin来做间距。

    .container {
      display: flex;
      text-align: center;
    }
    
    .container:before,
    .container:after {
      content: "";
      flex: 1;
      background: linear-gradient(black, black) center / 100% 1px no-repeat;
    }
    <div class="container">
      Normal Title<br>fdasfsaf
    </div>

    【讨论】:

      【解决方案2】:

      你也可以试试这个。

      HTML:

      <div class="container">
        <div class="header">
           <h1>Normal Title
              <br>fdasfsaf
           </h1>
        </div>
      </div>
      

      CSS:

      .container {
              display: flex;
              text-align: center;
          }
      
          .header {
              flex: 1;
          }
      
          .header h1 {
              font-size: 50px;
              margin: 0 30px;
              text-align: center;
              background-color: #fff;
              display: inline-block;
          }
      
          .header:after {
              content: '';
              border-bottom: 1px solid #000;
              display: block;
              margin-top: -58px;
          }
      

      【讨论】:

        猜你喜欢
        • 2020-03-05
        • 2018-01-01
        • 1970-01-01
        • 2016-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-13
        • 2021-03-12
        相关资源
        最近更新 更多