How to vertically align an image inside a div?

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:

Show code snippet

Or, if you don't want to have an extra element in modern browsers and don't mind using Internet Explorer expressions, you can use a pseudo-element and add it to Internet Explorer using a convenient Expression, that runs only once per element, so there won't be any performance issues:

The solution with :before and expression() for Internet Explorer:

Hide code snippet

.frame {
    height: 25px;      /* Equals maximum image height */
    width: 160px;
    border: 1px solid red;
    white-space: nowrap;

    text-align: center;
    margin: 1em 0;

.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 {
    behavior: expression(
            t.insertAdjacentHTML('afterBegin','<span class="frame_before"></span>');
            t.runtimeStyle.behavior = 'none';
<div class="frame"><img src="" height=250px /></div>
<div class="frame"><img src="" height=25px /></div>
<div class="frame"><img src="" height=23px /></div>
<div class="frame"><img src="" height=21px /></div>
<div class="frame"><img src="" height=19px /></div>
<div class="frame"><img src="" height=17px /></div>
<div class="frame"><img src="" height=15px /></div>
<div class="frame"><img src="" height=13px /></div>
<div class="frame"><img src="" height=11px /></div>
<div class="frame"><img src="" height=9px /></div>
<div class="frame"><img src="" height=7px /></div>
<div class="frame"><img src="" height=5px /></div>
<div class="frame"><img src="" height=3px /></div>




<div class="frame">
  <img src="foo"/>


.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;