views:

41

answers:

4

So, I have a table like below. Imagine there are multiple columns, I thought by formatting the <col/> tag I would be able to change the formatting of every <td> in that column. I want to give data in the first column a text-align:center, but it doesn't seem to work. Is there a way to get this to work other than adding a class to every <td>?

<table>
    <col class="column"/>
    <tr> 
        ... 
    </tr>
    <tr> 
        ... 
    </tr>
    <tr> 
        ... 
    </tr>
</table>
+1  A: 

Give the table a class, e.g. <table class="table1">. Then, in your CSS, you can reference the cells like so:

.table1 tr>td:first-child { text-align:center; }
Antony Highsky
Yep, wont do just first.Just use a CSS selector to get the first child td of each tr?
RPM1984
Good point. I edited the snippet.
Antony Highsky
This will not work in IE <= 6, as the [`>` combinator](http://msdn.microsoft.com/en-us/library/cc351024%28VS.85%29.aspx#combinators) and especially the [`:first-child` pseudo-class](http://msdn.microsoft.com/en-us/library/cc351024%28VS.85%29.aspx#pseudoclasses) are not supported before version 7 of this magnificent program.
Marcel Korpel
The "tr>" part is not strictly necessary, but yes IE6 and earlier don't support :first-child, so if compatibility w/ IE6- is important, I would just a put a class on every td. P.S. The site here has a matrix showing the different levels of support across browsers for CSS 2.1: http://www.quirksmode.org/css/contents.html
Antony Highsky
A: 

The class covers the tag it is tied to. Since you're closing the tag before doing anything, the class doesn't end up affecting anything. Like the other guy said, put it in the table, which spans all the tds.

stu
A: 

There's no way to get this to work in all common browsers, but you can use a modern CSS selector to achieve the effect in a standard (if not fully implemented) way.

tr:nth-child(1) { styles }

Where 1 is the first column.

More generally see http://www.w3.org/TR/css3-selectors/#nth-child-pseudo

The common solution is to add a class on each "td" which is usually a non-issue as you're typically generating HTML from other code. I've never seen the col class to which you reference before so did a quick search, and it appears that a) it should be within a colgroup tag and b) it's limited in the styles you can set.

Graphain
This only works in browsers supporting this CSS 3 feature. You'd rather use `td:first-child`, which is a CSS 2.1 feature *and* available in IE >= 7.
Marcel Korpel
If first-child was what he wanted (he said a specific column not the first column). I don't think I'd use any solution other than a class on each td personally unless I had control of the browsers or javascript enabled state.
Graphain
Ah I see he mentioned only first column - I swear it asked for a given column before
Graphain
Indeed, and nope: http://stackoverflow.com/posts/3002304/revisions
Marcel Korpel
A: 

The non-css way is to simply add align="center" to your td of the respective column. This is one way where Dreamweaver saves development time: select the entire column, type in center for align and you're done.

kelly