🔹 Definition: Pixel (px) is an absolute unit of measurement. 1 pixel = 1/96th of an inch. Pixels are fixed and don't change based on parent elements or screen size.
📌 Use Case: Borders, small spacing, precise layouts that shouldn't scale
🔹 Definition: Percentage (%) is a relative unit that's calculated based on the parent element's size. 100% means full width/height of parent container.
📌 Use Case: Responsive layouts, grid systems, flexible designs
🔹 Definition: EM (em) is a relative unit that's calculated based on the parent element's font size. 1em = parent's font size, 2em = 2x parent's font size.
📌 Use Case: Font sizing, responsive typography, scalable components
🔹 Definition: REM (rem) is a relative unit based on the root element's (html) font size, not the parent. This solves the cascading problem of EM. By default, root font size is 16px.
📌 Use Case: Recommended for responsive design, modern web development
| Unit | Type | Value | Best For | Example |
|---|---|---|---|---|
| PX | Absolute | Fixed size | Borders, icons | width: 24px; |
| % | Relative | % of parent | Responsive layouts | width: 50%; |
| EM | Relative | × parent font | Typography, spacing | font-size: 1.5em; |
| REM | Relative | × root font | Responsive design | font-size: 1.5rem; |