Tailwind CSS Numeric font variants

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Did you know there is a CSS property to adjust how numeric values are displayed?
It's called the font-variant-number property, and it is used to distinguish how a particular font should render specific numeric values.
Tailwind CSS has this support built-in so let's look at the options we have and what they output.
Note: Not all fonts support this property, so be aware it might not work when you try this on custom fonts
You can use the normal-nums class to reset the value back to normal whenever you want the default behavior.
You want to show ordinal markers like in 1st, the st should show smaller on the top line.
We can use the ordinal class, which in CSS terms would add the following piece of CSS:
font-variant-numeric: ordinal;
Let's see how our HTML section would look in Tailwind:
<p class="ordinal">1st</p>
You can see the demo at the bottom linked CodePen.
Sometimes you might want to make a clear visual difference between a zero and the letter O. We can achieve this by using the slashed-zero class.
This will add the following CSS class.
font-variant-numeric: slashed-zero;
Making our HTML available like this:
<p class="slashed-zero">0</p>
Note: Small note here is that Google fonts do not yet support the glyph for slashed zero's, so only self-hosted/CDN fonts work
You can see the demo at the bottom linked CodePen.
Let's see how we can align numbers to match the heights of each other.
Adding the lining-nums class will ensure the numbers are aligned by their baseline.
It adds the following CSS:
font-variant-numeric: lining-nums;
And the Tailwind code will look like this:
<p class="lining-nums">1234567890</p>
Compared to oldstyle-nums which has an alternative alignment that will be dependent on the font style.
Which will add the following CSS:
font-variant-numeric: oldstyle-nums;
And it looks like this in Tailwind:
<p class="oldstyle-nums">1234567890</p>
You can see the demo at the bottom linked CodePen.
Another option we get is to display the font proportional or tabular.
Proportional means the numbers show in their width, where tabular makes every number the same width.
To make them proportional, we can add the proportional-nums class, which adds the following CSS:
font-variant-numeric: proportional-nums;
In Tailwind, that means adding the following class
<p class="proportional-nums">1212</p>
<p class="proportional-nums">9090</p>
And for the tabular nums we can add the tabular-nums class, which adds the following CSS:
font-variant-numeric: tabular-nums;
To make this work in Tailwind add the following HTML:
<p class="tabular-nums">1212</p>
<p class="tabular-nums">9090</p>
In the CodePen linked below, you can see the difference between the two renders as the tabular one is a bit more spaced out.
Sometimes we want to render fragments, so they showcase a bit nicer. This is a super cool feature, but not many fonts support this, so be aware of that when testing it out.
We can either choose to show diagonal-fractions which will show the fraction numbers divided with a slash.
The stacked-fractions option will showcase it as a horizontal divider between the two numbers.
Add the diagonal-fractions class to add the following CSS code:
font-variant-numeric: diagonal-fractions;
In Tailwind that would look like this:
<p class="diagonal-fractions">1/2 3/4 5/6</p>
Or, if you rather have the stacked fractions, you can add the stacked-fractions class which will add the following CSS:
font-variant-numeric: stacked-fractions;
And in Tailwind you can add it like so:
<p class="stacked-fractions">1/2 3/4 5/6</p>
I could not find a free font that supports it in my research. But for instance, Surveyor does.
Which makes it look like this:

Although not many fonts support this, it can be really helpful to display numeric values in a better way. Let's hope we get some more fonts supporting this excellent CSS utility.
You can see the working examples in this CodePen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter