Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

This is a bug in css, here are examples:

http://creexe.zxq.net/div-issue-padding.html = padding issue

http://creexe.zxq.net/div-issue-margin.html = margin issue

the red and green div tags in the examples were created by the css property TOP,but it has its own disadvantages athat TOP,BOTTOM etc works only when the position of the div tag is Absolute and relative, but not static

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.

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!

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top