Skip to content
Justice Tech School

CSS Units

Absolute px is a fixed size. em is relative to the element's own font size (or the parent's for font-size itself), and rem is relative to the root font size, which makes it predictable. % is relative to the parent, vw and vh are 1% of the viewport width and height.

Use rem for font sizes and spacing so users who change their browser default get proportional results. Use % and vw for fluid widths, and vh for full-screen sections.

Example
<style>
  html { font-size: 16px; }
  .rem { font-size: 1.5rem; }
  .half { width: 50%; background: #fde68a; padding: 6px; }
  .vw { width: 30vw; background: #a7f3d0; padding: 6px; }
</style>
<p class="rem">1.5rem equals 24px here</p>
<div class="half">50% of the parent</div>
<br>
<div class="vw">30vw of the viewport</div>

Exercise

Set the width to 80% of the viewport width.

.hero {
  width: 80;
}

Quiz Test

If the root font size is 16px, how big is 2rem?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.