Performance: Number types and units
How CSS number units affect layout calculations and rendering speed.
$width: 50%; $margin: 1.25rem; .element { width: $width; margin: $margin; }
$width: 100px; $margin: 20px; .element { width: $width; margin: $margin; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fixed px units | No extra DOM nodes | Multiple reflows on resize | Moderate paint cost | [X] Bad |
| Relative units (%, rem) | No extra DOM nodes | Single reflow on resize | Lower paint cost | [OK] Good |
px, %, em, rem, etc.12px uses a valid unit px. Others use invalid or misspelled units.5em is correct with no space. Others have spaces or invalid units.$width: 10px * 2;
.result {
width: $width;
}10px by 2 doubles the value but keeps the unit.20px, which is valid CSS for width.$height: 50px + 10;
.box {
height: $height;
}50px and 10 (unitless) causes a unit mismatch error.2rem and 5% directly.$small-padding normally and override with $large-padding inside media query.Pass the test and this pattern moves unknown