How to Use CSS3’s Viewport Units

CSS3 has a really cool (and completely underrated) feature that allows developers to define the size of an element relative to the viewport. The units are vh (viewport height) and vw (viewport width), and they can be applied to elements like width, height, font-size, margin…basically anything that you might normally define with a px or an em unit. Using viewport units to define sizes is an awesome way to make sure that your site is fully responsive and that the size of your HTML elements will adjust based on the size of the viewport.

Here’s how they work:

Join us in our newest publication:

1vh = 1% of the viewport’s height. So 28vh would be 28% of the viewport’s height, and so on.

1vw = 1% of the viewport’s width. So 16vw would be 16% of the viewport’s height, and so on.

To use the units to define the size of a particular element, they would appear in your CSS in the same way that px or em unts would appear.

  1. h1{
  2. font-size: 24vw;
  3. }
  5. div{
  6. width: 75vh;
  7. height: 75vw;
  8. }

Share and Enjoy !

0 0