Performance: Relative units
Relative units affect how CSS scales and adapts during page load and resizing, impacting layout stability and rendering speed.
Jump into concepts and practice - no test required
body { font-size: 1rem; margin: 1.25rem; }body { font-size: 16px; margin: 20px; }| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fixed px units | Normal | Multiple on resize/zoom | Moderate | [X] Bad |
| Relative units (rem, em, %) | Normal | Single or none on resize/zoom | Low | [OK] Good |
rem unit always refers to the font size set on the root <html> element.em is relative to the parent element's font size, px is fixed pixels, and vw is relative to viewport width.em units scale relative to the parent element's font size.rem is root-relative, px is fixed, and vw is viewport width relative.<div> if the viewport width is 1000px?div {
width: 50vw;
}1vw equals 1% of the viewport width. So 50vw is 50% of viewport width.p {
font-size: 1.5rem;
}rem units are relative to the root font size, not the parent element.em should be used instead of rem.30vw means 30% of viewport width, which matches the requirement.min-width: 200px ensures the button never shrinks below 200 pixels.30rem is root font-relative; 30% with min-width: 200vw uses parent-relative width and huge min-width; 30em with min-width: 200% uses font-relative and parent-relative units incorrectly.