5

Wondering what the best way to make this more efficient, perhaps with jQuery. I am ok with solutions not compliant with ie7, even lack of support for ie8 might be ok if necessary.

<style type="text/css">
.cal {text-align:center}
.ral {text-align:right}
</style>
<table>
 <th>
  <td class="cal">center</td>
  <td>left</td>
  <td class="cal">center</td>
  <td>left</td>
  <td class="ral">right</td>
 </th>
 <tr>
  <td class="cal">center</td>
  <td>left</td>
  <td class="cal">center</td>
  <td>left</td>
  <td class="ral">right</td>
 </tr>
 <tr>
  <td class="cal">center</td>
  <td>left</td>
  <td class="cal">center</td>
  <td>left</td>
  <td class="ral">right</td>
 </tr>
</table>
chrisrth
  • 1,182
  • 3
  • 15
  • 36

4 Answers4

5

You could add a class to the fifth td in each row using jQuery, and style the class to align text to the right.

Alternatively (and the way I do it) is using the nth child selector.

tr td:nth-child(odd) {
    text-align: center;
}

tr td:nth-child(5) {
    text-align: right;
}

it's not compatible with IE 8.0, but it is a lot simpler than a JavaScript based solution.

mayhewr
  • 4,003
  • 1
  • 18
  • 15
djlumley
  • 2,955
  • 2
  • 24
  • 30
  • This will work, based on the tip I found a great solution that has broader support for ie by applying the style with jQuery (only when using ie). There is even a jsfiddle demo to accompany the solution [link](http://stackoverflow.com/a/3951427/1101267) – chrisrth Apr 18 '12 at 08:19
3

Would this work?

table td { text-align: center; }
table td:last-child { text-align: right; }

This isn't supported by IE8- unfortunately. Another alternative is using JavaScript (e.g. jQuery, since you've tagged this post with it):

$(function(){ $('table tr').each(function(){ $('td:last', this).css({textAlign: 'right'}); } )}

This question seems to be well-accepted, although it's as ugly a hack as you can get: Styling the last td in a table with css

Finally, you could cheat a little and have your last column's cell as a <th> element and then style it easily with:

table th { text-align: right; }

It wouldn't necessarily be semantically correct but it's another quick and easy option. I'm assuming the existing <th> elements in your original post are typos.

Community
  • 1
  • 1
Phil.Wheeler
  • 16,748
  • 10
  • 99
  • 155
2
$('td:first-child').css('text-align', 'left');
$('td:nth-child(2)').css('text-align', 'center');
$('td:last-child').css('text-align', 'right');

See http://api.jquery.com/category/selectors/child-filter-selectors/ for other filters

Dmitri K
  • 21
  • 1
-1
<style>
td:first-child, td:first-child + td + td {
  text-align: center;
}
td:first-child + td + td + td + td {
  text-align: right;
}
</style>
<table>
<col align=center>
<col align=left>
<col align=center>
<col align=left>
<col align=right>

The style sheet works on reasonably new browsers. I have omitted the rules that set alignment to left, as that’s the default for td, but you may need to add such rules if other stylesheets may affect the alignment.

The col tags act as a backup: they use a method that should not work, by the specifications, but actually works on old versions of IE (as well as on newer versions when in Quirks Mode).

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390