views:

660

answers:

6

i totally understand the box model. this question is more about trying to pin down a semantic methodology regarding when to use margins and when to use padding.

here is a typical example,

first, in plain English:

  • situation: we have a container div, inside of which there is a paragraph element.
  • goal: to have a 12px space between the inside of the div and the outside of the paragraph.

  • option a) apply 12px of padding to the container div

  • option b) apply 12px margins to the paragraph element

or, if you prefer, HTML:

<div id="container">
    <p>Hello World!</p>
</div>

and, CSS:

option a)

div#container {padding: 12px;}

option b)

p {margin: 12px;}

Cheers!

Jon

A: 

Vertical padding on the division - because if I decided I wanted a different amount of vertical space in between the multiple paragraphs I'd use bottom margins, and the top/bottom padding of the enclosing division pretty much will always stay intact assuming you just have staticly positioned elements inside.

meder
+1  A: 

It depends on what you're trying to accomplish visually. Would container have other child elements which might hang over into the gutter on either side of the paragraph? If so, a margin makes more sense. But if container should have a 12-pixel gutter for all elements, period, it makes the most sense to use the padding to avoid having to apply margins to multiple element sets.

Generally speaking you always want paragraphs to have vertical margins to ensure consistent paragraph leading.

Rex M
what do you mean by "gutter"? :)
jon
@jon the gutter is the unused space on one or more sides of text. It's different from a margin because it might make sense for a graphical element to hang into the gutter.
Rex M
thanks for explaining!
jon
+1  A: 

Personally, I prefer option A. Why? Say now I have to add other HTML elements into the div and I want the padding to be maintained, I would not have to add other rules to my CSS files to get it working.

Dhana
for me, this is the best answer. but you must consider your specific situation. see "Itay Moav's" answer.
jon
A: 

The difference is where the border sits.

The border sits SMACK DAB in the middle of the margins and padding. If you specify margins, that is white space OUTSIDE the border.

If you specify padding, that is white space INSIDE the border (pushes the border further out from the element)

Can't show you here due to css stripping, but try this out:


<body style="background-color: #aaa">
<p style="background-color: #aee; margin: 40px; padding: 40px; border: solid 2px black;">
  i have margins, padding and a border.
</p>

<p style="background-color: #aee; margin: 40px; padding: 0; border: solid 2px black;">
  i have margins, and a border.
</p>

<p style="background-color: #aee; margin: 0; padding: 40px; border: solid 2px black;">
  i have padding and a border.
</p>
</body>

other stuff!

  • padding brings in background color of the element, margins are basically transparent

  • some elements ( like td ) seem to ignore margins, while they respond to changes in padding

bobobobo
right. but what about when there's no border involved? :)
jon
A few things to consider: - padding brings in background color of the element, margins are basically transparent - some elements ( like td ) seem to ignore margins, while they respond to changes in padding
bobobobo
+1  A: 

Personally, I'd go with option a of #container {padding: 12px;} because it makes amply clear that all child elements must stay 12px away from the border of this div.

If I want other elements to stay more than 12px away from the #container's border, then I apply as much more margin to that element.

Cheers!

Here Be Wolves
A: 

Paddings and margins gives the same effect, Except in the following cases (I might miss some):

  1. You have some kind of background properties. Margins won't get them.
  2. You have a border
  3. You use TD (no margins)
  4. Two nested items, The margins are collapsed together, where paddings not.
  5. (need to check this one) They probably affect the width and height of the element differently. (If some one knows better, pls edit this).
Itay Moav