html - div 未与顶部对齐

标签 html css vertical-alignment css-calc

为什么会发生这种情况?我需要有人可以解释为什么 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 未包含在 .horizo​​ntal-ruler 中。

最佳答案

您还需要在行中添加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>

关于html - div 未与顶部对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39590832/

相关文章:

html - 在 HTML 中紧密打包列表项网格

CSS:如何在垂直方向的 block 元素中心设置文本

javascript - event.clipboardData 未定义

html - Ionic 元素行中具有不同宽度的多列

html - CSS 中的相对 Spritesheet 动画

python - Django 手动渲染表单并使用 csrf token

jquery - 循环图像

html - Angular 1 选择不正确呈现

html - 是否可以用@font-face 替换图标?

objective-c - NSTextField 底部对齐