垂直居中动态高度div中的HTML元素

问题描述:


可能重复:

vertical-align:middle does not work

我可以确保< a>< / a> 垂直居中在这个div,不管div的大小:

How can i make sure that the <a></a> is vertically centered in this div, regardless of the size of the div:

http://jsfiddle.net/XF9WM/

感谢

使用 display:table $ c $ (IE 8 +):

Using display:table and display:table-cell, which works in all modern browsers (IE 8+):

.post {
    width: 200px;
    height: 200px;
    background: rgba(0,0,0,0.8);
    display:table;
}

.post h2 {
    text-align: center;
    width: 100%;
    height: 100%;
    display:table-cell;
    vertical-align: middle;
}

.post h2 a {
    color: #f7f7f7;
    font-size: 2.2em;
    font-weight: normal;
    font-style: italic;
    width: 100%;
}