使用CSS如何仅更改表的第2列

117 css

仅使用css,如何仅覆盖表的第2列的css.

我可以使用以下方法访问所有列:

.countTable table table td
Run Code Online (Sandbox Code Playgroud)

我无法访问此页面上的html来修改它,因为它不是我的网站.

谢谢.

Nic*_*ver 223

您可以:nth-child像这样使用伪类:

.countTable table table td:nth-child(2)
Run Code Online (Sandbox Code Playgroud)

请注意,这在旧版浏览器(或IE)中不起作用,在这种情况下,您需要为单元格提供类或使用javascript.

  • @Deeksy - 这不准确,它不关心选择器是什么.在找到元素后应用`nth-child`,并且无论它是否在选择器中,它都与它拥有的任何父元素进行比较.你可以在这里看到这个:http://jsfiddle.net/JQQPz/ (3认同)

Mar*_*chi 26

试试这个:

.countTable table tr td:first-child + td
Run Code Online (Sandbox Code Playgroud)

您还可以重申以设置其他列的样式:

.countTable table tr td:first-child + td + td {...} /* third column */
.countTable table tr td:first-child + td + td + td {...} /* fourth column */
.countTable table tr td:first-child + td + td + td +td {...} /* fifth column */
Run Code Online (Sandbox Code Playgroud)


Abz*_*ozy 16

要仅更改表的第二列,请使用以下内容:

一般案例:

table td + td{  /* this will go to the 2nd column of a table directly */

background:red

}
Run Code Online (Sandbox Code Playgroud)

你的情况:

.countTable table table td + td{ 

background: red

}
Run Code Online (Sandbox Code Playgroud)

注意:这适用于所有浏览器(现代和旧版),这就是为什么我在旧问题中添加了答案