如何使表格单元格内的div占据整个单元格的高度?
如果单元格没有固定高度,将div的高度设置为100%是不起作用的。我无法设置固定高度,因为单元格的高度必须根据变量内容而定。
我试图让每行中的所有div具有相同的完整行高。
以下是代码,在http://www.songtricks.com/CellDivBug.html上可以查看实时示例。
如果单元格没有固定高度,将div的高度设置为100%是不起作用的。我无法设置固定高度,因为单元格的高度必须根据变量内容而定。
我试图让每行中的所有div具有相同的完整行高。
以下是代码,在http://www.songtricks.com/CellDivBug.html上可以查看实时示例。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
td
{
padding:0px;
vertical-align:top;
height:auto;
}
.box
{
margin:0px;
border:solid 2px red;
height:100%;
}
</style>
</head>
<body>
<table border="1" width="50%">
<tr>
<td width="50%">
<div class="box">
This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text. This box has a lot of text.
</div>
</td><td width="50%">
<div class="box">
This box has a little text.
</div>
</td>
</tr>
</table>
</body>
</html>
经过更多的研究和实验,我找到了可能是唯一使用CSS解决问题的方法。由于我太新手无法回答自己的问题,所以在这里发布。
具体步骤如下:
- 在表格单元格上添加position:relative
- 在包含的div上添加position:absolute; top:0; left:0; right:0; bottom:0;
- 将内容直接放置在单元格内,与div并排放置,而不是在其中,以强制单元格采用内容的高度
table
(或 JavaScript)的方法来完成这个任务。我应该发布吗?但是这种方法有点复杂。 - thirtydot