Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to right align numbers in the center of a table cell?

Given a table with a column that contains numbers, I'd like to position them in the center.

But, I'd like to right-align the numbers as well!

table {
  border: 1px solid black;
}

th {
  width: 200px;
}

td {
  text-align: center;
}
<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>45</td>
    </tr>
    <tr>
      <td>2</td>
    </tr>
    <tr>
      <td>18923538273</td>
    </tr>
    <tr>
      <td>9823</td>
    </tr>
  </tbody>
</table>

Outputs:

enter image description here

Desired output:

enter image description here

Note: The table cell width should be constant (200px), regardless of the numbers. For example, if all numbers are 0, they all should be in the center of the table:

enter image description here

Also:

  • You are allowed to modify the content of the <td>s, but there should be one number per <tr>.
  • CSS only, please.
like image 703
Misha Moroshko Avatar asked Nov 07 '17 04:11

Misha Moroshko


People also ask

How to fix Excel numbers won’t center align?

As shown below, highlight the cells where the Excel numbers won’t left or centre align and: 1 go to Format Cells, 2 Click on Custom, 3 You will see the code that tells Excel to use the Accounting Format, 4 Delete the * in the format, 5 Click OK.

How do I align text in a cell in Excel?

How to align text in Excel using the Format Cells dialog Another way to re-align cells in Excel is using the Alignment tab of the Format Cells dialog box. To get to this dialog, select the cells you want to align, and then either: Press Ctrl + 1 and switch to the Alignment tab, or

How do I change the default number alignment in Excel?

By default, Microsoft Excel aligns numbers to the bottom-right of cells and text to the bottom-left. However, you can easily change the default alignment by using the ribbon, keyboard shortcuts, Format Cells dialog or by setting your own custom number format.

How do I align numbers to the center of the page?

It depends on the numbers. One option is to change the display property for td elements to block You can then set a max-width to bring td elements to the center of tr elements. Once that's done you set the text-align property to right for td elements to make the numbers start from the right hand side.


1 Answers

Updated based on an edit of the question and a few comments

In a comment you wrote "In the desired outcome, the cell width stays the same (200px) as numbers change".

In another comment you wrote "...my numbers are links and I want them to occupy the full cell width".

Given those requirements, the only CSS based solution I can find is, where one use CSS Table instead of <table> elements, an anchor a element displayed as table-row, making the full width clickable without adding an event handler, and for the centering, using pseudo elements to puch the numbers to the middle.

Stack snippet

.table {
  display: table;
  border: 1px solid black;
}
.tr {
  display: table-row;
  text-decoration: none;
  color: black;
}
.tr span {
  display: table-cell;
  width: 200px;
}
a.tr {
  text-align: right;
}
.tr::before, .tr::after {
  content: '';
  display: table-cell;
  width: 50%;
}
<div class="table">
  <div class="thead">
    <span class="tr">
      <span>Amount</span>
    </span>
  </div>
  <div class="tbody">
    <a href="#1" class="tr">
      <span>45</span>
    </a>
    <a href="#2" class="tr">
      <span>2</span>
    </a>
    <a href="#3" class="tr">
      <span>18923538273</span>
    </a>
    <a href="#4" class="tr">
      <span>9823</span>
    </a>
  </div>
</div>

<div class="table">
  <div class="thead">
    <span class="tr">
      <span>Amount</span>
    </span>
  </div>
  <div class="tbody">
    <a href="#1" class="tr">
      <span>0</span>
    </a>
    <a href="#2" class="tr">
      <span>0</span>
    </a>
    <a href="#3" class="tr">
      <span>0</span>
    </a>
    <a href="#4" class="tr">
      <span>0</span>
    </a>
  </div>
</div>

_____________________________________________________________________________

This is my first answer, which I will leave, as there might be someone that can make use of it as is.

One simple way to accomplish that is to simply nest a table for the values, center it using auto margin and right align its td's content.

This way you will get pretty much the exact same behavior as with your original markup, but get a better control of the values alignment.

Stack snippet

table {
  border: 1px solid black;
}

th {
  width: 200px;
}

table table {
  border: none;
  margin: 0 auto;
}

table table td {
  text-align: right;
}
<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <table>
          <tr>
            <td>45</td>
          </tr>
          <tr>
            <td>2</td>
          </tr>
          <tr>
            <td>18923538273</td>
          </tr>
          <tr>
            <td>9823</td>
          </tr>
        </table>
      </td>
    </tr>
  </tbody>
</table>

<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <table>
          <tr>
            <td>0</td>
          </tr>
          <tr>
            <td>0</td>
          </tr>
          <tr>
            <td>0</td>
          </tr>
          <tr>
            <td>0</td>
          </tr>
        </table>
      </td>
    </tr>
  </tbody>
</table>

You can of course use div's instead of a table, displayed as inline block or inline flex column.

Inline block

table {
  border: 1px solid black;
}

th {
  width: 200px;
}

td {
  text-align: center;
}

td > div {
  display: inline-block;
}

td > div > div {
  text-align: right;
}
<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div>
          <div>45</div>
          <div>2</div>
          <div>18923538273</div>
          <div>9823</div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div>
          <div>0</div>
          <div>0</div>
          <div>0</div>
          <div>0</div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

Inline flex column

table {
  border: 1px solid black;
}

th {
  width: 200px;
}

td {
  text-align: center;
}

td > div {
  display: inline-flex;
  flex-direction: column;
}

td > div > div {
  text-align: right;
}
<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div>
          <div>45</div>
          <div>2</div>
          <div>18923538273</div>
          <div>9823</div>
        </div>
      </td>
    </tr>
  </tbody>
</table>

<table>
  <thead>
    <tr>
      <th>Amount</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div>
          <div>0</div>
          <div>0</div>
          <div>0</div>
          <div>0</div>
        </div>
      </td>
    </tr>
  </tbody>
</table>
like image 112
Asons Avatar answered Nov 15 '22 06:11

Asons