A long time ago, while I was still using Modular Scale, I created a library called Typi that helps me with responsive typography.
It uses Sass. You can create a map of all the different sizes that you want to use and it works — brilliantly.
$typi: ( base: ( null: (16px, 1.4), small: (18px), large: (20px) ), h1: ( null: (24px, 1.3), small: (2.369rem, 1.2), ) // Other font maps here);Once you have created a map, you can include the font-size you created by using the typi mixin.
h1 { @include typi('h1');}And typi will create the following CSS for you:
/* css output */h1 { font-size: 1.5rem; line-height: 1.3;}
@media all and (min-width: 600px) { h1 { font-size: 2.369rem; line-height: 1.2; }}I will be honest with you, I still love this approach, and I highly encourage you to check it out if you are still using Modular Scale. (There’s a ms function that calculates Modular Scale values for you. But fair warning, this hasn’t been maintained for many years.)
I’ve loved it so much that I ported the entire map system into a Tailwind config which works with Tailwind 3.
const scales = { h1: { base: ['24px', 1.3], bp8: `36px`, },}But as I migrated to Tailwind 4, I realized that this system doesn’t work anymore. It creates problems because it’s very difficult to define a map with the way Tailwind utilities work in this version.
So, with great pains, I ditched all of that and went for a very simple system instead.
My simple responsive typography system
You will laugh at this because there is no other system that is as simple as this.
I simply create a utility for each pattern I like to use, and then I include the font-sizes and the responsive values and the different breakpoints within. This is nothing more than CSS with a utility wrapper.
It’s so simple that I’m ashamed to blog about it, even though there’s nothing special about it.
Here’s what it looks like.
/* example of a h1 utility */@utility h1 { @apply marvin; @apply text-12; line-height: 1.2; @media screen and (width >= 800px) { @apply text-14; line-height: 1.15; }}I put all of this into a type.css file so they can be accessed and changed easily in one location.
@utility marvin { /*...*/ }@utility jetbrains { /*...*/ }@utility h1 { /*...*/ }@utility h2 { /*...*/ }@utility h3 { /*...*/ }@utility h4 { /*...*/ }As I’m writing this, I have this feeling that you might be disappointed after reading that I have such a simple system…
But isn’t that good news too? This means responsive typography doesn’t have to be complicated. It doesn’t have to be tedious, and it doesn’t have to be some awesome CSS that’s hard to handle. (Pointing to fluid typography here).
No need to make everything responsive
It is tempting to make everything responsive, but there is no need to do so. The smaller type doesn’t need to be responsive because it can just keep its size throughout.
To find the right font-sizes, I look at each component at different viewports and make my decisions from there. It can be tedious that way, but there’s no other way than this manual check if you want to have sizes that look perfect.
But my general rule of thumb is this:
- Anything below
16px? Not much change needed. - Anything above? Give it a deeper look.
Don’t change global font-size
When I first started out, I had this habit of changing the global font size at different viewports.
/* What I did in the past */html { font-size: 100%; @media screen and (width > 600px) { font-size: 112.5%; } @media screen and (width > 800px) { font-size: 125%; }}My rationale for doing this is:
- All of the other font sizes should be relative to body size.
- When we change the body size, the other font sizes will naturally change as well, which makes responsive typography easier.
But there are two major flaws in the argument.
- If the user zooms in or out, the font size will change accordingly because the viewport width has changed, so there are cases where I can’t reach the font size that I actually want to use — I found out about this when I was debugging, and for a long time it irritated the heck out of me.
- Bumping up the body size (and expecting everything to change accordingly) only works for marketing sites. When I started designing applications, I realized that you can’t do that because certain text needs to stay small.
So I treat the body text like any other element now — I give it a class (called prose) that I can resize easily.
.prose { @apply text-7; @media screen and (width >= 600px) { @apply text-8; }}Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson