简体   繁体   English

如何获取锚标记以填充表格单元格的宽度?

[英]How to do I get an anchor tag to fill the width of a table cell?

I have a table cell with an anchor tag and I want the width (and height) of the anchor tag to fill the width of the containing table cell. 我有一个带有锚标记的表格单元,并且我希望锚标记的宽度(和高度)填充包含的表格单元的宽度。 Intuition doesn't seem to work on this. 直觉似乎没有用。 Please help. 请帮忙。

HTML: HTML:

<table>
    <tr>
        <td class="width_is_100px">
            <a href="http://www.doesnotwork.com"><span class="make_width_100px">Some Text</span></a>
        </td>
    </tr>
</table>

CSS: (doesn't work) CSS :(无效)

.make_width_100px {
    width:100px !important;
}

The anchor tag is only as wide as the text "Some Text". 定位标记仅与文本“ Some Text”一样宽。 I want the user to be able to click anywhere inside the table cell and trigger the link. 我希望用户能够单击表格单元格内的任何位置并触发链接。 No javascript please. 没有JavaScript,请。

Try it: 试试吧:

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

Make your <a> element a block element (it's inline by default): 将您的<a>元素设置为块元素(默认情况下为内联):

.width_is_100px a {
    display:block;
}

None of these answers work properly to fill the width and height when the text wraps . 当文本自动换行时 ,这些答案都不能正确地填充宽度和高度。

The only answer I found that produces a proper result filling the height and width of the cell right to the edges is this one : 唯一的答案我发现,产生正确的结果填充到边缘单元右侧的高度和宽度是这样一个

td {
    overflow: hidden;
}

td > a {
    display: block;
    margin: -10em;
    padding: 10em;
}
<table>
    <tr>
        <td class="width_is_100px">
            <a href="http://www.doesnotwork.com"><div style="width:100%;">Some Text</div></a>
        </td>
    </tr>
</table>

this is probably too late but, this worked for me in tables with different content height. 这可能为时已晚,但这在具有不同内容高度的表中对我有用。

html html

<table class="example">
    <tr>
        <td><a href="">small content</a></td>
        <td><a href="">multiline<br>content</a></td>
    <tr>
</table>

css 的CSS

table{ 
    padding:10px;  /*for example*/
}
table.example td{
    overflow:hidden;
    padding:0px; /*you can need !important*/
}
table.example a{
    display: block;
    height: 100%;
    width: 100%;
    padding: 10px 10px 500px 10px; /*table default padding except on bottom 500px for example*/
    margin-bottom: -490px; /*same as bottom padding - table padding*/
}

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

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