窗口中div的垂直+水平居中的最简单方法是什么?

Mis*_*hko 7 html css alignment vertical-alignment

给定div已知尺寸,比如说width: 300px; height: 200px,将屏幕垂直和水平放置在屏幕中间的最简单方法是什么?这里的例子

我对最新的Firefox感兴趣(不需要IE黑客).

只有CSS,没有Javascript.

Mar*_*rko 9

将它从窗口/父容器中定位50%,并使用边距大小为元素宽度/高度的一半:)

position: absolute; // or fixed if you don't want it scrolling with the page.
width: 300px;
height: 200px;
left: 50%;
top: 50%;
margin-left: -150px;
margin-top: -100px;
Run Code Online (Sandbox Code Playgroud)

您可能需要设置height: 100%,同时在bodyhtml

html, body {
    height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个有效的例子.