Why is vertical-align: middle not working on my span or div?

Brad picture Brad · May 19, 2013 · Viewed 479k times · Source

I'm trying to vertically center a span or div element within another div element. However when I put vertical-align: middle, nothing happens. I've tried changing the display properties of both elements, and nothing seems to work.

This is what I'm currently doing in my webpage:

Here is a jsfiddle of the implementation showing that it doesn't work: http://jsfiddle.net/gZXWC/

Answer

Charles Addis picture Charles Addis · May 19, 2013

This seems to be the best way - some time has passed since my original post and this is what should be done now: http://jsfiddle.net/m3ykdyds/200

/* CSS file */

.main {
    display: table;
}

.inner {
    border: 1px solid #000000;
    display: table-cell;
    vertical-align: middle;
}

/* HTML File */
<div class="main">
    <div class="inner"> This </div>
</div>