狐狸.fox 2012-01-14 21:25 采纳率: 0%
浏览 548
已采纳

如何用 CSS 垂直居中显示文本?

I have a div element which contains text, and I want to align the contents of this div vertically center.

Here is my div style:

#box {
  height: 170px;
  width: 270px;
  background: #000;
  font-size: 48px;
  color: #FFF;
  text-align: center;
}
<div id="box">
  Lorem ipsum dolor sit
</div>

What is the best way to do this?

</div>

转载于:https://stackoverflow.com/questions/8865458/how-do-i-vertically-center-text-with-css

  • 写回答

30条回答 默认 最新

  • 狐狸.fox 2014-08-11 12:38
    关注

    You can try this basic approach:

    div {
      height: 90px;
      line-height: 90px;
      text-align: center;
      border: 2px dashed #f69c55;
    }
    <div>
      Hello World!
    </div>

    It only works for a single line of text though, because we set the line's height to the same height as the containing box element.


    A more versatile approach

    This is another way to align text vertically. This solution will work for a single line and multiple lines of text, but it still requires a fixed height container:

    div {
      height: 200px;
      line-height: 200px;
      text-align: center;
      border: 2px dashed #f69c55;
    }
    span {
      display: inline-block;
      vertical-align: middle;
      line-height: normal;
    }
    <div>
      <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Haec et tu ita posuisti, et verba vestra sunt. Non enim iam stirpis bonum quaeret, sed animalis. </span>
    </div>

    The CSS just sizes the <div>, vertically center aligns the <span> by setting the <div>'s line-height equal to its height, and makes the <span> an inline-block with vertical-align: middle. Then it sets the line-height back to normal for the <span>, so its contents will flow naturally inside the block.


    Simulating table display

    And here is another option, which may not work on older browsers that don't support display: table and display: table-cell (basically just Internet Explorer 7). Using CSS we simulate table behavior (since tables support vertical alignment), and the HTML is the same as the second example:

    div {
      display: table;
      height: 100px;
      width: 100%;
      text-align: center;
      border: 2px dashed #f69c55;
    }
    span {
      display: table-cell;
      vertical-align: middle;
    }
    <div>
      <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
    </div>

    Using absolute positioning

    This technique uses an absolutely positioned element setting top, bottom, left and right to 0. It is described in more detail in an article in Smashing Magazine, Absolute Horizontal And Vertical Centering In CSS.

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知