The picture below illustrates what I'm trying to accomplish:
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).
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.
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.
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'.
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.
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         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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With