Performance: Margin
Margin affects layout calculation and can trigger reflows when changed, impacting page responsiveness and visual stability.
Jump into concepts and practice - no test required
element.style.margin = '10px';
element.style.marginTop = '10px'; element.style.marginBottom = '10px'; element.style.marginLeft = '10px'; element.style.marginRight = '10px';
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Setting margins individually multiple times | Multiple style changes | Multiple reflows (one per change) | Higher paint cost due to repeated layout | [X] Bad |
| Setting margin shorthand once | Single style change | Single reflow | Lower paint cost | [OK] Good |
margin property do?div {
margin: 1rem 2rem 3rem 4rem;
}div?p {
margin: 10;
}