Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Last active August 29, 2015 14:21
Show Gist options
  • Save murarisumit/6bd48a631966915e3f97 to your computer and use it in GitHub Desktop.
Save murarisumit/6bd48a631966915e3f97 to your computer and use it in GitHub Desktop.
Margin vs Padding in CSS

Consider two elements next to each other each with padding of 1em. This padding is considered to be part of the element, and is always preserved. So you will end up with the content of the first element, followed by the padding of the first element, followed by the padding of the second. followed by the content of the second element. Thus content of the two elements will end up being 2em apart.

Now replace that padding with 1em margin. Margins are considered to be outside of the element, and margins of adjacent items will overlap. So in this example you will end up with the content of the first element followed by 1em of combined margin followed by the content of the second element. So the content of the two elements is only 1em apart. This can be really useful when you know that you want say 1em of spacing around an element, regardless of what element it is next to. The other two big differences is that padding is included in the click region and background color/image, but not the margin.

**Margins auto-collapse, and padding doesn’t.**

By default I prefer margin everywhere, except when I've a border or background and want to increase the space inside that visible box.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment