HTML 表格:使用 CSS 设置第二列的宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1720343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTML Table: Set width for second column onwards using CSS
提问by KalEl
Can I set the column width for all but first column using CSS?
我可以使用 CSS 为除第一列之外的所有列设置列宽吗?
回答by Dominic Rodger
HTML tables don't really have "columns" - rows just have first cells, at least as far as markup is concerned. However, you could do something like with CSS selectors:
HTML 表格并没有真正的“列”——行只有第一个单元格,至少就标记而言。但是,您可以使用 CSS 选择器执行类似操作:
Given the following markup:
鉴于以下标记:
<table>
<tr><td>foo</td><td>bar</td><td>bar 2</td></tr>
<tr><td>foo</td><td>bar</td><td>bar 2</td></tr>
<tr><td>foo</td><td>bar</td><td>bar 2</td></tr>
<tr><td>foo</td><td>bar</td><td>bar 2</td></tr>
</table>
CSS:
CSS:
table tr td { width: 20em; }
table tr td:first-child { width: 10em; }
This would set the width of the first "column" to 10em, and all other columns to 20em.
这会将第一个“列”的宽度设置为 10em,将所有其他列的宽度设置为 20em。
You might want to consider browser supportfor :first-child
though. The alternative is adding a class to the first <td>
in every <tr>
(it appears to be supported by pretty well every major browser other than IE6).
你可能要考虑浏览器支持的:first-child
,但。另一种方法是<td>
在每个中的第一个中添加一个类<tr>
(它似乎被除 IE6 之外的每个主要浏览器都很好地支持)。
回答by Web_Designer
Either of these would work:
这些中的任何一个都可以工作:
td:not(:first-child) {
width: 20%;
}
td:nth-child(n+2) {
width: 20%;
}
Both examples work in IE9+
两个例子都适用于 IE9+
Demo
演示
回答by Soufiane Hassou
Using CSS2.1, I'm not aware of any possible selector to do it.
使用 CSS2.1,我不知道有任何可能的选择器来做到这一点。
However, CSS3 has a not() selector, so if you give your first column a class, you can use a selector td:not(.your_class)
然而,CSS3 有一个 not() 选择器,所以如果你给你的第一列一个类,你可以使用一个选择器td:not(.your_class)