4 ms·
It's to avoid vertical margin collapsing. The rules for margin collapsing are complex, counter-intuitive, and often considered misconceived. (https://wiki.csswg
by bobince 3y ago
It's to avoid vertical margin collapsing. The rules for margin collapsing are complex, counter-intuitive, and often considered misconceived. (https://wiki.csswg.org/ideas/mistakes https://wiki.csswg.org/ideas/mistakes)
It's typically easier to work out a layout if you use padding in preference so you don't have to worry about them, but first you have to reset the margins the browser gives you.
- wwweston 3y agoI'd agree vertical margin collapse rules have some counterintuitive cases. And I've definitely seen (and used) something like: h1, h2, h3, h4, h5, h6 { margin: 0 0 1em 0; } which fits with the expectation that headers don't need to kick off with a vertical margin along with overflow containment rules to manage that behavior. I don't know if I've seen using padding values to do margin's job lead to less pain.