您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在div中垂直对齐图像

如何在div中垂直对齐图像

唯一的(和最好的跨浏览器)的方式,因为我知道是使用inline- block佣工height: 100%和vertical-align: middle两个 元素。

.frame {

    height: 25px;      /* Equals maximum image height */

    width: 160px;

    border: 1px solid red;

    white-space: Nowrap; /* This is required unless you put the helper span closely near the img */



    text-align: center;

    margin: 1em 0;

}



.helper {

    display: inline-block;

    height: 100%;

    vertical-align: middle;

}



img {

    background: #3A6F9A;

    vertical-align: middle;

    max-height: 25px;

    max-width: 160px;

}


<div class="frame">

    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=250px />

</div>

<div class="frame">

    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=25px />

</div>

<div class="frame">

    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=23px />

</div>

<div class="frame">

    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=21px />

</div>

<div class="frame">

    <span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=19px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=17px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=15px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=13px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=11px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=9px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=7px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=5px />

</div>

<div class="frame">

    <span class="helper"></span>

    <img src="http://jsfiddle.net/img/logo.png" height=3px />

</div>

或者,如果您不想在现代浏览器中添加多余的元素并且不介意使用Internet Explorer表达式,则可以使用伪元素,然后使用方便的表达式将其添加到Internet Explorer,该表达式每个元素仅运行一次,因此不会有任何性能问题:

通过该解决方:before,并expression()为Internet Explorer:

.frame {

    height: 25px;      /* Equals maximum image height */

    width: 160px;

    border: 1px solid red;

    white-space: Nowrap;



    text-align: center;

    margin: 1em 0;

}



.frame:before,

.frame_before {

    content: "";

    display: inline-block;

    height: 100%;

    vertical-align: middle;

}



img {

    background: #3A6F9A;

    vertical-align: middle;

    max-height: 25px;

    max-width: 160px;

}



/* Move this to conditional comments */

.frame {

    list-style:none;

    behavior: expression(

        function(t){

            t.insertAdjacentHTML('afterBegin','<span class="frame_before"></span>');

            t.runtimeStyle.behavior = 'none';

        }(this)

    );

}


<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=250px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=25px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=23px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=21px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=19px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=17px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=15px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=13px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=11px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=9px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=7px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=5px /></div>

<div class="frame"><img src="http://jsfiddle.net/img/logo.png" height=3px /></div>

怎么运行的:

当两个inline-block元素彼此靠近时,可以使彼此的边对齐,这样vertical-align: middle您将得到如下所示: 两个对齐的块

当你有固定的高度(在一个块中pxem或另一个绝对值单元),可以设置在内部块的高度%。

所以,加入一个inline-block具有height: 100%与固定高度块将对准另一inline-block它元素(<img/>你的情况),垂直接近它。

其他 2022/1/1 18:13:33 有598人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶