As a frontend developer, you've probably had a conversation like this at least once:
💬 Designer: "Why are these table numbers so uneven? The widths are all different."
Developer: "That's because each number has a different width in this font..."
Designer: "Do we have to use a monospace font then?"
After reading this article, you'll be able to answer:
"No, one line of font-variant-numeric
will solve it."
The font-variant-numeric Property
font-variant-numeric
is a CSS property that controls how numbers are displayed, allowing you to apply specific number-related styles. It's primarily used in typography to finely adjust the appearance, spacing, and alignment of numbers.
Width Control (numeric-spacing-values)
-
tabular-nums
: Makes the width of proportional font numbers uniform.cssfont-variant-numeric: tabular-nums;
before01234567891231231231after01234567891231231231 -
proportional-nums
: Conversely, makes monospace font numbers proportionally spaced.cssfont-variant-numeric: proportional-nums;
before01234567891231231231after01234567891231231231
Style Control (numeric-figure-values)
-
lining-nums
: Aligns all numbers to the baseline with uniform height.cssfont-variant-numeric: lining-nums;
before0123456789after0123456789 -
oldstyle-nums
: Displays certain numbers (3, 4, 5, 7, 9) lowered like lowercase letters.cssfont-variant-numeric: oldstyle-nums;
before0123456789after0123456789
Fraction Control (numeric-fraction-values)
-
diagonal-fractions
: Automatically converts standard fraction notation (1/2, 3/4) to diagonal fractions (½, ¾).cssfont-variant-numeric: diagonal-fractions;
beforeFlour 1/2 cupSugar 3/4 cupafterFlour 1/2 cupSugar 3/4 cup -
stacked-fractions
: Displays fractions in a stacked format.cssfont-variant-numeric: stacked-fractions;
before1/2Sugar 3/4after1/2Sugar 3/4
Other Useful Features
-
slashed-zero
: Adds a slash to the number 0 to clearly distinguish it from the letter O.cssfont-variant-numeric: slashed-zero;
beforeID: U00123O0
afterID: U00123O0
-
ordinal
: Displays text like 1st, 2nd, 3rd in ordinal format.cssfont-variant-numeric: ordinal;
before1st, 2nd, 3rd
after1st, 2nd, 3rd
Important Considerations
Not all fonts support the CSS properties listed above. The font you use must support the corresponding OpenType features for the effects to appear.
Since the font-variant-numeric
property utilizes advanced features of OpenType fonts, web fonts or system fonts must have these features built-in to work properly.
For example, older fonts or basic web fonts may not support features like diagonal-fractions
or oldstyle-nums
.
Conclusion
Now, when a designer asks, "Why is the number alignment in this table so messy?", don't panic. One line of font-variant-numeric
is enough.
It's just one small line of CSS, but the difference in user-perceived quality is significant. Next time you're working on a UI with numbers, remember font-variant-numeric
. It will definitely help you create a better user experience.
References