If I have a table
with two columns, how do I specify a padding
or any other css so that it is applied just for the first column of <td>
s. Also how do I style an n-th column similarly?
Style the first column of a table differently
Asked
Active
Viewed 1.5e+01k times
105
-
1
http://quirksmode.org/css/selectors/firstchild.html - http://quirksmode.org/css/selectors/nthchild.html
– techfoobar
Mar 28 '13 at 05:52
5 Answers
206
You could use the n-th child selector.
to target the nth element you could then use:
td:nth-child(n) {
/* your stuff here */
}
(where n
starts at 1)
-
26
Suggest td:first-child over this mostly because it has more support for older IE versions. If browser support isn't an issue nth-child is a powerful selector to begin using.
– lukek
Mar 28 '13 at 06:05
-
6
Yes, for the first element, `first-child` is better. But OP asked for both :)
– RRikesh
Mar 28 '13 at 06:08
-
21
-
But if the first `td` of previous `tr` has a rowspan greater than 1, `first-child` will mis-select a 2nd-column `td`. Any trick to solve this?
– Frozen Flame
Jul 01 '23 at 01:59
14
If you've to support IE7, a more compatible solution is:
/* only the cells with no cell before (aka the first one) */
td {
padding-left: 20px;
}
/* only the cells with at least one cell before (aka all except the first one) */
td + td {
padding-left: 0;
}
Also works fine with li
; general sibling selector ~
may be more suitable with mixed elements like a heading h1 followed by paragraphs AND a subheading and then again other paragraphs.
FelipeAls
- 21,711
- 8
- 54
- 74
-
-
This actually worked for me, where nth-child and first-child did not (perhaps due to cascading rules? *shrug*)
– Brian A. Henning
Mar 09 '23 at 16:18
13
The :nth-child() and :nth-of-type() pseudo-classes allows you to select elements with a formula.
The syntax is :nth-child(an+b), where you replace a and b by numbers of your choice.
For instance, :nth-child(3n+1) selects the 1st, 4th, 7th etc. child.
td:nth-child(3n+1) {
/* your stuff here */
}
:nth-of-type() works the same, except that it only considers element of the given type ( in the example).
For more information about nth-child
https://developer.mozilla.org/es/docs/Web/CSS/:nth-child
Mauricio Gracia Gutierrez
- 10,288
- 6
- 68
- 99
MS Ibrahim
- 1,789
- 1
- 16
- 28
5
This should help. Its CSS3 :first-child where you should say that the first tr
of the table you would like to style. http://reference.sitepoint.com/css/pseudoclass-firstchild
tiantang
- 406
- 1
- 7
- 14
-
3
Also if you would like to only style the first `tr` of a specific table you could put `.tableclass tr:first-child` or `#tableid tr:first-child`
– tiantang
Mar 28 '13 at 05:56
-
1
1
To select the first column of a table you can use this syntax
tr td:nth-child(1n + 2){
padding-left: 10px;
}
Duck
- 34,902
- 47
- 248
- 470
Saptarshi Banerjee
- 11
- 1
-
1http://quirksmode.org/css/selectors/firstchild.html - http://quirksmode.org/css/selectors/nthchild.html – techfoobar Mar 28 '13 at 05:52
5 Answers
You could use the n-th child selector.
to target the nth element you could then use:
td:nth-child(n) {
/* your stuff here */
}
(where n
starts at 1)
-
26Suggest td:first-child over this mostly because it has more support for older IE versions. If browser support isn't an issue nth-child is a powerful selector to begin using. – lukek Mar 28 '13 at 06:05
-
6Yes, for the first element, `first-child` is better. But OP asked for both :) – RRikesh Mar 28 '13 at 06:08
-
21
-
But if the first `td` of previous `tr` has a rowspan greater than 1, `first-child` will mis-select a 2nd-column `td`. Any trick to solve this? – Frozen Flame Jul 01 '23 at 01:59
If you've to support IE7, a more compatible solution is:
/* only the cells with no cell before (aka the first one) */
td {
padding-left: 20px;
}
/* only the cells with at least one cell before (aka all except the first one) */
td + td {
padding-left: 0;
}
Also works fine with li
; general sibling selector ~
may be more suitable with mixed elements like a heading h1 followed by paragraphs AND a subheading and then again other paragraphs.

- 21,711
- 8
- 54
- 74
-
-
This actually worked for me, where nth-child and first-child did not (perhaps due to cascading rules? *shrug*) – Brian A. Henning Mar 09 '23 at 16:18
The :nth-child() and :nth-of-type() pseudo-classes allows you to select elements with a formula.
The syntax is :nth-child(an+b), where you replace a and b by numbers of your choice.
For instance, :nth-child(3n+1) selects the 1st, 4th, 7th etc. child.
td:nth-child(3n+1) {
/* your stuff here */
}
:nth-of-type() works the same, except that it only considers element of the given type ( in the example).
For more information about nth-child https://developer.mozilla.org/es/docs/Web/CSS/:nth-child

- 10,288
- 6
- 68
- 99

- 1,789
- 1
- 16
- 28
This should help. Its CSS3 :first-child where you should say that the first tr
of the table you would like to style. http://reference.sitepoint.com/css/pseudoclass-firstchild

- 406
- 1
- 7
- 14
-
3Also if you would like to only style the first `tr` of a specific table you could put `.tableclass tr:first-child` or `#tableid tr:first-child` – tiantang Mar 28 '13 at 05:56
-
1
To select the first column of a table you can use this syntax
tr td:nth-child(1n + 2){
padding-left: 10px;
}

- 34,902
- 47
- 248
- 470

- 11
- 1