代码之家  ›  专栏  ›  技术社区  ›  Razvan Cuceu

刻度未与顶部对齐

  •  2
  • Razvan Cuceu  · 技术社区  · 10 年前

    HTML和CSS:

    .horizontal-ruler {
      width: 100%;
      height: 25px;
      line-height: 25px;
      display: block;
      font-size: 14px;
      color: #373737;
    }
    .horizontal-ruler .ruler-unit {
      width: 30px;
      text-align: center;
      display: inline-block;
      line-height: 25px;
      height: 25px;
      vertical-align: middle;
    }
    .h-ruler-first-line,
    .h-ruler-second-line {
      width: calc(50% - 15px);
      margin: 12px 0;
      height: 1px;
      background-color: #373737;
      display: inline-block;
    }
    <div class="horizontal-ruler">
      <div class="h-ruler-first-line"></div><!--
    --><div class="ruler-unit">24"</div><!--
    --><div class="h-ruler-second-line"></div>
    </div>

    https://jsfiddle.net/6xuvr6vw/1/

    正如您所看到的 .ruler-unit .horizontal-ruler .

    4 回复  |  直到 9 年前
        1
  •  1
  •   Nenad Vracar    10 年前

    你需要添加 vertical-align: middle 也到行

    .horizontal-ruler {
      width: 100%;
      height: 25px;
      line-height: 25px;
      display: block;
      font-size: 14px;
      color: #373737;
    }
    
    .horizontal-ruler .ruler-unit {
      width: 30px;
      text-align: center;
      display: inline-block;
      line-height: 25px;
      height: 25px;
      vertical-align: middle;
    }
    
    .h-ruler-first-line,
    .h-ruler-second-line {
      width: calc(50% - 15px);
      vertical-align: middle;
      margin: 12px 0;
      height: 1px;
      background-color: #373737;
      display: inline-block;
    }
    <div class="horizontal-ruler">
      <div class="h-ruler-first-line"></div><!--
    --><div class="ruler-unit">24"</div><!--
    --><div class="h-ruler-second-line"></div>
    </div>
        2
  •  1
  •   Mohammad Usman    10 年前

    只需2个 div 元素使用 :after 和 :before 伪元素。

    .horizontal-ruler{
      text-align: center;
      overflow: hidden;
      line-height: 25px;
      font-size: 14px;
      color: #373737;
      height: 25px;
    }
    .horizontal-ruler .ruler-unit {
      display: inline-block;
      vertical-align: top;
      position: relative;
      padding: 0 10px;
    }
    .horizontal-ruler .ruler-unit:before,
    .horizontal-ruler .ruler-unit:after {
      background-color: #373737;
      position: absolute;
      margin-top: -1px;
      width: 9999px;
      height: 1px;
      right: 100%;
      content: '';
      top: 50%;
    }
    .horizontal-ruler .ruler-unit:after {
      right: auto;
      left: 100%;
    }
    <div class="horizontal-ruler">
      <div class="ruler-unit">24"</div>
    </div>
        3
  •  1
  •   gavgrif    10 年前

    与您的解决方案稍有不同的方法是,在整个div上有一个边框底部,文本在行上的相对位置向下,看起来像是由文本内容分隔的两行。可能会更好——只是为了向您展示一种不需要太多代码就能达到相同效果的替代方案。

    .horizontal-ruler{
      border-bottom:solid 1px #373737;
      text-align:center;
    }
    .ruler-unit{
      font-size:24px;
      width:30px;
      margin:0 auto;
      padding: 0 10px;
      position:relative;
      top:14px;
      background:white;
    }
    <div class="horizontal-ruler">
    <div class="ruler-unit">24"</div>
    </div>
        4
  •  0
  •   kukkuz    10 年前

    添加 vertical-align: middle 至线路:

    .h-ruler-first-line, .h-ruler-second-line {
        width: calc(50% - 15px);
        margin: 12px 0;
        height: 1px;
        background-color: #373737;
        display: inline-block;
        vertical-align: middle;
    }
    

    小提琴: https://jsfiddle.net/udwffajo/