Viewport units help you size elements based on the visible area of the browser window. This makes your design adjust smoothly when the window size changes.
Viewport units in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
width: 50vw; height: 100vh; font-size: 5vmin;
vw means 1% of the viewport's width.
vh means 1% of the viewport's height.
vmin is the smaller value between vw and vh.
vmax is the larger value between vw and vh.
div {
width: 100vw;
height: 100vh;
}p {
font-size: 3vmin;
}header {
padding: 5vh 10vw;
}This example shows a blue box that fills the whole browser window. The text inside scales with the viewport size using vmin. When you resize the browser, the box and text adjust smoothly.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Viewport Units Example</title> <style> body, html { margin: 0; height: 100%; } .full-screen { width: 100vw; height: 100vh; background-color: #4a90e2; display: flex; justify-content: center; align-items: center; color: white; font-size: 5vmin; text-align: center; padding: 2vmin; } </style> </head> <body> <div class="full-screen"> <p>This box fills the entire browser window.<br>Resize the window to see the text and box adjust.</p> </div> </body> </html>
Viewport units are great for responsive design but be careful on mobile browsers where the viewport height can change when the address bar shows or hides.
Use vmin and vmax to keep sizes balanced between width and height.
Viewport units let you size elements relative to the browser window size.
Use vw, vh, vmin, and vmax for flexible, responsive layouts.
They help your design look good on all screen sizes without fixed pixels.
Practice
1vw represent?Solution
Step 1: Understand viewport width unit
The unitvwstands for viewport width, so 1vw equals 1% of the browser window's width.Step 2: Compare with other units
Unlikevhwhich is viewport height,vwrelates only to width, not height or pixels.Final Answer:
1% of the viewport's width -> Option AQuick Check:
1vw= 1% viewport width [OK]
- Confusing vw with vh
- Thinking vw is pixels
- Mixing viewport units with parent size
Solution
Step 1: Identify viewport height unit
The unitvhmeans viewport height, so 50vh means 50% of the viewport height.Step 2: Check other units
vwis viewport width,vminis the smaller of width or height, andvmaxis the larger. Onlyvhsets height relative to viewport height directly.Final Answer:
height: 50vh; -> Option DQuick Check:
Use vh for viewport height in CSS [OK]
- Using vw for height
- Confusing vmin and vmax
- Forgetting semicolon in CSS
div {
width: 10vw;
height: 20vh;
}If the browser window is 1000px wide and 800px tall, what will be the div's width and height in pixels?
Solution
Step 1: Calculate width from vw
10vw means 10% of viewport width. 10% of 1000px = 100px.Step 2: Calculate height from vh
20vh means 20% of viewport height. 20% of 800px = 160px.Final Answer:
Width: 100px, Height: 160px -> Option CQuick Check:
vw and vh convert to % of viewport size [OK]
- Mixing width and height values
- Calculating percentages incorrectly
- Confusing vh with vw
.box {
width: 50vmin;
height: 50vmin;
}What is the likely problem?
Solution
Step 1: Understand
vminbehaviorvminuses the smaller of viewport width or height. If viewport changes size (like resizing window), the box size changes too.Step 2: Identify dynamic viewport effect
Because viewport size can change, the box size changes dynamically, which may look like it doesn't work as expected.Final Answer:
The viewport size might be changing, causing unexpected results -> Option BQuick Check:
vmindepends on viewport size changes [OK]
- Thinking vmin needs px unit
- Assuming viewport units are fixed
- Believing vmin is unsupported
Solution
Step 1: Understand vmin and vmax
vminis the smaller of viewport width or height,vmaxis the larger.Step 2: Choose unit for fitting inside viewport
To fit inside viewport without scrolling, usevminso the square fits the smaller dimension.Step 3: Confirm width and height match
Setting both width and height to 100vmin creates a square that fits inside viewport.Final Answer:
width: 100vmin; height: 100vmin; -> Option AQuick Check:
Use vmin for square fitting smaller viewport side [OK]
- Using vmax causes overflow
- Using 100% depends on parent size
- Confusing vw/vh with vmin/vmax
