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
.frame {
height: 25px; /* equals max image height */
line-height: 25px;
width: 160px;
border: 1px solid red;
text-align: center; margin: 1em 0;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
<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>
This might be useful:
Reference : http://www.student.oulu.fi/~laurirai/www/css/middle/
Try this solution with with pure css http://jsfiddle.net/sandeep/4RPFa/72/ May be the main problem with your html. Your not use quote when you define c
lass
&image height
in your html.CSS:
EDIT :
When i work around with the
img
tag it's leave3px to 2px
space fromtop
. Now i decreaseline-height
& it's work. css:the
line-height
property isrender
differently in different browsers. So; we have to define differentline-height
property browsersCheck this example http://jsfiddle.net/sandeep/4be8t/11/
Check this example about
line-height
different in different browsers input height differences in Firefox and ChromeUsing
table
andtable-cell
method do the job, specially because you targeting some old browsers as well, I create a snippet for you which you can run it and check the result:Also, you can use Flexbox to achieve the correct result:
You could do this:
demo
http://jsfiddle.net/DZ8vW/1
CSS
javascript
I have been playing around with using padding for center alignment. You will need to define the top level outer-container size, but the inner container should resize, and you can set the padding at different percentage values.
jsfiddle