Performance: Spacing utilities (margin, padding)
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 |
p stands for padding and m stands for margin.p-3 adds padding of size 3 on all sides, while pt-3 adds padding only on top, and ml-3 adds margin on the left.m for margin and b for bottom side.mb-4. Other options have incorrect order or missing dash.px-5 have on a Bootstrap element?p means padding, and x means horizontal sides (left and right).ml-2 and it doesn't work. What is the likely mistake?ml-2 is replaced by ms-2 (start instead of left), so ml-2 won't work.ml-2, will apply styles. But the specific class points to version issue.ml-2 is invalid in Bootstrap 5. -> Option Apy means padding on vertical sides (top and bottom), and mx means margin on horizontal sides (left and right).py-3 adds padding top and bottom size 3, mx-4 adds margin left and right size 4, exactly what is asked.Pass the test and this pattern moves unknown