Vertically align an image inside a div with responsive height

Vertically align an image inside a div with responsive height

by Avinashullal

HTML

<div class="responsive-container">
    <div class="dummy"></div>
    <div class="img-container centerer">
        <img class="centered" src="http://placehold.it/150x200" alt="" />
    </div>
</div>

CSS

.responsive-container {
    position: relative;
    width: 100%;
    border: 1px solid black;
}
.dummy {
    padding-top: 100%;
    /* forces 1:1 aspect ratio */
}
.img-container {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
.centerer {
    text-align:center;
    /* Align center inline elements */
    font: 0/0 a;
    /* Hide the characters like spaces */
}
.centerer:before {
    content:' ';
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}
.centered {
    vertical-align: middle;
    display: inline-block;
    max-height: 100%;
    max-width: 100%;
}