Skip to main content
Spelling, grammar, formatting revision
Source Link
TylerH
  • 21.3k
  • 84
  • 84
  • 121

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 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 considered to be part of the element and is always preserved.

So youYou 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 the content of the two elements will end up being 2em apart.

Now replace that padding with 1em1em margin. Margins are considered to be outside of the element, and margins of adjacent items will overlap.

So inIn this second 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences aredifference is that padding is included in the click region and background color/image, but not the margin is not.

div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; } div.padding > div { padding-top: 20px; } div.margin > div { margin-top: 20px; }
<h3>Default</h3> <div class="box"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>padding-top: 20px</h3> <div class="box padding"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>margin-top: 20px; </h3> <div class="box margin"> <div>A</div> <div>B</div> <div>C</div> </div>

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

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 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 the 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; } div.padding > div { padding-top: 20px; } div.margin > div { margin-top: 20px; }
<h3>Default</h3> <div class="box"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>padding-top: 20px</h3> <div class="box padding"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>margin-top: 20px; </h3> <div class="box margin"> <div>A</div> <div>B</div> <div>C</div> </div>

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 considered to be part of the element and is always preserved.

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 the 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.

In this second 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other big difference is that padding is included in the click region and background color/image, but margin is not.

div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; } div.padding > div { padding-top: 20px; } div.margin > div { margin-top: 20px; }
<h3>Default</h3> <div class="box"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>padding-top: 20px</h3> <div class="box padding"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>margin-top: 20px; </h3> <div class="box margin"> <div>A</div> <div>B</div> <div>C</div> </div>

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

Post Made Community Wiki by pavon
Added example
Source Link
Black
  • 20.9k
  • 47
  • 188
  • 300

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 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 the 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; } div.padding > div { padding-top: 20px; } div.margin > div { margin-top: 20px; }
<h3>Default</h3> <div class="box"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>padding-top: 20px</h3> <div class="box padding"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>margin-top: 20px; </h3> <div class="box margin"> <div>A</div> <div>B</div> <div>C</div> </div>

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 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 the 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

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 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 the 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

div.box > div { height: 50px; width: 50px; border: 1px solid black; text-align: center; } div.padding > div { padding-top: 20px; } div.margin > div { margin-top: 20px; }
<h3>Default</h3> <div class="box"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>padding-top: 20px</h3> <div class="box padding"> <div>A</div> <div>B</div> <div>C</div> </div> <h3>margin-top: 20px; </h3> <div class="box margin"> <div>A</div> <div>B</div> <div>C</div> </div>

Highlighting code, removing unnecessary emphasis
Source Link
TylerH
  • 21.3k
  • 84
  • 84
  • 121

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 verticalvertical margins auto-collapse, and padding doesn't.

Consider two elements one above the other each with padding of 1em1em. 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 the content of the two elements will end up being 2em2em 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 1em1em of combined margin followed by the content of the second element. So the content of the two elements is only 1em1em apart.

This can be really useful when you know that you want to say 1em1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

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 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 the 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

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 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 the 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 to say 1em of spacing around an element, regardless of what element it is next to.

The other two big differences are that padding is included in the click region and background color/image, but not the margin.

formatting a little more readable now
Source Link
shareef
  • 9.6k
  • 16
  • 64
  • 94
Loading
Moved the "conclusion" to the top
Source Link
allegutta
  • 5.6k
  • 11
  • 41
  • 59
Loading
Made it clear that only vertical margins collapse
Source Link
GreenAsJade
  • 14.7k
  • 11
  • 68
  • 103
Loading
Source Link
pavon
  • 16.6k
  • 1
  • 25
  • 26
Loading