How to Use CSS3’s Viewport Units

Published September 5, 2016 by CSS Newbies.

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:

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.

h1{
font-size: 24vw;
}

div{
width: 75vh;
height: 75vw;
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>