🌐CSS Units
Also known as CSS Length Units, px em rem, rem Unit, Viewport Units
The units for sizes in CSS, like px, rem, em, % and vw. Each measures against something different.
Think of it like
It is like measuring with your hand or with a ruler. One grows with you, one does not.
Example
Jack sets his text in rem. A reader who turns up the browser's text size now gets bigger text all over his site.
Why it matters
Units decide how a page reacts to big screens, small screens and bigger text. Fixed sizes break first.
Definition
Example code
css
h1 { font-size: 2rem; } /* twice the root size */
.side { width: 25%; } /* a quarter of the parent */
.hero { height: 50vh; } /* half the window height */Common mistake
Key takeaways
- 1Fixed or relative. px stays, the rest follow something.
- 2rem for text. It follows the reader's text setting.
- 3vw and vh. They follow the window size.