DEV Community

Cover image for What CSS font-size units do you prefer to use?
Madza
Madza Subscriber

Posted on

21 6

What CSS font-size units do you prefer to use?

There are several different ways you can declare the size of a font in CSS. The units fall into one of two categories - absolute and relative.

Absolute units are fixed. Once they are declared, their size cannot be altered by changing the font size of some other element. Those include px, ­pt and pc units.

The size of relative units is determined by the size of a parent element. Their size can be altered by changing the sizing of that dependent element. Those include %, em, rem, vw, vh, vmin, vmax, etc.

What units do you normally prefer to use?

Top comments (43)

Collapse
 
madza profile image
Madza

Awesome to hear that 👍💯✨

Collapse
 
vtrpldn profile image
Vitor Paladini

call me old fashioned but 90% of my font-sizes are 16px

Collapse
 
ahferroin7 profile image
Austin S. Hemmelgarn

In visual display media, always rem, except on the root of the document (where I usually use pixels because they are de-facto consistent across all platforms. Using rems means that the site layout works correctly no matter how much zoom the user uses, because it all scales together.

For print media it gets a bit trickier. I would normally use points there because most printers will know how to handle that correctly without multiple unit conversions (unlike other absolute units which may or may not need to be converted twice). I might use ems if I need to offset size relative to the parent font size for some reason, but would probably not use relative units for anything else, instead relying on the (sane) assumption that people are printing at the 'correct' size (and obviously handle margins correctly so that I don’t need to handle separate styles for US and ISO paper sizes).

Collapse
 
contradicthelaw profile image
Minh Nguyen (he/him)

I use 100% for the body element, and relative units for everything else—mainly REM. Most of the clients I do work for require that their sites are accessible, which means I need to ensure the sites meet WCAG requirements.

There’s this really good article on 24 Accessibility on this subject by Kathleen McMahon which showcases side-by-side examples of what happens, including this one below, where a user sets their font size to "Very large" in Chrome:

Side by side visual differences of using pixel and relative units

This image shows the following:

  • The 1st example on the left prioritises the developer set font-size and the fixed line-height properties, thus does not resize according to user preference.
  • The 2nd example in the centre scales up the title and paragraph elements according to the user, but because line-height is set in pixels the paragraph text is visually squashed together.
  • The 3rd example on the right fully utilises relative units and visually looks the best.

REM is a bit more work to implement, but it goes a long way for the user.

Collapse
 
andrewbaisden profile image
Andrew Baisden

This right here.

Collapse
 
perpetual_education profile image
perpetual . education

We went through 6+ phases over the years... AND we're back to px for the font-sizes.. but we use em for letter-spacing and no unit for line-height... and well, it depends. Sometimes we think about opening up the discussion again... but - most em people can't make their argument. We also use clamp() a lot now - (but mostly with vmin and px...

Collapse
 
dailydevtips1 profile image
Chris Bongers

I recently switched mainly to REM, only using pixels for borders and media queries.

Collapse
 
geobrodas profile image
Georgey

yah rem a lot for media querie.

Collapse
 
andrewbaisden profile image
Andrew Baisden

Using rem for everything other than media queries.

Collapse
 
louislow profile image
Louis Low • Edited

Yes! I am the FIRST!

The em unit, gotta take care of the nice-looking designs compatible for both retina and high-density screens.

Collapse
 
weswedding profile image
Weston Wedding

On web I prefer rems, but will modify the 16px baseline depending on client needs (like font sizes that adjust based on display width).

Usually pixels if I'm making an Electron application that's on a fixed device screen size and doesn't need to support multiple devices.

 
andrewbaisden profile image
Andrew Baisden

Testing has always been a part of my development process its to be expected when you are building a website. Working with pixels is exactly the same it is not limited to rem. Some developers prefer rem whereas some prefer pixels as you can see from this discussion. Thats the great thing about being a developer the freedom of choice. 🙂

Thread Thread
 
93alan profile image
Alan Montgomery

I back you Andrew, I use rem for everything. Fonts, margins, padding, width, height. Scales better with respnsiveness

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

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

Okay