Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align last two columns of a table with CSS

I'd like to right align the text of last two columns of a table.

<table>
  <tr>
    <th>H 1</th>
    <th>H 2</th>
    <th>H 3</th>
    <th>H 4</th>
  </tr>
  <tr>
    <td rowspan='3'>1</td>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
  <tr>
    <td>2</td>
    <td>3</td>
    <td>4</td>
  </tr>
</table>

There is not the same number of columns in each row and I'm not sure how to use css:nth-child to select the last two td items in each row.

like image 647
Martlark Avatar asked Nov 25 '13 01:11

Martlark


People also ask

How do you right align a column in CSS?

For aligning columns to the left, the align-content property will set to 'flex-start'. For aligning columns to the right, the align-content property will set to 'flex-end'. For aligning columns to the extreme ends, the align-content property will set to 'space-between'.

How do you right align a table in CSS?

The text-align property sets the horizontal alignment (like left, right, or center) of the content in <th> or <td>. By default, the content of <th> elements are center-aligned and the content of <td> elements are left-aligned.

How do I align columns to the right?

HTML | <col> align Attribute left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justify: It stretches the text of paragraph to set the width of all lines equal.

How do I move a column to the right in a table in HTML?

Use width:100% on the table and the middle column. You could also set width:100% on the last column's style and td align="right" on the last column. Then you can insert more columns in the middle while the spacing still works.


1 Answers

http://jsfiddle.net/BB9ty/

th:last-child,
td:last-child,
th:nth-last-child(2),
td:nth-last-child(2) {
    text-align: right;
}
like image 51
Mark Simpson Avatar answered Sep 25 '22 21:09

Mark Simpson