Performance: Spacing utilities (margin, padding)
MEDIUM IMPACT
Spacing utilities affect the layout and paint stages by adding CSS rules that control margin and padding, impacting rendering speed and visual stability.
<div class="mt-3 ps-3"></div> <div class="mt-3 ps-3"></div> <div class="mt-3 ps-3"></div>
<div style="margin-top: 1rem; padding-left: 1rem;"></div> <div style="margin-top: 1rem; padding-left: 1rem;"></div> <div style="margin-top: 1rem; padding-left: 1rem;"></div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline styles for spacing on many elements | Multiple style recalculations | Multiple reflows (one per element) | High paint cost due to layout thrashing | [X] Bad |
| Bootstrap spacing utility classes | Single style recalculation | Single reflow for all elements | Lower paint cost with cached styles | [OK] Good |
| Dynamic inline style changes on interaction | Immediate style recalculation | Multiple reflows per change | High paint cost and jank | [X] Bad |
| Dynamic class toggling for spacing | Batched style recalculation | Single reflow per batch | Smooth paint with minimal jank | [OK] Good |