喵-见缝插针 2012-01-14 21:25 采纳率: 0%
浏览 494
已采纳

如何用 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条回答 默认 最新

  • ℡Wang Yan 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条)

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档