我正在寻找一种使图像垂直居中的方法,类似于text-align center
. text-align
center 有点高效,因为您不需要指定父级或子级的宽度,它会自动居中其内容。还有其他方法可以垂直定位图像吗?
你可以通过两种方式做到这一点
Way 1 (首选), 通过使用display: table-cell
div {
display: table-cell;
height: 100px;
border: 1px solid #f00;
vertical-align: middle;
}
Demo
方式二、使用position: absolute;
and top: 50%
然后减去总数的1/2height
图像的使用margin-top
div {
height: 100px;
border: 1px solid #f00;
position: relative;
}
div img {
position: absolute;
top: 50%;
margin-top: -24px; /* half of total height of image */
}
Demo 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)