python小菜 2008-12-28 12:57 采纳率: 0%
浏览 506
已采纳

如何为所有浏览器垂直居中一个 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条回答 默认 最新

  • 谁还没个明天 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条)

报告相同问题?

悬赏问题

  • ¥20 python忆阻器数字识别
  • ¥15 无法输出helloworld
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊
  • ¥15 振荡电路,ADS仿真