Interview Question

Difference between px, em, rem, and %?

px is absolute; em, rem, and percentages are context-relative.

💡 Concept ✅ Quick Revision 🎨 CSS

Answer

px, em, rem, and percentages use different reference sizes. • A CSS pixel is an absolute CSS unit, not necessarily one device pixel. • em uses the relevant element font size; rem uses the root element font size. • Percentages use the reference defined by each property.

💡 Simple Example

.title { font-size: 2rem; margin-block: .5em; width: 80%; }

⚡ Quick Revision

px is absolute; em, rem, and percentages are context-relative.