tags:

views:

82

answers:

4

I have an ordinary HTML table:

<table>
  <tr>
    <td class="first-column-style">FAT</td> 
    <td>...</td>
  </tr>
  <tr>
    <td class="first-column-style">FAT</td>
    <td>...</td>
  </tr>
</table>

I want to apply CSS style to every table cell (td) in a particular column. Is it possible to do that without applying the class/style attribute to every table cell in that column, and without JavaScript?

+3  A: 

Use the <col> tag and style it following this guide. This way you only need to add a class (or inline style specification) to the <col> element instead of each <td> in the table.

Deniz Dogan
note: <col> accepts only a few style properties (like text-align or width)
Fabrizio Calderan
A: 

Well for the first and last columns you can use the :first-child and :last-child pseudo class:

/* make the first cell of every row bold */
tr td:FIRST-CHILD{
    font-weight:bold;
}

/* make the last cell of every row italic */
tr td:LAST-CHILD{
    font-style:italic;
}

Reference:

seanizer
`:first-child` is only [almost supported](http://www.quirksmode.org/css/contents.html) by IE8 and `:last-child` is not supported at all.
Deniz Dogan
Same old song. Sorry, haven't used IE in years. Works like a charm in FF and Chrome.
seanizer
A Downvote? why? This is a valid CSS solution. You should send your downvotes to Redmond for building buggy browsers ...
seanizer
A: 

.table_class tr td { padding:10px; font-weight:bold; }

apply above class table_class to table.

Eswar
This will select every data cell in a table, the question is looking to limit the styles to a specific *column*.
David Dorward
+1  A: 

Additionally to seanizer's solution you can combine :first-child with the adjacent sibling selector + (also not supported by IE6):

td:first-child { /* first column */ }

td:first-child + td { /* second column */ }

td:first-child + td + td { /* third column */ }

/* etc. */
RoToRa
May I ask: Why to down vote?
RoToRa
you also got the downvote. so here's my upvote for balance :-)
seanizer
@RoToRa apparently someone here thinks that MS defines standards
seanizer