I have a table and I want to be able to assign specific styles within CSS to certain columns in the table: –
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
</tr>
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
<td>row 1, cell 3</td>
<td>row 1, cell 4</td>
<td>row 1, cell 5</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
<td>row 2, cell 3</td>
<td>row 2, cell 4</td>
<td>row 2, cell 5</td>
</tr>
</table>
I want to be able to give the first 2 th only the style of text-align:left; and the remaining th = text-align:center;.
The standard and direct solution :
The advantage of this notation is that it’s easy to change it to apply for example to the fourth and sixth
thinstead of the first two.Note that this supposes the availability of CSS3 in the browser.
If you want to be compatible with IE8, you might do it easily in javascript :
or with jQuery :