Loading...
Loading...
In CSS, various units of measurement are used to set sizes (margins, fonts, widths, etc.). Among them, especially important are: px, em, rem. They affect responsiveness, scalability and style inheritance.
font-size: 16px;
padding: 10px 20px;
/* parent */
.parent {
font-size: 16px;
}
/* child */
.child {
font-size: 1.5em; /* 24px relative to parent */
}
em nesting can accumulate:
.outer {
font-size: 16px;
}
.inner {
font-size: 1.2em; /* 16px × 1.2 = 19.2px */
}
.inner .text {
font-size: 1.2em; /* 19.2px × 1.2 = ~23px */
}
<html> level (16px by default).html {
font-size: 16px;
}
.button {
font-size: 1.25rem; /* 20px */
padding: 0.5rem 1rem; /* 8px 16px */
}
| Unit | Relative to? | Scaling Support | Application |
|---|---|---|---|
px | Absolute pixel | ❌ | Fixed sizes |
em | Parent font-size | ✅ (but can accumulate) | Internal padding, margin |
rem | html font-size | ✅ | Text sizes, layout, everything |
px — when you need to fix size regardless of context (e.g., 1px icon).em — for component internal scalability (e.g., padding, margin).rem — for global typography, margins, responsive design.Tip:
Use rem for base sizes, em — for component internal proportions, and px — only in extreme cases.