Hacker News new | ask | show | jobs
by waterside81 5550 days ago
That's interesting. I've always wondered if some people had conventions on when to use padding and when to use margin, since you can almost always accomplish the desired effect either way.
4 comments

Except that adjacent vertical margins collapse on each other, so the space is the larger of the 2 margin values not the sum.

more info: http://reference.sitepoint.com/css/collapsingmargins

This is not true of padding (unless you count a bug in IE8 where percentage-based padding-bottom collapses on the margin-bottom)

If you want to be modular, you must have conventions. This is to prevent double spacing when you put two elements one next to another without additional styling. I also use to have ELEMENT > *:first-child {padding-top: 0} on containers, so that I can control the padding of the container and without having to consider what element I place into it. There's a lot of best practices I made part of my workflow, perhaps I should write a blog post about it.
You can't really. CSS backgrounds are the main reason you need to use padding (instead of margin): margins exist outside an element, while padding expands within the box itself (beyond the content, but permitting the background to remain visible).

To visualize it, http://hicksdesign.co.uk/boxmodel/ is probably the most oft-cited example - certainly the one I find most helpful.

Ok, I was talking about transparent elements, where in fact you have complete choice (at least if you have no borders too).
In my experience, that is almost never the case, except when you're faking margin by using padding.

In that case, use margin.