Q7: What is the difference between rem, em, vh, and vw units?

  • rem (Root EM): Relative to font-size of the root <html> element (default 16px).
  • em: Relative to font-size of the parent or local element (compounds on nested elements).
  • vh / vw: Percentage of viewport height / width (1vh = 1% viewport height).
rem vs em Comparison
html {
  font-size: 16px;
}
.card {
  font-size: 1.25rem; /* 20px */
  padding: 1em;       /* 20px (relative to card's own font-size) */
}

rem references the root html font-size, while em calculates based on local font-size.

Key Takeaways
  • rem ensures consistent scaling relative to root html font configuration.

  • em scales relative to immediate parent font size and compounds when nested.

Knowledge Check

If root html has font-size: 16px, what is the calculated value of 2.5rem?