垂直对齐表格单元格不适用于绝对位置

Jen*_*ell 25 css vertical-alignment css-tables

http://jsfiddle.net/fQv97/

HTML

<div class="table-cell">
    My text, should be align middle
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.table-cell {
    height: 200px;
    width: 200px;
    vertical-align: middle;
    background: #eee;
    display: table-cell;
    position: absolute;
}
Run Code Online (Sandbox Code Playgroud)

问题

文本应放在我的"表格单元格"的中间.一切都按预期工作,直到我添加"position:absolute".现在它不能再将我的内容放在中间了?为什么不?它仍然知道我的高度和宽度,因为我在我的CSS中设置它.

有什么聪明的解决方法吗?

thi*_*dot 40

一切都按预期工作,直到我添加"position:absolute".现在它不能再将我的内容放在中间了?为什么不?

position: absolute部队display: block,在这里读二号.

至于解决方法,我认为你必须将它包装在另一个元素中:

<div class="table-cell-wrapper">
    <div class="table-cell">
        My text, should be align middle
    </div>
</div>

.table-cell-wrapper {
    position: absolute;
}
.table-cell {
    height: 200px;
    width: 200px;
    vertical-align: middle;
    background: #eee;
    display: table-cell;
}
Run Code Online (Sandbox Code Playgroud)