我试图在<th>
元素中放置链接,并让<a>
元素填充<th>
的整个空间。虽然我可以用100%的宽度水平填充它,但是垂直填充会证明是麻烦的。身高:100%似乎没有效果。CSS:使块元素填充父元素的整个空间?
我想这样做,以便用户可以单击单元格中的任意位置以激活链接。我知道我可以把onClick属性放在<th>
本身上,并通过javascript处理它,但我想这样做的“正确”的CSS方式。
澄清:表的每一行可以有不同的高度,因为内容是动态的,所以使用固定高度的<a>
或<th>
元素的解决方案将无法正常工作。
下面是一些示例代码:
<style type="text/css">
th {
font-size: 50%;
width: 20em;
line-height: 100%;
}
th a {
display:block;
width:100%;
height:100%;
background-color: #aaa;
}
th a:hover {
background-color: #333
}
</style>
<table border=1>
<tr>
<th><a href="foo">Link 1</a></th>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor.</td>
</tr>
<tr>
<th><a href="foo">Link 2</a></th>
<td>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam dapibus tortor. </td>
</tr>
</table>
And here is a page with that exact same code
正如你可以看到,如果你将鼠标放置链接时,<a>
元素不是垂直填充产生一个尴尬的样子。我可以通过将悬停背景置于“th:悬停”样式来修复悬停背景,但如果您单击实际的a
标签所在的位置,链接仍然只能实际发挥作用。
只需增加100%的高度;) – gonxalo 2009-02-13 18:07:58