Performance: Padding
Padding affects layout size and can trigger reflows when changed, impacting rendering speed and visual stability.
Jump into concepts and practice - no test required
div { padding: 1.25rem 0.625rem; } /* Using relative units for consistent spacing */div { padding: 20px 10px 20px 10px; } /* Large padding causing layout shift on resize */| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fixed large padding in px | Minimal | Triggers reflow on resize | Medium paint cost | [!] OK |
| Relative padding in rem/em | Minimal | Single reflow on initial load | Low paint cost | [OK] Good |
| Padding changed dynamically via JS frequently | Multiple DOM writes | Multiple reflows | High paint cost | [X] Bad |
padding property do in a webpage layout?padding: 20px;.padding: 20; misses units, padding: 20px all; and padding: all 20px; use invalid keywords.div { padding: 10px 20px 30px 40px; }div element?p { padding: 10px 20; }section element, leaving top and bottom padding at 0. Which CSS rule achieves this correctly?section { padding: 0 15px; } sets top/bottom padding to 0 and left/right padding to 15px correctly.section { padding: 15px 0; } reverses values (top/bottom 15px, left/right 0px). section { padding-left: 15px; padding-right: 15px; padding-top: 15px; padding-bottom: 0; } sets top padding to 15px incorrectly. section { padding: 15px 15px 0 0; } sets top/right 15px, bottom/left 0px.