建立< div>填充其父级< td>高度,而不指定< td>上的高度 [英] Make a <div> fill its parent <td> height, without specifying height on <td>

查看:95
本文介绍了建立< div>填充其父级< td>高度,而不指定< td>上的高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有类似的问题,但它在我的情况下不工作。

I know there is a similar question here, but it doesn't work in my case.

<table class="Layout">
    <tbody>
        <tr>
            <td><div class="Key">HEY</div></td>
            <td><div class="Key">HEY</div></td>
            <td><div class="Key">HEY</div></td>
            <td><div class="Key">HEY</div></td>
            <td><div class="Key">HEY</div></td>
            <td><div class="Key">HEY</div></td>
            <td><div class="Key">HEY</div></td>
        </tr>
    </tbody>
</table>

如何使我的 div.Key 元素具有与它们的父< td> 相同的高度,而不指定高度< td> 元素

How can I make my div.Key elements have the same height as their parent <td> without specifying a height to <td> elements ?

我的表会动态变化(添加和删除列和行),所以我不能强制< td> ; 元素为绝对大小,我也不想每次修改表时都计算并给出相对大小。 .Layout 表大小也动态更改,并且将始终为绝对值。 (因为某些原因,我也不想使用row / colspan来防止有人这样说。)

My table will dynamically change (adding and deleting columns and rows) so I can't force the <td> element to an absolute size, and I would also like not to have to compute and give them a relative size every time I modify the table. The .Layout table size also changes dynamically and will always be an absolute value. (For some reasons I would also like not to use row/colspan in case someone suggests that.)

任何想法?

推荐答案

通过在每个 div.Key 中添加一个内部div并使用 :table;和display:table-cell; 这样做我需要的。这里是代码:

I found a solution by adding an inner div into each div.Key and using display: table; and display: table-cell; This does what I need. Here is the code:

.Layout {
    width: 1024px; /* TEST */
    height: 480px; /* TEST */
}
.Key {
    text-align: center;
    display: table;
    width: 100%;
    height: 100%;
}
.Key > div {
    display: table-cell;
    vertical-align: middle;
}

和:

<table class="Layout">
    <tbody>
        <tr>
            <td><div class="Key"><div>HEY</div></div></td>
            <td><div class="Key"><div>HEY</div></div></td>
            <td><div class="Key"><div>HEY</div></div></td>
            <td><div class="Key"><div>HEY</div></div></td>
            <td><div class="Key"><div>HEY</div></div></td>
            <td><div class="Key"><div>HEY</div></div></td>
            <td><div class="Key"><div>HEY</div></div></td>
        </tr>
    </tbody>
</table>

这篇关于建立&lt; div&gt;填充其父级&lt; td&gt;高度,而不指定&lt; td&gt;上的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆