使表格单元格中的链接填充整个行高


68

我有一个数据表,每个单元格都是一个链接。我想允许用户单击表格单元格中的任意位置,并让他们点击链接。有时表单元格不止一行,但并非总是如此。我使用td {display:block}来获取覆盖大部分单元格的链接。当一行中有一个单元格为两行,而其他单元格只有一行时,一个衬板不会填满表格行的整个垂直空间。这是示例HTML,您可以在http://www.jsfiddle.net/RXHuE/上看到它的运行方式:

<head>
<style type="text/css">
  td {width: 200px}
  td a {display: block; height:100%; width:100%;}
  td a:hover {background-color: yellow;}
</style>
<title></title>
</head>
<body>
<table>
  <tbody>
    <tr>
      <td>
        <a href="http://www.google.com/">Cell 1<br>
        second line</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 2</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 3</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 4</a>
      </td>
    </tr>
  </tbody>
</table>
</body>

+1这是我第一次听说“ display:table”。
亚伦·迪古拉

是的,我尝试使用“ display:inline-block”,它实际上在Webkit浏览器中有效,但在firefox或IE中不起作用。然后,我读到“ display:table”是相似的,所以我想尝试一下,最后用这个发表问题。此后,我已将问题编辑为使用“显示:阻止”,因为这是我的第一个想法。
Brian Fisher

我几乎问同样的问题:stackoverflow.com/questions/9127274/...
蒂姆·布泰

Answers:


46

您需要对CSS进行一些小的更改。使之td height:100%;适用于IE 8和FF 3.6,但不适用于Chrome。

td {
  width: 200px;
  border: solid 1px green;
  height: 100%
}
td a {
  display: block;
  height:100%;
  width:100%;
}

但要做出height50px作品的Chrome除了IE和FF

td {
  width: 200px;
  border: solid 1px green;
  height: 50px
}
td a {
  display: block;
  height:100%;
  width:100%;
}

编辑:

您已经在这里的另一篇文章中自己给出了解决方案;这是要用的display: inline-block;。与我的Chrome,FF3.6,IE8解决方案结合使用时

td {
  width: 200px;
  border: solid 1px green;
  height: 100%}
td a {
  display: inline-block;
  height:100%;
  width:100%;
}

更新资料

以下代码在IE8,FF3.6和chrome中对我有效。

的CSS

td {
  width: 200px;
  border: solid 1px green;
  height: 100%;
}
td a {
  display: inline-block;
  height:100%;
  width:100%;
}
td a:hover {
  background-color: yellow;
}

的HTML

<table>
  <tbody>
    <tr>
      <td>
        <a href="http://www.google.com/">Cell 1<br>
        second line</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 2</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 3</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 4</a>
      </td>
    </tr>
  </tbody>
</table>

例子在这里


好的建议,但是,我不想固定细胞的高度。我基本上是在向用户显示数据表,有时一行中有一个包含大量数据的单元格,导致该行要包装,但有时一行中没有此类单元格。
Brian Fisher

@Brian:将身高设为100%是否对您也不可行?
加拉夫·萨克森纳

@Gaurav:我可以将td和链接的高度设置为100%,我只是不想给它们设置高度(否则它们有时会比需要的高)。如果我在其他地方给出答案,那也将很有趣。
Brian Fisher

1
讨厌这么说,但是下面zobier的答案是此问题的答案。完全不需要JavaScript。
Ricardo Zea

3
不幸的是,链接的示例仅在怪癖模式下有效。放在<!DOCTYPE html>文档的顶部,它会断开。
Jez

107

在块元素上设置任意大的负边距和相同的填充,并在父元素上隐藏溢出。

td {
    overflow: hidden;
}
td a {
    display: block;
    margin: -10em;
    padding: 10em;
}

http://jsfiddle.net/RXHuE/213/


2
我喜欢这一个。我没有使用JS。
mikato

1
在当前的Firefox(27),Chrome(33)和IE(11)中运行良好。
Alex

2
最佳解决方案。还可以让您仍将文本居中放置其他解决方案似乎存在问题的文本。唯一的问题是,在Android版Chrome(可能还有其他浏览器)上,当您点击某个链接时,整个大链接都会显示出来,包括<a>以外的部分。可使用-webkit-tap-highlight-color修复:rgba(0,0,0,0);
Dirbaio 2014年

4
这很聪明,但不幸的是,如果您使用表格单元格进行响应式导航,则会中断操作,并且您需要该导航的下拉菜单。
Matthew Dean

5
你这个肮脏,肮脏,性感的男人。这是邪恶的天才,我喜欢它。
杰森2014年

6

聚会晚了一点,但是我刚刚发现了一个不错的解决方案。

您可以结合使用相对和绝对定位的元素以及伪元素来获得所需的效果。无需额外的标记!

将表格单元格(<td>)更改为position: relative;,并在标记上创建一个::before ::after伪元素<a>,并将其设置为position: absolute;,然后使用top: 0; left: 0; right: 0; bottom: 0;

由于伪元素已附加到锚定标记,并且您要告诉它占用整个表单元格,因此它将迫使锚定标记至少具有该大小,而不会影响锚定标记本身的实际内容(从而保持其垂直居中对齐)。

例如

table {
  border-collapse: collapse;
  table-layout: fixed;
}

td {
  position: relative;
  width: 200px;
  padding: 0.5em 1em;
  border: 2px solid red;
  
  background-color: lime;
}

td a {
  /* FONT STYLES HERE */
  text-decoration: none;
}

td a::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 0;
}
<table>
  <tbody>
    <tr>
      <td>
        <a href="http://www.google.com/">Cell 1<br>
        second line</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 2</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 3</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 4</a>
      </td>
    </tr>
    <tr>
      <td>
        <a href="http://www.google.com/">Cell 5</a>
      </td>
      <td>
        <a href="http://www.google.com/">Cell 6<br>
        second line</a>
      </td>
    </tr>
  </tbody>
</table>

希望这可以帮助!


这似乎会拉伸可点击区域,但不会扩展背景色:i.imgur.com/NDO61sV.png
mpen

2
也将背景颜色应用于after元素... td a, td a:after { background-color: red; }
Jack_Hu

1
您可能还需要调整使用的:: before或:: after伪元素的z-index,以便背景颜色不会阻止您查看单元格的实际内容。
鲁比

奇迹般有效!
罗伯·科德斯

@rooby-@mpen-改进我以前的评论,比使用z-index更容易,并且更好的做法是调整background-colorontd而不是a标签。请参阅我的答案中的重做示例。
Jack_Hu

4

以下骇客作品[在Chrome / Firefox / Safari上测试]具有相同的td和anchor元素填充。对于锚点,也具有等于填充值-ve的余量。

的HTML

<table>
    <tr>
        <td><a>Hello</a></td>
    </tr>
</table>

CSS:

td {                          
    background-color: yellow;                                                                              
    padding: 10px;                                                                                                            
}  
a {
    cursor:pointer;
    display:block;
    padding: 10px;
    margin: -10px;
}

工作小提琴:http : //jsfiddle.net/JasYz/


1

尝试display: block

td a {display: block; height:100%;}

[编辑] WTF ...我可以确认这在FF 4和Chrome中不起作用。这有效:

td a {display: block;  height: 2.5em; border: 1px solid red;}

这表明height:100%;未在表单元格中定义。可能是因为单元格从内容中获取了它的大小(所以内容不能说“告诉我您的大小”,因为那样会导致循环)。如果像这样设置单元格的高度,它甚至将无法工作:

td {width: 200px; height: 3em; padding: 0px}

同样,上面的代码将失败。因此,我的建议是为链接使用定义的高度(您可以忽略宽度;默认情况下,块元素的宽度为100%)。

[EDIT2]我已经在http://www.cssplay.co.uk/menus/上浏览了一百个示例,但是它们都没有混合使用单行和多行单元格。好像您碰到了盲点。


谢谢你的建议。实际上,这是我首先尝试的方法,然后更改了帖子中的代码以使用该代码。这样链接就可以占据表格单元的整个水平空间,而不是垂直空间。
Brian Fisher

@布赖恩·费舍尔(Brian Fisher):那么还有其他问题。在表格单元格和链接周围添加边框,以查看其实际大小。如果链接是单元格中的唯一元素,并且该单元格没有填充等,height: 100%;则应解决此问题。
亚伦·迪古拉

@Brian:或者换句话说:单元格应该从上面代码中的链接获取高度,所以您永远不能在链接之外有任何空间。
亚伦·迪古拉

所以我在单元格周围放了一个边框,看起来单元格没有填满行的整个高度。我尝试将高度设置为:将100%设为标准,但这没有用。还有其他想法吗?
Brian Fisher

如果我固定td的高度,然后在IE 8,FF3.6和Chrome中将td a的高度设置为100%,则对我有用。我并不是很想固定td的高度,因为有时行将是1、2或3行,具体取决于内容。WTF很棒,如果可以的话,我给你加2。
Brian Fisher

1

正如我对自己的问题所做的一样,我将在此处发布相同的答案。

Jannis M回答启发,我做了以下工作:

$(document).ready(function(){    
    $('table tr').each(function(){
        var $row = $(this);
        var height = $row.height();
        $row.find('a').css('height', height).append('&nbsp;');  
    });       
});

我添加了一个&nbsp;因为空链接(不包含文本节点)不能设置样式(?)。

请参阅我更新的小提琴


1
您应该查看Gaurav Saxena的答案,这是仅CSS的解决方案,在所有当前的浏览器上均能正常工作。
布莱恩·费舍尔

0

唯一的问题是使用display:块会强制浏览器忽略垂直align:center ...

哎呀

我判断它是否正确,以找到一个高度为60的单元格,并通过添加一个br来占据20像素的字体。然后我意识到我有一些2行文字的项目。ang

我最终使用了javascript。javascript并没有给出很好的鼠标指向性单击器,但是文本行却显示了,因此它实际上会触发视觉响应,而不是我想要的位置...然后,JavaScript将捕获所有“未命中的单击”实际的href。

也许不是最优雅的解决方案,但目前为止效果很好。

现在,如果我只能弄清楚如何正确执行此操作...。

关于如何将鼠标图标添加到onclick覆盖区域的手的任何想法?现在,单击页面有效,但是仅当图标单击href时才更改,这只会影响文本。


0

对我来说,唯一的解决方案是<table> <tr><div>s替换并使用display:tabledisplay:table-row相应地设置样式。

然后,您可以<td>用just替换<a>并用设置样式display:table-cell

即使在不同高度的<td>内容上也能完美工作。

因此原始HTML没有锚点:

<table>
  <tr>
    <td>content1<br>another_line</td>
    <td>content2</td>
  </tr>
</table>

现在变成:

a:hover
{
  background-color:#ccc;
}
    <div style="display:table; width:100%">
      <div  style="display:table-row">
        <a href="#" style="display:table-cell; border:solid 1px #f00">content1<br>another_line</a>
        <a href="#" style="display:table-cell; border:solid 1px #f00">content2</a>
      </div>
    </div>


0

我已经使用了这种解决方案:在我的情况下,效果要比其余的更好。

CSS:

.blocktd {width: 100%; height: 100%; padding: 0px; overflow: hidden}

a.blocktd {margin: 0em;  padding: 50px 20px 50px 20px; display: block;}

a.blocktd:hover {border: 4px solid #70AEE8; border-radius: 10px; padding: 46px 16px 46px 16px; transition:  0.2s;}

在HTML中: <a href="..." class="blocktd">...</a>


-1

您为什么不只摆脱<a>高音扬声器而直接将其添加onClick<td>

<head>
<style type="text/css">
td {
    text-align:center;
}
td:hover {
    cursor:pointer;
    color:#F00;
}
</style>
<title></title>
</head>
<body>
<table>
    <tbody>
        <tr>
            <td onclick="location.href='http://www.google.com/';">Cell 1<br />second line</td>
            <td onclick="location.href='http://www.google.com/';">Cell 2</a></td>
            <td onclick="location.href='http://www.google.com/';">Cell 3</td>
            <td onclick="location.href='www.google.com';">Cell 4</td>
        </tr>
    </tbody>
</table>

这样,您可以裁掉中间人。

PS:我知道这是很多年前提出并回答的,但是以上答案都无法解决我的问题。希望这对某人有帮助。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.