How to vertically align an image inside a div?
Question
How can you align an image inside of a containing div
?
Example
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
'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.
See the jsfiddle here
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.
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>
This might be useful:
div {
position:relative;
width:200px;
height:200px;
}
img {
position:absolute;
top:0;
bottom:0;
margin:auto;
}
.image {
min-height:50px
}
Reference : http://www.student.oulu.fi/~laurirai/www/css/middle/
matejkramny's solution is a good start, but oversized images have a wrong ratio.
Here's my fork:
demo: https://jsbin.com/lidebapomi/edit?html,css,output
HTML:
<div class="frame">
<img src="foo"/>
</div>
CSS:
.frame {
height: 160px; /*can be anything*/
width: 160px; /*can be anything*/
position: relative;
}
img {
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
链接地址: http://www.djcxy.com/p/6614.html
上一篇: MetadataException:无法加载指定的元数据资源
下一篇: 如何垂直对齐div中的图像?