Responsive Design with CSS Viewport Units

| Web Design
CSS viewport units

Responsive design makes it possible to adapt a website to any screen.

In responsive design, the size of a site’s elements change based on the browser size and CSS media queries.

Viewport units is an interesting CSS feature that allows you to automate some aspects of your responsive design.

Using viewport units, the sizes of specific elements can become smaller if the browser does the same.

Instead of pixel, em or percentage values, you can use these settings:

  • vw: the percentage of the browser’s width
  • vh: the percentage of the browser’s height
  • vmin: the minimum percentage of browser’s height or width, the smallest value of both
  • vmax: the maximum percentage of browser’s height or width, the smallest value of both

A font-size example

This will change the font-size based on the screen width:

h1 {
    font-size: 7vw;
}

7vw represent 7% of the screen pixel width.

A column width example

This will change the column width based on the screen width:

.block {
    width: 20vw;
}

20vw represent 20% of the screen pixel width.

A column height example

This will change the column height based on the screen height:

.block {
    height: 85vh;
}

85vh represent 85% of the screen pixel height.

Want to see a demo?

There's a demo available here

Resize the window to see it in action. This demo will automatically resize fonts, column widths and column heights using viewport units.

Browser support

CanIUse.com has a useful overview of which browsers support viewport units .


About the author

Valentín creates beautiful designs from amongst the tequila plants of Jalisco, Mexico. You can see Valentín's design work all over this site and you can often find him helping members in support.