简体   繁体   English

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

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

I've got a table with two cells. 我有一张有两个牢房的桌子。 One has a link in it and the other has an image. 一个有链接,另一个有图像。

 <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> 

How can I make the whole of the first cell work as a link rather than just the text itself. 如何将整个第一个单元格作为链接而不仅仅是文本本身。

There may be different sized images so I can't rely on it being a fixed height. 可能有不同大小的图像,所以我不能依赖它是固定的高度。

jsfiddle 的jsfiddle

Since the image height is always changing, take the words out of the <a> . 由于图像高度总是在变化,所以请从<a>取出单词。 Make the link be positioned absolutely within the block so it takes up the whole space. 使链接绝对位于块内,以便占据整个空间。 This will make the width of the td's still be to the cell's content so that the link can cover the entire space. 这将使得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> 

you can do it like this : 你可以这样做:

 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> 

You can create a pseudo element to cover the area of the td: 您可以创建一个伪元素来覆盖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