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>
Want to align a image wich have after a text / title and both are inside a div?
See on JSfiddle or Run Code Snippet.
Just be sure to have an ID or a class at all your elements(div, img, title, etc).
For me works this solution on all browsers(for mobile devices you must to adapt your code with: @media).
Background image solution I removed the image element all together and set it as background of the div with a class of
.frame
http://jsfiddle.net/URVKa/2/
this at least works fine on IE8, FF6 and Chrome13
I checked, this solution will not work to shrink images larger then 25px height. There is a property called
background-size
which does set the size of the element, but it is CSS3 which would conflict with IE7 requirement.I wouldd advice you to either redo your browser priorities and design for the best available browsers, or get some serverside code to resize the images if you'd want to use this solution
The only (and the best cross-browser) way as I know is to use an
inline-block
helper withheight: 100%
andvertical-align: middle
on both elements.So there is a solution: http://jsfiddle.net/kizu/4RPFa/4570/
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:
The solution with
:before
andexpression()
for IE: http://jsfiddle.net/kizu/4RPFa/4571/How it works:
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:When you have a block with fixed height (in
px
,em
or other absolute unit), you can set the height of inner blocks in%
.inline-block
withheight: 100%
in a block with fixed height would align anotherinline-block
element in it (<img/>
in your case) vertically near it.http://jsfiddle.net/MBs64/
The key property is display: table-cell; for .frame. Div.frame is displayed as inline with this, so you need to wrap it in a block element.
This works in FF, Opera, Chrome, Safari and IE8+.
UPDATE
For IE7 we need to add a css expression:
You could try setting the CSS of PI to
display: table-cell; vertical-align: middle;
You can try below code