零零乙 2012-01-14 21:25 采纳率: 33.3%
浏览 450
已采纳

如何用 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

  • 写回答

29条回答 默认 最新

  • 妄徒之命 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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(28条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作