CSS:我想在容器中单独定位每个孩子

Don*_*n P 6 html css css-selectors css3

我想为每个人分配单独的颜色<td>.一种解决方案是使用类,但如果存在简单的CSS选择器解决方案,我不想挤占HTML.

HTML:

<tr>
  <td>Item 1</td>
  <td>Item 2</td>
  <td>Item 3</td>
  <td>Item 4</td>
</tr>
Run Code Online (Sandbox Code Playgroud)

CSS:

/* item #1 */
{color: red}

/* item #2 */
{color: blue}

/* item #3 */
{color: green}
Run Code Online (Sandbox Code Playgroud)

j08*_*691 9

使用CSS的nth-child选择器:

td:nth-child(1) {
    color:blue;
}
td:nth-child(2) {
    color:red;
}
td:nth-child(3) {
    color:brown;
}
td:nth-child(4) {
    color:green;
}
Run Code Online (Sandbox Code Playgroud)

jsFiddle例子


Exp*_*lls 5

您可以使用nth-child选择器指定特定元素(从1开始计算):td:nth-child(1) { color: red; }

http://jsfiddle.net/ayTmy/