We’ve all seen margin: 0 auto; for horizontal centering, but margin: auto; has refused to work for vertical centering… until now!

But there are certain ways by which you can achieve it without any fuss!

1. Flexbox

CSS >

.v-center-flex {
    display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6 */
    display: -moz-box; /* OLD - Firefox 19- */
    display: -ms-flexbox; /* TWEENER - IE 10 */
    display: -webkit-flex; /* NEW - Chrome */
    display: flex; /* NEW, Opera 12.1, Firefox 20+ */
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    height: 200px;
    background: red;
}
.v-center-flex div {
    margin: 0 auto;
}

HTML >

<div class='v-center-flex'>
    <div>Vertically-Center!!!</div>
</div>

Demo


2. Table

CSS >

.v-center-table {
    display: table;
    width: 100%;
    height: 200px;
    text-align: center;
    background: blue;
}
.v-center-table div {
    display: table-cell;
    vertical-align: middle;
}

HTML >

<div class='v-center-table'>
    <div>Vertically-Center!!!</div>
</div>

Demo


3. Ghost (LOL :D)

CSS >

.v-center-ghost {
    width: 100%;
    height: 200px;
    position: relative;
    text-align: center;
}
.v-center-ghost::before {
    content: " ";
    display: inline-block;
    width: 0%;
    height: 100%;
    vertical-align: middle;
}
.v-center-ghost p {
    display: inline-block;
    vertical-align: middle;
    width: 200px;
    margin: 0;
    padding: 20px;
    color: #FFF;
    background: black;
}

HTML >

<div class='v-center-ghost'>
    <p>Vertically-Center!!!</p>
</div>

Demo


4. Transform

CSS >

.v-center-transform {
    width: 200px;
    height: 100px;
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background: yellow;
}

HTML >

<div class='v-center-transform'>
    <div>Vertically-Center!!!</div>
</div>

Demo


5. Position

CSS >

.v-center-position div{
    width: 50%;
    height: 150px;
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    overflow: auto;
    margin: auto;
    z-index : 999;
    background: green;
}

HTML >

<div class='v-center-position'>
    <div>Vertically-Center!!!</div>
</div>

Demo