html - 为什么out div的高度受vertical-align影响

标签 html css

在 chrome 中,我认为 .wrap 的高度应该是 24px。 vertial-align 设置为 middle 时为 25px。有人可以解释为什么吗?

this article 中回答

body {
  line-height: 1.5;
}

.wrap {
  font-size:16px;  /* 16*1.5= 24 */
}

.btn {
  font-size: 14px;
  padding: 4px;
  line-height: 1;
  box-sizing: border-box;
  border: 1px solid blue;
  vertical-align: middle;
}
<div class="wrap">
  <button class="btn">
    OK
  </button>
</div>

最佳答案

正如解释的那样Here

Aligns the middle of the element with the baseline plus half the x-height of the parent.

您的 .wrap 元素的行高默认为 1.5,它继承自 body,垂直对齐属性将根据您的行高呈现,因此如果行高大于您的包装元素,您将看到效果。要修复它,只需将 line-height 放入您的 .wrap 类中,您可以使用它来查看 line-height 如何影响垂直对齐。

body {
  line-height: 1.5;
}

.wrap {
  line-height:1;
  background:red;
}

.btn {
  font-size: 14px;
  padding: 4px;
  line-height: 1;
  box-sizing: border-box;
  border: 1px solid blue;
  vertical-align: middle;
}
<div class="wrap">
  <button class="btn">
    OK
  </button>
</div>

关于html - 为什么out div的高度受vertical-align影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47653836/

相关文章:

html - 使 HTML 内容扩展以填充窗口

html - CSS自动顶部div

javascript - 从方位获取像素位置

html - 仅使用 HTML 自动调整文本大小以填充固定大小的容器

javascript - 在转换前动态设置 css 转换

javascript - 即使在添加 -webkit 后,CSS 动画也无法在 Safari Web 浏览器中运行

html - 垂直对齐图像和两行文本

html - 分辨率高于 1024x768 时页面底部的空白区域

javascript - 带有变量的复杂 Jquery 选择器

Javascript 替换未定义错误显示!