(已知宽高居中)
div{
width:200px;
height:200px;
border:1px black solid ;
background-color: #cceecc;
position:absolute;
top:50%;
left:50%;
margin-top: -100px;
margin-left: -100px;
}
margin-top和margin-left的值是已知宽高的一半
(未知宽高居中问题)
div{
border:1px solid black ;
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto
}
上面的是未知宽高的元素居中,可在该元素添加宽高属性值,居中不受影响