Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: I want to target each child separately in a container

I want to assign seperate colors to each <td>. One solution is to use classes, but I don't want to crowd the HTML if a simple CSS selector solution exists.

HTML:

<tr>
  <td>Item 1</td>
  <td>Item 2</td>
  <td>Item 3</td>
  <td>Item 4</td>
</tr>

CSS:

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

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

/* item #3 */
{color: green}
like image 921
Don P Avatar asked Apr 22 '13 17:04

Don P


2 Answers

Use CSS's nth-child selector:

td:nth-child(1) {
    color:blue;
}
td:nth-child(2) {
    color:red;
}
td:nth-child(3) {
    color:brown;
}
td:nth-child(4) {
    color:green;
}

jsFiddle example

like image 52
j08691 Avatar answered Sep 23 '22 22:09

j08691


You can use the nth-child selector to specify a specific element (counting from 1): td:nth-child(1) { color: red; }

http://jsfiddle.net/ayTmy/

like image 26
Explosion Pills Avatar answered Sep 24 '22 22:09

Explosion Pills