avatarKen Nersisyan

Summary

The author explains why setting the HTML font size to 62.5% simplifies converting between pixels and rems, while also addressing the importance of respecting users' accessibility settings.

Abstract

The article discusses a CSS technique where setting the font size of the HTML element to 62.5% facilitates easier conversion between pixels and rems. This is because it establishes a base font size of 10px, making calculations straightforward. The author emphasizes the convenience of this method, especially when using units like rems in CSS. However, the author also acknowledges the potential accessibility issues that arise from altering the default font size, which could override user preferences. To mitigate this, the author suggests resetting the font size in the body element to 1.6rem, effectively maintaining the browser's default settings and respecting user accessibility choices. The article provides a comprehensive explanation, including the mathematical rationale behind the 62.5% value and the ethical considerations for web development practices.

Opinions

  • The author believes that using 62.5% for the HTML font size is a valuable development trick for simplifying calculations when working with rems.
  • They recognize the importance of not disrupting users' accessibility preferences, advocating for a solution that maintains the default font size through a reset in the body element.
  • The author encourages the use of this technique but with the caveat that developers should be mindful of accessibility, suggesting that this approach should be part of a CSS reset file.
  • There is an emphasis on the ease of mental calculation when using this method, which is particularly useful when working with design specifications in pixels that need to be translated into rems.
  • The article reflects on the author's personal experience and learning journey, highlighting the importance of continuous learning and the sharing of knowledge within the developer community.
  • The author promotes the work of Aleksandr Hovhannisyan, citing it as a source of inspiration and further reading on the topic of accessibility and font size preferences.
  • The author invites readers to connect on social media platforms like Twitter and GitHub, indicating a commitment to community engagement and ongoing dialogue about web development practices.

Why I Change the Font Size to 62.5% in Every Project

Small development trick that will help you to simplify your calculations.

Percentage sign GIF from GIPHY

Context

When I first started coding in CSS, I didn’t really pay attention to other sizing options such as rem, vw, vh, etc. I only stuck to the traditional pixel values and sometimes would use percentages to set like the width of a div to 100%.

But then, when I started to learn and use CSS more professionally, I started to look into rems and ems. A good source to learn more in-depth about them would be this awesome video by Kevin Powell.

The Problem

However, when you start using rems, it becomes hard to calculate every time how many pixels it would be or vice versa. Like you’d get a design that uses pixels, but you want to use rems.

If you’re using something like Tailwind CSS (learn more), you don’t really need to worry about calculations that much. You can just do it once, put it in the config file and forget about calculating them every time you need to use it.

On the other hand, if you’re using just CSS, SCSS, styled components, or basically anything that doesn’t do the calculations for you. You may need to use specific sizes, and every time you’d have to convert it from rems to px or vice versa.

Some people say you get used to it, but why worry about an extra thing when you can easily fix this by writing a couple of lines of CSS?

The Solution

So the solution to avoiding calculations is by adding the following lines to your CSS reset file.

html {
  font-size: 62.5%;
}

Why 62.5% you ask? Because 100% / 16px *10=62.5%

Ok now let’s break this down, shall we?

We divide 100 by 16 because 16px is the browser’s default font size, so that gives us 6.25, then we multiply that by 10, so when we try to convert from rem to px we can easily calculate it in our minds.

For example, with this setting the calculation would be something like this:

  • 1.6rem = 16px
  • 2rem = 20px
  • 0.4rem = 4px
  • 10rem = 100px

You get the point, take whatever number you have in your mind, let’s say 24px you divide it by 10 and you get your desired result in rem, which in this case will be 2.4rem.

But wait a minute, we’re not done yet. When changing the font-size in html we also overwrite users’ preferences, which is very bad, because it can mess with users’ accessibility settings.

A user may have their font-size set to 22px in their browser, and we as a developer have to respect their choice. But don’t you worry there is a solution for this as well.

So when setting font-size: 62.5% in html, you also should reset the font-size in body, like so:

html {
   font-size: 62.5%;
}
body {
   font-size: 1.6rem;
   // ...
   // the rest of your resets
   //...
}

This solves the issue that technically was created by us. By resetting font-size: 1.6rem, we’re saying that everything should go back to the browser's default settings.

We don’t scale down font-size in html to change the root font size, we do it because that number helps us to convert rem to px and another way around, more easily.

Yet if we don’t change it back to 1.6rem in body, it means that we just scaled down the root size and pretty much messed up users’ preferences without even them knowing about it.

If you’re more concerned about accessibility stuff you can check out Aleksandr Hovhannisyan’s article, he goes more in-depth about accessibility.

In Conclusion

You might’ve seen this trick somewhere or you have been using it for a while yourself, but it never crossed your mind that this can mess up someone’s preferences.

Honestly, I neither, until one day a friend of mine opened one of my projects and realized that the font size that he had put in the settings doesn’t work anymore.

And only then do I have to research and find a solution that was provided by Aleksandr, and that’s where the inspiration for this article came from. I just want more people to know how to use this cool trick without any pitfalls.

If this was helpful. consider sharing it, and don’t forget to follow me, so you don’t miss out on my future articles.

Further Reading

Let’s Connect

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Check out our Community Discord and join our Talent Collective.

CSS
Font Size
Web Development
Tips And Tricks
Percentage
Recommended from ReadMedium