如何在<div>元素CSS中垂直对齐文本?

Ale*_*ing 1 html css google-chrome google-chrome-extension

我是第一次进行chrome扩展程序,并试图将文本居中。我现在使用的text-align: centre;是水平对齐方式,但无法弄清楚如何垂直对齐,因此此刻我的文字看起来像这样:

扩展屏幕截图

如果有人可以帮助,那就太好了。

小智 9

例如)显示:表格

div{
  display: table;
  width: 100px;
  height: 50px;
  background-color: #000;
}

div p{
  display: table-cell;
  text-align: center;
  vertical-align: middle;
  color: #fff;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <p>chatter</p>
</div>
Run Code Online (Sandbox Code Playgroud)

例如)flex

div{
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100px;
  height: 50px;
  background-color: #000;
}

div p{
  color: #fff;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <p>chatter</p>
</div>
Run Code Online (Sandbox Code Playgroud)

ex)位置

div{
  position: relative;
  width: 100px;
  height: 50px;
  background-color: #000;
  text-align: center;
}
div p{
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  color: #fff;
}
Run Code Online (Sandbox Code Playgroud)
<div>
  <p>chatter</p>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 这对我来说很有效!好的解决方案。 (2认同)