繁体   English   中英

如何使链接填充不同高度的表格单元格?

[英]How do I make a link fill a table cell of varying height?

我有一张有两个牢房的桌子。 一个有链接,另一个有图像。

 <table> <tr> <td> <a href="https://google.com">My Link</a> </td> <td> <img src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /> </td> </tr> </table> 

如何将整个第一个单元格作为链接而不仅仅是文本本身。

可能有不同大小的图像,所以我不能依赖它是固定的高度。

的jsfiddle

由于图像高度总是在变化,所以请从<a>取出单词。 使链接绝对位于块内,以便占据整个空间。 这将使得td的宽度仍然是单元格的内容,以便链接可以覆盖整个空间。

 td{ border:1px solid; position:relative; } td a{ position:absolute; top:0; bottom:0; right:0; left:0; } 
 <table> <tr> <td> <a href="https://google.com"></a> My Link </td> <td> <img src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /> </td> </tr> </table> 

你可以这样做:

 a { display: block; height: 100%; } table{height: 100%;} td{height: 100%;} 
 <table> <tr> <td> <a href="https://google.com">My Link</a> </td> <td> <img src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /> </td> </tr> </table> 

您可以创建一个伪元素来覆盖td的区域:

 a:before { content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; z-index: 0; } td { position: relative; /*Make it relative to td*/ border: 1px solid; } 
 <table> <tr> <td> <a href="https://google.com">My Link</a> </td> <td> <img src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /> </td> </tr> </table> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM