← Back to CSS Course | Chapter 7: Layout Utilities | Lesson 5 of 5

CSS Relative Units

CSS Units already introduced em, rem, %, and vw/vh individually — this topic covers the two relative units it leaves out, vmin and vmax, and gives direct, practical guidance on choosing between the whole relative-unit family.
Syntax
css
selector {
  font-size: numberem;  /* also rem, %, vw, vh, ch */
}

vmin: The Smaller Viewport Dimension

vmin represents 1% of whichever viewport dimension is smaller — width or height, whichever is less at any given moment. This makes it useful for sizing elements that need to fit comfortably regardless of whether the device is in portrait or landscape orientation.

Note:
  • Accepted values:
  • em — relative to the parent's font size
  • rem — relative to the root font size
  • % — relative to the parent
  • vw | vh — 1% of the viewport width or height
  • ch — width of the 0 character
  • vmin | vmax — the smaller or larger viewport side

Example: vmin: The Smaller Viewport Dimension

css
<style>
.box {
  width: 50vmin;
  height: 50vmin;
  background: steelblue;
  color: white;
}
</style>
<div class="box">Fits comfortably in portrait or landscape</div>

vmax: The Larger Viewport Dimension

vmax is the mirror image of vmin — 1% of whichever viewport dimension is larger. It is used far less often than vmin in practice, but is useful when you specifically want an element to scale with the dominant, longer dimension of the screen.

Note:
  • Accepted values:
  • 1vmax — 1% of the larger viewport dimension
  • 50vmax — half of the larger dimension
  • 100vmax — full larger dimension
  • 1vmin — 1% of the smaller dimension (for comparison)

Example: vmax: The Larger Viewport Dimension

css
<style>
.box {
  width: 50vmax;
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div class="box">Scales with the longer viewport dimension</div>

When to Choose rem Over em

em is relative to the current element's (or its parent's) font-size, which means nesting elements with em-based sizing compounds unpredictably — a common source of bugs.

rem is always relative to the root html element's font-size, giving predictable, non-compounding sizing, which is why most modern CSS defaults to rem for spacing and typography.

Example: When to Choose rem Over em

css
<style>
html { font-size: 16px; }
.a { font-size: 1.5em; }
.c { font-size: 1.5rem; }
</style>
<div class="a">em compounds when nested inside other em-sized elements</div>
<div class="c">rem always stays relative to the root</div>

When Percentage Makes More Sense Than viewport Units

Percentage is relative to the parent element's size, not the viewport, which makes it the right choice for components that should scale with their container rather than the whole screen — for example, a sidebar width inside a flexible layout, or an image that should fill its wrapping div.

Example: When Percentage Makes More Sense Than viewport Units

css
<style>
.sidebar {
  width: 25%;
  background: steelblue;
  color: white;
  padding: 10px;
}
</style>
<div style="display:flex; background:#eee;">
  <div class="sidebar">Scales with its parent, not the whole viewport</div>
</div>

Combining Relative Units for Robust Sizing

Production CSS often mixes relative units deliberately: rem for typography and spacing that should respect user font preferences, % for container-relative sizing, and vw/vmin sparingly for hero sections that should genuinely scale with the viewport.

Relying on just one unit everywhere usually produces worse results than choosing the right unit per situation.

Example: Combining Relative Units for Robust Sizing

css
<style>
h1 {
  font-size: 2rem;
}
.container {
  width: 80%;
}
.hero {
  min-height: 40vmin;
  background: linear-gradient(135deg, #667eea, #764ba2);
}
</style>
<div class="container">
  <h1>rem, %, and vmin, each used for what it's best at</h1>
  <div class="hero"></div>
</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Confusing em (relative to the parent's font size) with rem (relative to the root's font size), so nested em values compound.
  2. Using vw for font sizes, which can make text too small or big and ignores user zoom.
  3. Using 100vh on mobile and getting content hidden behind the browser bars.
Chapter Summary
  • Website layout is organized with CSS, and gap adds space between items.
  • CSS units set sizes and lengths.
  • Absolute and relative units behave differently.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.