如何将 html 表格最后一列的宽度设置为可用宽度的百分比?

发布于 2024-11-08 12:19:45 字数 396 浏览 2 评论 0原文

我有一个有两列的表格。我通过将第一列的标题宽度设置为 30% 来设置第一列的宽度:

#TableFirstHeader
{
    width: 30%;
}

效果很好,但如果我尝试将第二列标题的宽度设置为 65%:

#TableSecondHeader
{
    width: 65%;
}

它会被忽略。我可以将该值设置为任何值,并且第二列始终延伸到表的末尾。

我真正想要的是第二列和表格右侧之间有大约 5% 的间隙。

我尝试通过设置宽度为 5% 的标题来创建一个空列,其中没有任何内容,但这没有任何作用。

我怎样才能让该列只使用可用总宽度的 65%(或者像可用宽度的 90%,这也可以)?

I've got a table with two columns. I set the width of the first column by settings its header's width to 30%:

#TableFirstHeader
{
    width: 30%;
}

That works great, but if I try to set the width of the second header to 65%:

#TableSecondHeader
{
    width: 65%;
}

it is simply ignored. I can set that value to anything, and the second column always stretches to the end of the table.

All I really want is about a 5% gap between the second column and the right side of the table.

I've tried creating an empty column by setting a header of width 5% with nothing in it, but that doesn't do anything.

How can I get that column to use only 65% of the total width available (or like 90% of the width available to it, that would be fine, too)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-11-15 12:19:45
<style type="text/css">
    table#YourTableId { width:95%; }
    th#TableFirstHeader { width:31.6%; }
    th#TableSecondHeader { width:68.4%; }
</style>

第二个不一定需要分配宽度。你也可以用它

    table#YourTableId td:first-child { width:31.6%; }

来代替。品味问题。

从这里编辑:

由于您在上面的评论中已经澄清,无论出于何种原因,行都需要是表格容器宽度的 100%,如果最后一列可能有额外的填充,我会支持 jeroen 的解决方案。

如果您必须将第二列设置为表格宽度的 65%(而不是 70%,右填充 5%):

<style type="text/css">
    th#TableFirstHeader { width:30%; }
    th#TableSecondHeader { width:65%; }
    th#TableThirdHeader { width:5%; }
    div#Stretch { width:100%; }
</style>

<table>
    <tr>
    <th id="TableFirstHeader">Some Content</th>
    <th id="TableSecondHeader">Some Content</th>
    <th id="TableThirdHeader"><div id="Stretch"></div></th>
    </tr>
</table>

空的第三列将会折叠。容器宽度为 100% 的空 div 可以防止这种行为。

<style type="text/css">
    table#YourTableId { width:95%; }
    th#TableFirstHeader { width:31.6%; }
    th#TableSecondHeader { width:68.4%; }
</style>

The second th does not necessarily need to be assigned a width. Also you could use

    table#YourTableId td:first-child { width:31.6%; }

instead. Matter of taste.

Edited from here:

Since you have clarified in the above comment, that - for whatever reason - the rows need to be 100% the table's container's width, I second jeroen's solution if the last column may have extra padding.

If you must have the second column at 65% the table's width (and not 70% with 5% right padding):

<style type="text/css">
    th#TableFirstHeader { width:30%; }
    th#TableSecondHeader { width:65%; }
    th#TableThirdHeader { width:5%; }
    div#Stretch { width:100%; }
</style>

<table>
    <tr>
    <th id="TableFirstHeader">Some Content</th>
    <th id="TableSecondHeader">Some Content</th>
    <th id="TableThirdHeader"><div id="Stretch"></div></th>
    </tr>
</table>

An empty third column would collapse. The empty div with 100% width of it's container prevents that behavior.

听风念你 2024-11-15 12:19:45

不能,表格的所有列组合在一起就构成了表格的宽度。

我不知道您到底想要实现什么,但也许您可以在最后一列的单元格上添加正确的填充,以获得您想要的效果或类似的效果。

You can´t, all columns of a table combined make up the width of the table.

I don´t know what you are trying to achieve exactly, but perhaps you can add a right padding on the cells of the last column to get the effect you want or something similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文