
Viewport units measure the available browser viewport. They are useful for full-height sections, responsive layouts, and pages whose footer should reach the bottom when content is short.
1. What are vh and vw?
1vh is one hundredth of the viewport height and 1vw is one hundredth of its width. For an 800px by 600px viewport, 50vw is 400px and 50vh is 300px.
2. Using viewport units
Use viewport units anywhere that accepts a length, such as a minimum height or width. On mobile, browser toolbars can hide and reappear. svh uses the smaller viewport, while dvh responds to changes in the visible height. You can put 100vh first as a fallback.
.hero { min-height: 100vh; min-height: 100dvh; }3. Viewport units versus percentages
width: 50% usually measures half the containing block. width: 50vw measures half the viewport, even when the element is inside a narrow container. Prefer width: 100% for an ordinary full-width container, since 100vw can cause horizontal overflow in layouts with a vertical scrollbar.
4. Practical examples
4.1. Center one line of text
A line height matching the viewport height can center a single line of text vertically. Pair it with centered text alignment. For text that may wrap, use Grid or Flexbox instead.
.single-line { line-height: 100vh; text-align: center; }4.2. Center an element with transform
Move the top-left corner to the center of the viewport, then translate by half of the element’s own width and height.
.centered {
position: fixed;
top: 50vh;
left: 50vw;
transform: translate(-50%, -50%);
}4.3. Keep the footer at the bottom on short pages
Make the page a vertical flex container. The main section grows when space is available. If there is a lot of content, the footer follows it in the normal flow.
body {
margin: 0;
min-height: 100vh;
min-height: 100dvh;
display: flex;
flex-direction: column;
}
main { flex: 1; }Quick check
A container is 400px wide and the viewport is 1,000px wide. What is the difference between 50% and 50vw?
For a width property resolved against this container, 50% is 200px. The viewport length 50vw is 500px.