DEV Community

Cover image for The new (and old) CSS units you've never heard about

The new (and old) CSS units you've never heard about

Massimo Artizzu on June 11, 2018

JavaScript is evolving fast, recently, but it's not like the other web development language companions are set to stone either. CSS is evolving to...
Collapse
 
matthijsewoud profile image
⚡️

Love this lad. Absolute unit.

Collapse
 
maxart2501 profile image
Massimo Artizzu

In awe at the wit of this comment.

You win this:
An absolute, striped unit

Collapse
 
pbkarlsson profile image
Philip Karlsson

I've used vh/vw a bit, and the new fr unit (which is useful when working with CSS Grid).

I've also come across the new (I assume that they're new) units vmin and vmax, which works pretty much the same as vh and vw. But represents the bigger/smaller side of the viewport instead of the height and width.

It'd be quite interesting to see a use case for all of these units.

Great post!

Collapse
 
maxart2501 profile image
Massimo Artizzu

vmin and vmax are actually as old as vw and vh, both on Level 3. Not many use cases, but there are some! They're especially useful for responsive layouts and font sizing.

Some trivia about them:

  • IE never supported vmax, and Edge started to support it only recently; a way to emulate it was using calc. E.g. if you wanted 15vmax, you could do calc(15vw + 15vh - 15vmin) - not really handy, though.
  • IE9 used vm instead of vmin, together with a completely wonky implementation of all these viewport units.

They're great units, unfortunately they lose a lot of value due to inconsistencies among mobile browsers (I'm looking at you, Safari).

Collapse
 
ryan profile image
Ryan

I used to like vh/vw. Until I discovered that Safari scaled them with zoom. So if you are zoomed at 85%, 100vw becomes 85% of the width of the page which is now equal to 72.2% of its original size.

I don't know who thought that made any sense and it seems to be the only browser that behaves that way :\

Collapse
 
pbkarlsson profile image
Philip Karlsson

Oh, I see.

It's a shame that it's not implemented consistently, which often seems to be the case with CSS.

Collapse
 
stephband profile image
stephband

All these typography units and still no way to control the baseline position...

Collapse
 
maxart2501 profile image
Massimo Artizzu • Edited

Ah, tell me about it 😄
It should be coming, though. Alas, not very soon.

Collapse
 
benjaminblack profile image
Benjamin Black • Edited

lh units can be simulated when using a scalar line height (like line-height: 1.2) with:

calc(1em * 1.2)

Likewise rlh with rem.

Collapse
 
leoratzlaff profile image
Leonardo Ratzlaff

How does the min-aspect-ratio works?

In the example, 16/10 or wider?

Collapse
 
maxart2501 profile image
Massimo Artizzu

Exactly.
16/10 is, after all, equal to 1.6 🙂

Collapse
 
leoratzlaff profile image
Leonardo Ratzlaff

I feel really dumb right now.
Thanks.

Collapse
 
jibinp profile image
Jibin Philipose

Awesome article, thank you for sharing the knowledge.