CSS · Lesson 4 of 12 · 3 min
Units, Values & Responsive Basics
Your choices here determine if a layout is scalable and accessible, a core topic in any frontend interview.
After this, you will be able to explain the trade-offs between CSS units and justify your strategy for building responsive layouts. This isn't about memorizing definitions; it's about demonstrating a system for making design decisions.
Absolute vs. Relative: The Fundamental Trade-off
Every sizing decision in CSS starts with a choice between an absolute or a relative unit.
Absolute units, primarily the pixel (px), are fixed. A 1px border is a 1px border. Use them for things that must not scale with user settings or viewport changes, like fine borders, box-shadow offsets, or fixed-size icons.
Relative units (rem, em, %, vw, vh) calculate their size based on another value. They are your primary tools for creating fluid, responsive, and accessible designs. The rule of thumb is to default to relative units unless you have a specific reason for a fixed size.
How to Explain em vs. rem
"Explain the difference between
emandrem." This is a classic CSS screening question. Your answer shows if you understand CSS inheritance and how to build a scalable system.
Start with the definitions. 1em is equal to the computed font-size of the element it's used on. If you set font-size itself with em, it's relative to the parent's font-size. 1rem is always equal to the font-size of the root element (<html>).
The trap: The cascading nature of em is its biggest weakness. If you have nested components that all use em for font sizes or padding, the values compound. A font-size: 0.8em inside another font-size: 0.8em results in a tiny, unreadable font. This makes styling unpredictable and hard to debug.
rem solves this by providing a single, stable reference point. By setting a base font-size on the <html> element, every rem value in your application becomes predictable. This is essential for maintaining a consistent typographic scale and spacing system.
For most applications, the modern best practice is to use rem for all scalable values: font-size, padding, margin, gap, and component dimensions. Use em only in specific cases where you want a value to scale relative to the font size of that particular component, like an icon whose padding should be proportional to its own size.
When to Use Viewport Units (and When Not To)
Viewport units (vw for width, vh for height) size elements relative to the browser window's dimensions. 1vw is 1% of the viewport width.
Their most common use is for creating sections that fill the screen, like a hero banner with height: 100vh. They are also effective for "fluid typography," where a headline's font-size scales directly with the screen width (e.g., font-size: 5vw).
The trap: On mobile browsers, 100vh doesn't always mean "100% of the visible height." The address bar and other UI elements can appear and disappear, changing the actual viewport height. This can cause jarring layout shifts. Modern CSS has introduced new units like dvh (dynamic viewport height) to solve this, but browser support is still evolving. Be aware of this limitation and test on real devices.
Building a Practical Responsive Foundation
A robust layout rarely relies on a single unit. The most effective approach is to combine them, each playing to its strength.
A common and effective pattern is to use percentages for fluid containers, a px-based max-width to maintain readability on large screens, and rem for all internal spacing and typography. This gives you a fluid layout that scales predictably and is constrained where it needs to be.
This example demonstrates a layout that is fluid, constrained, and scalable.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Foundation</title>
<style>
html {
/* Base font size for rem calculations. 16px is browser default. */
font-size: 16px;
}
body {
font-family: system-ui, sans-serif;
margin: 0;
background-color: #f9f9f9;
color: #333;
}
.main-content {
/* Fluid width: takes up 90% of its parent (body) */
width: 90%;
/* Fixed constraint: will not grow wider than 1100px */
max-width: 1100px;
/* Scalable margin and padding based on root font size */
margin: 2rem auto;
padding: 2rem;
background-color: white;
border-radius: 8px;
}
h1 {
/* Scalable font size */
font-size: 2.5rem;
margin-bottom: 1.5rem;
}
p {
font-size: 1rem;
line-height: 1.6;
margin-bottom: 1rem;
}
</style>
</head>
<body>
<main class="main-content">
<h1>A Practical Layout</h1>
<p>
This container is fluid because its width is set to 90%.
On very large screens, it stops growing at 1100px thanks to max-width,
preventing text lines from becoming too long and hard to read.
</p>
<p>
All font sizes, margins, and padding use `rem` units. If you change
the `font-size` on the `<html>` element, the entire layout and type
scale proportionally, which is great for accessibility and maintenance.
</p>
</main>
</body>
</html>
Try it
Find a small, static component you've built (like a card or a button). Refactor it to use this mixed-unit strategy. Set its container width in % with a max-width in px. Convert all px values for font-size, padding, and margin to rem. Then, go into your browser's dev tools and change the font-size on the <html> element to see how everything scales together.
Next: We'll control layout with more power using Flexbox Fundamentals.
Lock it in — apply this lesson under interview conditions.