开发者

vertical align image within parent

开发者 https://www.devze.com 2022-12-11 07:29 出处:网络
i just need a way to make an image align vertically within it\'s parent element (in this 开发者_如何学Pythoncase, an <li>, but would be nice if solution worked for <div> as well). i have t

i just need a way to make an image align vertically within it's parent element (in this 开发者_如何学Pythoncase, an <li>, but would be nice if solution worked for <div> as well). i have tried everything and can't get anything to work...

basically, i want to align it vertically. also, i do not know the height of the parent element ahead of time.


Try this:

<div class="wraptocenter"><span></span><img src="..." alt="..."></div>

And the CSS:

    <style type="text/css">
.wraptocenter {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
    width: ...;
    height: ...;
}
.wraptocenter * {
    vertical-align: middle;
}
/*\*//*/
.wraptocenter {
    display: block;
}
.wraptocenter span {
    display: inline-block;
    height: 100%;
    width: 1px;
}
/**/
</style>
<!--[if lt IE 8]><style>
.wraptocenter span {
    display: inline-block;
    height: 100%;
}
</style><![endif]-->

Credit: http://www.brunildo.org/test/img_center.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号