【问题标题】:Divs not aligned to topdiv 未与顶部对齐
【发布时间】:2017-01-28 04:27:31
【问题描述】:

为什么会这样?我需要有人可以解释为什么 div 不能正确对齐?

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 中。

【问题讨论】:

  • "vertical-align: middle" 在此选择器 .h-ruler-first-line 上缺失
  • @RazvanCuceu :如果他们对您有帮助,您能否确认以下答案?谢谢!

标签: html css vertical-alignment css-calc


【解决方案1】:

您还需要将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】:

    您只需使用:after:before 伪元素,只需2 个div 元素即可实现。

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

      您的解决方案略有不同的方法 - 这在整个 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】:

        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/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-04-04
          • 1970-01-01
          • 2015-02-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多