DEV Community

Cover image for Units in CSS (em, rem, pt, px, vw, vh, vmin, vmax, ex, ch, ...)

Units in CSS (em, rem, pt, px, vw, vh, vmin, vmax, ex, ch, ...)

Matthias 🤖 on July 30, 2019

CSS knows several measurement units. The best known unit is pixels, but there are other units that are not that popular but very handy in some uses...
Collapse
 
maxart2501 profile image
Massimo Artizzu

Good one, Matthias!

These are all length units. Here's your friendly reminder that in CSS you could also measure:

  • angles (deg, rad, grad, turn);
  • time (s, ms);
  • aspect ratio (expressed as a fraction, like 16/9);
  • frequency (Hz, kHz) (but nobody has ever implemented it haha)
Collapse
 
asfo profile image
Asfo

The frequency is on "CR" (Candidate Recommendation) yet. So, not a single browser implemented this yet.

Collapse
 
maxart2501 profile image
Massimo Artizzu

And we won't see them implement it either for the foreseeable future either. Because, after reaching the CR stage in 2012, the CSS Speech Module has been largely ignored by vendors and has actually been retired last year.
So much for standards... 🙃

Thread Thread
 
asfo profile image
Asfo

Yep haha...this one is really weird, and I think pretty useless. But well...#JustW3Things

Collapse
 
matthias profile image
Matthias 🤖

Thank you! 🙏

I could not even imagine a use case for frequency 🤔 Do you have an example?

Collapse
 
maxart2501 profile image
Massimo Artizzu

No... and nobody will have one either for a while! 😂

But it was meant for some properties (like pitch) where a synthesized voice was involved, for screen readers and such.

And it's actually the second time a module like this gets canned. Oh well, looks like the visually impaired are fine without it 😐

Collapse
 
sebbdk profile image
Sebastian Vargr

Nice article, easy to skim through and to the point, thank you! :)

I have to admit, i still use px everywhere in practice.
Occasionally i use vh and vw for layout and such.

Someday i will get an opportunity to make or implement a system that uses proper font scaling... Or so i keep telling my self.

Collapse
 
deiga profile image
Timo Sand

A quick way to jump on the relative size bandwagon: Replace all uses of px with rem and adjust accordingly :)

Collapse
 
sebbdk profile image
Sebastian Vargr

I need to align it with teams of mixed experience already making lots of other more major mistakes, telling people to use px values is a lot easier than adding to the pile of things to learn :/

I agree tho, it’s a good way to start a migration. :)

Collapse
 
sebbdk profile image
Sebastian Vargr

Reporting back, did this.

Thank you.

Thread Thread
 
deiga profile image
Timo Sand

Nice! 🎉

Collapse
 
hozefaj profile image
Hozefa • Edited

Great post. Very well written and detailed.

On top of this, I recommend listing to syntax.fm/show/107/hasty-treat-css... episode. It covers all of the above units well. Will act to strengthen what you get from this article.

Collapse
 
matthias profile image
Matthias 🤖

Thank you! I always try hard to write good articles. I hope they are easy to understand as English is not my mother language.

I didn't know that there is a CSS units episode of syntax.fm. I started listening their podcast a few weeks ago.
@wesbos and @stolinski are incredible good hosts and it's fun to listen.

Collapse
 
stolinski profile image
Scott Tolinski

Thanks for the shout out!

Collapse
 
gnsp profile image
Ganesh Prasad

There is another interesting unit 'q'. 1q = 0.945px (approx.). I came across this unit while css code-golfing on cssbattle.dev .

Collapse
 
mjcoder profile image
Mohammad Javed

Awesome article - thank you.

Collapse
 
matthias profile image
Matthias 🤖

Thank you!

Collapse
 
chanclidev profile image
Chanclidev

Nice article!

Brief and well explained

Collapse
 
matthias profile image
Matthias 🤖

Thank you so much for your feedback!

Collapse
 
mayaqj profile image
mayaqj

Great post!

Collapse
 
matthias profile image
Matthias 🤖

Thank you!

Collapse
 
durai profile image
DURAI

hlo,i am front end developer ,woking in html ,css, js, bs .
what should add on my profile for future developement skills.

Collapse
 
weakish profile image
Jang Rush

Hello, I'd like to translate this awesome tutorial to Chinese. The translated text will be published at nextfe.com. Can you give me the permission? :-)

Collapse
 
mazonthemoon profile image
Mary Ronan

Nice one 👏👏