1. 元素高度声明的情况下在父容器中居中:绝对居中法
<div class="parent">
<div class="absolute-center"></div>
</div>
.parent {
position: relative;
}
.absolute-center {
position: absolute;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 70%;
width: 70%;
}
优点:
1.跨浏览器,包括 IE8-10
2.无需其他冗余标记,CSS 代码量少
3.完美支持图片居中
4.宽度高度可变,可用百分比
缺点:
1.必须声明高度