在IE7中的父DIV中垂直对齐DIV

时间:2012-10-25 06:23:07

标签: html css vertical-alignment

想知道是否可以在父DIV中垂直对齐X个DIV。

所有DIV都没有固定高度。

在除IE7之外的所有浏览器中工作。

illustrated example

<div class="parent">
     <div class="left">
         Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed ac faucibus nisi. Proin nec eros est, vitae rhoncus purus.
     </div>
     <div class="right">
         <img src="image.gif" width="50" height="50">
     </div>
</div>
.parent {
   display: table;
   vertical-align: middle;

}
.left, .right {
   display: table-cell;
   vertical-align: middle;
 }

1 个答案:

答案 0 :(得分:1)

IE7不支持display: table-cell;属性,它支持IE8 +

Check its Compatibility

编辑:

作为一种解决方法,您可以选择jQuery -

$(function() {
// vertical align function
$.fn.vAlign = function() {
    return this.each(function(i){
        var ah = $(this).height();
        var ph = $(this).parent().height();       
        var mh = Math.ceil((ph-ah) / 2); //var mh = (ph - ah) / 2;
        $(this).css('margin-top', mh);
    });
};

$('.greenBorder img').vAlign();
//
});

Refer this post

相关问题