Why does this CSS margin-top style not work?

You’re actually seeing the top margin of the #inner element collapse into the top edge of the #outer element, leaving only the #outer margin intact (albeit not shown in your images). The top edges of both boxes are flush against each other because their margins are equal. Here are the relevant points from the W3C … Read more

Why is there an unexplainable gap between these inline-block div elements?

In this instance, your div elements have been changed from block level elements to inline elements. A typical characteristic of inline elements is that they respect the whitespace in the markup. This explains why a gap of space is generated between the elements. (example) There are a few solutions that can be used to solve this. Method 1 – Remove the whitespace from the markup Example … Read more

What does auto do in margin: 0 auto?

When you have specified a width on the object that you have applied margin: 0 auto to, the object will sit centrally within it’s parent container. Specifying auto as the second parameter basically tells the browser to automatically determine the left and right margins itself, which it does by setting them equally. It guarantees that the left and right margins will … Read more

When to use margin vs padding in CSS [closed]

TL;DR: By default I use margin everywhere, except when I have a border or background and want to increase the space inside that visible box. To me, the biggest difference between padding and margin is that vertical margins auto-collapse, and padding doesn’t. Consider two elements one above the other each with padding of 1em. This padding is … Read more