Responsive Design

Responsive Viewport Units

Move over em — CSS3 introduces vw, vh, and vmin: units that scale directly with the browser window.

0
Viewport Units
0
Sections
0
Code Examples
01 / Introduction

A CSS unit born for responsive design

With all the growing enthusiasm around responsive web design, it was about time CSS got a unit to match. CSS3 Values and Units introduces a new class of viewport-relative lengths that are finally ready for prime time.

Motorola were early adopters, implementing support before these units found their way into Chrome Canary. They are also supported in IE9, though the original vm unit was later renamed to vmin.

02 / The units

Introducing vh, vw, and vmin

vh Viewport Height

1% of the viewport's current height. Set 100vh for full-height elements.

vw Viewport Width

1% of the viewport's current width. Works identically to vh on the horizontal axis.

vmin Viewport Minimum

Whichever of vh or vw is the smaller value at any given moment.

The values can be confusing at first: 1vh represents 1% of the viewport height, not the full height. To make an element fill the entire viewport you need 100vh.

On a widescreen monitor at full-screen, the width is greater than the height — so vmin resolves to the same computed value as vh. On a tall narrow phone, the opposite holds.

03 / Dynamic behaviour

Units that move with the window

Viewport units are dynamic. As you resize the browser window, the computed pixel value updates in real time. An element set to 10vw on a 1000px-wide viewport is 100px wide — shrink the window to 400px and the element becomes 40px wide automatically.

Live demo — resize 10vw × 20vh
10vw × 20vh
Width Height —
04 / Practical use

Solving multi-column scrolling

One of the persistent pain points with CSS multi-column layout is vertical overflow: if a column block is taller than the viewport, the reader must scroll up and down repeatedly to follow each column. Viewport units offer a neat solution — constrain the height of the multi-column container to something less than 100vh, and let the browser scroll horizontally through additional columns instead.

There is still the matter of horizontal scrolling, which can feel awkward on a desktop browser. But for targeted contexts — tablet-sized devices held in landscape, for instance — it is a reasonable trade-off.

Note on macOS

Because macOS Lion hides scrollbars by default, it can be difficult to see that the multi-column container is horizontally scrollable. Consider adding a visible scroll indicator for cross-platform clarity.

05 / The code

Setting size with vw and vh

Constraining a multi-column section to 80% of the viewport in both dimensions requires just two declarations. The column width is expressed in rem so the browser can adjust column count as needed. Setting overflow: scroll keeps extra columns hidden until the reader scrolls to them.

css
article > section {
  column-width: 22rem;
  column-gap:   2.6rem;

  height:   80vh;
  width:    80vw;

  overflow: scroll;
}

This sets the container to 80% of the viewport in both dimensions. column-width: 22rem gives the browser a target column width, but allows it to adjust as needed. overflow: scroll hides the extra columns and makes them scrollable — they won't overflow and overlap any adjacent elements.

06 / Breathing room

Viewport-relative margins on body

Margins on the body element can scale with the viewport too. The snippet below gives 2% of the viewport height as top/bottom breathing room, and 10% of the viewport width as left/right margins:

css
body {
  margin: 2vh 10vw;
}

The result is a layout that breathes proportionally at any window size — wider windows get wider gutters, taller windows get more vertical space. No media queries involved.

07 / Takeaway

A truly responsive foundation

With these units you have a genuinely responsive design — at least for simpler layouts — without touching a single media query. Imagine combining them with media queries when you do need discrete breakpoints, or layering in Flexbox for alignment. The result is a layout system that adapts fluidly at every scale.

Browser support in 2012

At time of writing, viewport units were available in Motorola's browser, Chrome Canary, and IE9 (with vm replaced by vmin). Today they enjoy full cross-browser support including svh, lvh, and dvh — dynamic variants that account for collapsible browser chrome on mobile.