Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write css fallbacks for vh vw

Tags:

Can anyone explain how fallbacks work in CSS? I am trying to set it up for vh and vw and clearly I am not getting it...

Here is my attempted solution, which does not work. The height property is taken every time.

CSS:

-webkit-height: 5.2vh; -moz-height: 5.2vh; -ms-height: 5.2vh; -o-height: 5.2vh; height: 41px; /* The Fallback */ 
like image 323
user3311351 Avatar asked Apr 28 '14 20:04

user3311351


People also ask

How do you calculate VH and VW?

You calculate the vw value by taking the difference in font-size ( 22 - 18 ), divide it by the difference in viewport widths ( 1000 - 600 ), then multiply it by 100vw - smaller-viewport-width ( 100vw - 600px ).

What is 2vw?

For example, if a view-port is 1600px wide, and you specify something as being 2vw, that will be the equivalent of 2% of the view-port width, or 32px.


2 Answers

Your Code (and why it doesn't work)

Looking at your original code, I have a couple of comments:

-webkit-height: 5.2vh; -moz-height: 5.2vh; -ms-height: 5.2vh; -o-height: 5.2vh; height: 41px;  /* The Fallback */ 

The prefixes, the -webkit- bit, only apply if there is a prefixed property by that name. Height doesn't have a prefixed property, so the browsers just ignore those declarations.

(Tip: You can check something like MDN to see what properties exist.)

Solution:

In this case, we can take advantage of the fact that, if browsers encounter a property or a value that they don't understand, they ignore it and move on. So, what you're looking for is something like:

height: 41px; height: 5.2vh; 

The browser sees height: 41px, as expected. It parses that, and knows what to do with it. Then, it sees height: 5.2vh. If the browser understands the vh unit, it will use that instead of 41px, just like color: blue; color: red; would end up being red. If it doesn't understand the vh unit, it will ignore it, and, because we defined the fallback first, the fact that the browser ignores the vh unit doesn't matter.

Make sense?

like image 108
Hawken MacKay Rives Avatar answered Oct 19 '22 05:10

Hawken MacKay Rives


Place your fall back above you other values. If the overriding values dont work on a browser, the first value is used.

 height: 41px;  /* The Fallback */ height: 5.2vh; 
like image 30
ramesh Avatar answered Oct 19 '22 03:10

ramesh