Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

css

css-tables

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.

like image 434
Koala7 Avatar asked Aug 16 '12 09:08

Koala7


People also ask

How do I left align and right align text?

Align the text left or right Select the text that you want to align. On the Home tab, in the Paragraph group, click Align Left or Align Right .


2 Answers

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/

like image 163
cloudfeet Avatar answered Sep 23 '22 18:09

cloudfeet


You need to specify the following CSS:

.table td, .table th {     text-align:left; }  #right, .table td + td, .table th + th {     text-align:right; } 

Example here: http://jsfiddle.net/mWeYM/

You can read more about adjacent siblings CSS selector here: http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors

like image 33
keaukraine Avatar answered Sep 19 '22 18:09

keaukraine