更新时间:2022-11-01 23:36:31
My demo on dabblet.com
这个演示的主要技巧是在正常的元素流从上到下,所以 margin-top:auto
设置为零。然而,绝对定位的元素对于分配***空间是相同的,并且类似地可以在指定的 top
和底部
(在IE7中不起作用)。
The main trick in this demo is that in the normal flow of elements going from top to bottom, so the margin-top: auto
is set to zero. However, an absolutely positioned element acts the same for distribution of free space, and similarly can be centered vertically at the specified top
and bottom
(does not work in IE7).
<div></div>
div {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}