CSS vh and vw units for responsive web sites

CSS3 responsive design vh and vw
Difficulty

Let’s talk about the CSS VH and VW units.
Today’s websites must adapt better and better to new generations of browsers due to the fact that there is no longer a single type of device on which our site can be viewed. There are now an infinite series of devices, including televisions, smartphones, large and tiny PCs, tablets.
In this array of devices, flexible units of measure can help us, which adapt easily to the dimensions (in width and height) of the device that contains them.
I’m obviously talking about the units of measurement vh and vw that stand for:

vh = "viewport height"
vw = "viewport width"

With these units the browser goes to recover the context dimensions without any effort on our part.

With these units the browser goes to recover the context dimensions without any effort on our part.
Very convenient when you want a page on your site that is perfectly equal to the size of your window or when you want a perfect centering of something within the aforementioned page.
Each vh and vw unit is equal to 1% of the height and width of the browser window. However, there is an important differentiation to be made, compared to the classic percentage (%). The percentage refers to its container while these two units of measurement refer only and only to the window.

To achieve the same effect in javascript, you can use two properties of the window object:

window.innerHeight
window.innerWidth

It goes without saying that it is always convenient to use CSS styles when it comes to style needs.

That’s all, with the CSS VH and VW units.
Try it at home!

0
Be the first one to like this.
Please wait...

Leave a Reply

Thanks for choosing to leave a comment.
Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published.
Please do NOT use keywords in the name field. Let's have a personal and meaningful conversation.