元素高度確定的情況下,我喜歡使用下面這種方案奈嘿,因?yàn)榧嫒菪粤己梦牧簦瑹o須增加額外元素。
//父元素鞋诗,高度不定
.wrap{
border:1px solid black;
height:500px;
position:relative;
}
//需要居中的子元素,有確定的高度
.content{
height:100px;
width:100px;
background-color:pink;
position:absolute;
top:50%;
margin-top:-50px;//高度的二分之一
}
jsbin在線查看效果:https://gist.github.com/anonymous/f72f9190b90877154b472fae6369911b