Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table cells right align inside row

Tags:

html

css

I'm trying to figure out how to move a cell to the left on HTML table. I want to use less cells in the last row and it's on the right by default.

I have this table for example:

<table>
  <tr>
    <th>one</th>
    <th>two</th>
    <th>three</th>
  </tr>
  <tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
  </tr>
  <tr>
    <td>this</td>
    <td>right</td>
  </tr>
</table>

I'm trying to move the "this" and "right" cells to the opposite side. I'm looking for a way with less as possible css.. preferred HTML only.

Update: I wasn't looking for answers about text/value align. colspan solves it somehow but still, won't call it a perfect solution.

like image 910
Hakerovsky Avatar asked Sep 12 '25 02:09

Hakerovsky


2 Answers

The td should span two columns by using the attribute colspan="2", and align the text to the right:

.alignRight {
  text-align: right;
}
<table>
  <tr>
    <th>one</th>
    <th>two</th>
    <th>three</th>
  </tr>
  <tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
  </tr>
  <tr>
    <td>this</td>
    <td colspan="2" class="alignRight">right</td>
  </tr>
</table>

Another, html only, option is to use colspan="2" on the "this" cell:

<table>
  <tr>
    <th>one</th>
    <th>two</th>
    <th>three</th>
  </tr>
  <tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
  </tr>
  <tr>
    <td colspan="2">this</td>
    <td>right</td>
  </tr>
</table>
like image 116
Ori Drori Avatar answered Sep 13 '25 16:09

Ori Drori


you can only add align="right" attribute

<table>
  <tr>
    <th>one</th>
    <th>two</th>
    <th>three</th>
  </tr>
  <tr>
    <td>one</td>
    <td>two</td>
    <td>three</td>
  </tr>
  <tr>
    <td>this</td>
    <td colspan="2" align="right">right</td>
  </tr>
</table>
like image 44
ankita patel Avatar answered Sep 13 '25 14:09

ankita patel