小编Orc*_*uss的帖子

如何在div中居中和垂直对齐文本?

下面的代码垂直对齐div中的文本完全正确,但是当我尝试将其居中时,只有需要包装的文本才会居中.当文本很短时,例如5-6个单词,文本不居中.我不知道这只是我还是我在这里做错了什么.

我正在使用 display: table-cell;垂直对齐.两者divp元素都以相同的方式在CSS中定义.看看codepen看看问题.

<style>
  .outer { outline: 1px solid #eee; }
  .outer > p { 
    display: table-cell; 
    height: 200px; 
    vertical-align: middle;
    text-align: center;
  }
</style>
<div class="outer">
  <p>
    This longer text will be vertically aligned.
  </p>
</div>
<div class="outer">
  <p>
    This longer text will be vertically aligned and centered. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, …
Run Code Online (Sandbox Code Playgroud)

css vertical-alignment centering

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

centering ×1

css ×1

vertical-alignment ×1