How to apply text-align left in the first column and text-align right in the others

Koala7 picture Koala7 · Aug 16, 2012 · Viewed 45.7k times · Source

I have a Table to style, how can I apply text-align:left; just for the first column and text-align:right; for the other columns?

Here my Table example:

http://jsfiddle.net/gianlucaguarini/hAv7P/

At the moment the text-align is left for all the columns.

Answer

cloudfeet picture cloudfeet · Aug 16, 2012

You might want to look at the :first-child pseudo-class (W3C definition) - this is exactly the kind of thing it was designed for. You could use it like this:

.table td {
    text-align: right;
}

.table td:first-child {
    text-align: left;
}​

The :first-child pseudo-class means the selector only matches the first <td> in each <tr>.

Example here: http://jsfiddle.net/xv5Cn/1/