℡Wang Yan 2008-12-28 12:57 采纳率: 100%
浏览 609
已采纳

如何为所有浏览器垂直居中一个 div?

I want to center a div vertically with CSS. I don't want tables or JavaScript, but only pure CSS. I found some solutions, but all of them are missing Internet Explorer 6 support.

<body>
    <div>Div to be aligned vertically</div>
</body>

How can I center a div vertically in all major browsers, including Internet Explorer 6?

转载于:https://stackoverflow.com/questions/396145/how-to-vertically-center-a-div-for-all-browsers

  • 写回答

30条回答 默认 最新

  • Lotus@ 2011-05-31 03:13
    关注

    Below is the best all-around solution I could build to vertically and horizontally center a fixed-width, flexible height content box. It was tested and working for recent versions of Firefox, Opera, Chrome, and Safari.

    .outer {
      display: table;
      position: absolute;
      height: 100%;
      width: 100%;
    }
    
    .middle {
      display: table-cell;
      vertical-align: middle;
    }
    
    .inner {
      margin-left: auto;
      margin-right: auto;
      width: 400px;
      /*whatever width you want*/
    }
    <div class="outer">
      <div class="middle">
        <div class="inner">
          <h1>The Content</h1>
          <p>Once upon a midnight dreary...</p>
        </div>
      </div>
    </div>

    View A Working Example With Dynamic Content

    I built in some dynamic content to test the flexibility and would love to know if anyone sees any problems with it. It should work well for centered overlays also -- lightbox, pop-up, etc.

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

报告相同问题?

悬赏问题

  • ¥50 comsol稳态求解器 找不到解,奇异矩阵有1个空方程返回的解不收敛。没有返回所有参数步长;pid控制
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功