DEV Community

Cover image for Scale font size the right way.
Kunga Tashi
Kunga Tashi

Posted on

Scale font size the right way.

Hey there, I recently found this simple way of organising font sizes for different html semantics like h1, h2, p etc and also maintain the visual hierarchy among them.

Here is the snippet of it.

root: {
    --p: 1rem;
    --font-scale: 1.2;
    --h5: calc(var(--p) * var(--font-scale));
    --h4: calc(var(--h5) * var(--font-scale));
    --h3: calc(var(--h4) * var(--font-scale));
    --h2: calc(var(--h3) * var(--font-scale));
    --h1: calc(var(--h2) * var(--font-scale));
}
Enter fullscreen mode Exit fullscreen mode

This has mainly two benefits:
first: you always maintain the hierarchy, the magnitude depends on what you set for --font-scale variable so tinker with it to get your perfect size.
second: when building responsive web apps with media queries, you only have to apply media query for --p, the rest will scale w.r.t to that value.

Configure this in your Tailwind config to use these variables.

const config = {
 theme: {
  extend: {
   fontSize: {
     p: "var(--p)",
     h5: "var(--h5)",
     h4: "var(--h4)",
     h3: "var(--h3)",
     h2: "var(--h2)",
     h1: "var(--h1)",
    },
  }
 }
}

Enter fullscreen mode Exit fullscreen mode

I hope this helps bring font size consistency in your next project.

Follow me [twitter].(https://twitter.com/kuntashTweets/)
I also run a small web agency actualizeui.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay