html - 垂直对齐容器中的一行文本

标签 html css

vertical-align here 上阅读了一些内容之后,我决定对此功能进行一些练习,但陷入了一个真正难以理解的问题。能否请您弄清楚为什么“文本行”在包装器 div 中没有垂直对齐。

.wrapper {
  height: 200px;
  background-color: aquamarine;
  margin: 0;
  padding: 0;
}

.ctxt {
  display: inline-block;
  vertical-align: middle;
  margin: 0;
  padding: 0;
}

.ctxt-before {
  display: inline-block;
  width: 10px;
  height: 200px;
  background-color: #f66;
  margin: 0;
  padding: 0;
}
<div class="wrapper">
  <!-- i think the line-box is 200px and div.ctxt should vertical-aligned in div.wrapper -->
  <div class="ctxt-before"></div>
  <p class="ctxt">this line of text</p>
</div>

我认为 line-box 是 200px 并且 div.ctxt 应该在 div.wrapper 中垂直对齐。 enter image description here

最佳答案

您几乎明白了,但是您将 ctxt div 的中间与 ctxt-before div 的基线对齐。因为它没有内容,所以它的基线是它的底部边缘。

您需要将它与 ctxt-before div 的中间对齐:

.wrapper {
  height: 200px;
  background-color: aquamarine;
  margin: 0;
  padding: 0;
}

.ctxt {
  display: inline-block;
  vertical-align: middle;
  margin: 0;
  padding: 0;
}

.ctxt-before {
  display: inline-block;
  width: 10px;
  height: 200px;
  background-color: #f66;
  margin: 0;
  padding: 0;
  vertical-align: middle; /* add this */
}
<div class="wrapper">
  <!-- i think the line-box is 200px and div.ctxt should vertical-aligned in div.wrapper -->
  <div class="ctxt-before"></div>
  <p class="ctxt">this line of text</p>
</div>

关于html - 垂直对齐容器中的一行文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52323942/

相关文章:

css - Vaadin 网格布局边框

css - 运算符 '===' 不能应用于类型 'number' 和 'string .. 在 [ngClass] 中使用时

html - CSS - 网站不会在浏览器中居中

javascript - 如何在客户端使用 Node.js 变量?

html - 如何让内容完美的适应flex属性?

html - 应用程序应该将html内容还是markdown内容保存到数据库中吗?

css - 当 View 高度/宽度更改时,如何使子元素保留在父元素内?

jquery - 使用 jQueryUI sortable 和 draggable 导致 sortable 元素的容器内联 block 元素跳转

javascript - 在父div上方创建div

javascript - 我想删除 Anythingslider 中的开始/停止按钮,但保留其余的导航