Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center right-aligned numbers in table column using CSS?

Tags:

The picture below illustrates what I'm trying to accomplish:

enter image description here

UPD. As you see, I need the longest number to be centered in its cell, and all other numbers to be right aligned to the longest number's right border.

UPD 2. The numbers are dynamic (unknown initially).

like image 915
HiveHicks Avatar asked Dec 21 '12 14:12

HiveHicks


People also ask

How do you center values in a table CSS?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute.

How do you align a number in table column?

Changing the text alignment in a table column. Select the table chart, click in the corner of a table column, and then click in the column formatting bar until you reach the desired alignment. The control shows the alignment you are about to set.

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 I center align a column in CSS?

Centering Items within a CSS Grid Column To center the items within a column, we use align-items and justify-items instead of the align-content and justify-content in our previous example. Using the item properties, we can align our items just like we did with our columns.


2 Answers

There doesn’t seem to be any direct CSS method. But you could consider the approach suggested by @CMKanode in a comment. You would need to preprocess the numbers in the column and compute the largest of them (this requires locale-sensitive parsing since you are using a thousands separator), and then you would left-pad the numbers to the same number of characters, using U+2007 FIGURE SPACE as a space that has the same width as digits. And, of course, the column would be declared as centered.

So in the example, “5” would be padded to &#x2007;&nbsp;&#x2007;&#x2007;&#x2007;&nbsp;&#x2007;&#x2007;5 (assuming you use a normal space as thousands separator; U+2009 THIN SPACE might be better, but it has font issues.

The approach would mean that you need to use a font where digits have the same width (most fonts in computers do) and that contains U+2007.

If the thousands separator were a comma or a period, for example, you would need to use U+2008 PUNCTUATION SPACE in its stead.

In the end, I think this would be excessively complicated. It is probably better to make the column right-aligned but with a suitable left and right padding, selected as a good guess based on the width of the column header and the expected widths of the numbers.

like image 182
Jukka K. Korpela Avatar answered Sep 22 '22 23:09

Jukka K. Korpela


Below is not an ideal solution, since the size of the numbers are initially unknown, but it's a little closer without too much additional tags.

CSS:

.numSpan {     display: inline-block;     width: 100px;     text-align: right; } td { text-align: center; } 

HTML:

<td>     <span class="numSpan">5</span> </td> 
like image 27
CM Kanode Avatar answered Sep 23 '22 23:09

CM Kanode