如何垂直对齐div内的图像? [英] How to vertically align an image inside a div?
问题描述
如何在包含 div
的内部对齐图像?
How can you align an image inside of a containing div
?
在我的示例中,我需要垂直居中< img>
在< div>
中, class =frame
:
In my example, I need to vertically center the <img>
in the <div>
with class ="frame
" :
<div class="frame" style="height: 25px;">
<img src="http://jsfiddle.net/img/logo.png" />
</div>
.frame
的高度是固定的图像的高度未知。我可以在 .frame
中添加新元素,如果这是唯一的解决方案。我试图在IE≥7,Webkit,Gecko上这样做。
.frame
's height is fixed and image's height is unknown. I can add new elements in .frame
if that's the only solution. I'm trying to do this on IE≥7, Webkit, Gecko.
参见jsfiddle 这里
See the jsfiddle here
推荐答案
唯一(也是最好的跨浏览器)方式我知道是使用内联块
帮助器,其中高度:100%
和 vertical-两个元素对齐:中间
。
The only (and the best cross-browser) way as I know is to use an inline-block
helper with height: 100%
and vertical-align: middle
on both elements.
所以有一个解决方案: http://jsfiddle.net/kizu/4RPFa/4570/
So there is a solution: http://jsfiddle.net/kizu/4RPFa/4570/
.frame {
height: 25px; /* equals max 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=250 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=15 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=13 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=11 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=9 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=7 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=5 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=3 />
</div>
或者,如果你不想在现代浏览器中有额外的元素并且不介意使用IE表达式,你可以使用伪元素并使用方便的表达式将其添加到IE,每个元素只运行一次,因此不会出现任何性能问题:
Or, if you don't want to have an extra element in modern browsers and don't mind using IE expressions, you can use a pseudo-element and add it to IE using a convenient Expression, that runs only once per element, so there won't be any performance issues:
:IE之前的
和表达式()
: http://jsfiddle.net/kizu/4RPFa/4571/
.frame {
height: 25px; /* equals max 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=250 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=25 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=23 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=21 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=19 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=17 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=15 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=13 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=11 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=9 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=7 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=5 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=3 /></div>
工作原理:
-
你有两个
内嵌块
元素彼此靠近,你可以将每个元素对齐到另一边,所以用vertical-align:middle
你会得到这样的东西:
When you have two
inline-block
elements near each other, you can align each to other's side, so withvertical-align: middle
you'll get something like this:
当你有一个固定高度的块(在 px
, em
或其他绝对单位),您可以在<$ c $中设置内部块的高度c>%。
When you have a block with fixed height (in px
, em
or other absolute unit), you can set the height of inner blocks in %
.
这篇关于如何垂直对齐div内的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!