views:

230

answers:

5

In W3 CSS and also in XUL/CSS? (not between CSS and XUL/CSS).

+5  A: 

Padding is the space INSIDE an element (inside the border of the element).

Margin is the space OUTSIDE(Around) an element.

webdestroya
@downvoter - any reason for this?
webdestroya
uhh. Why is this being voted up? According to the CSS box model this is untrue. http://www.w3.org/TR/CSS2/box.html
ghoppe
@ghoppe - What are you talking about? Margin is the spacing around/outside an element. Padding is internal.
webdestroya
The reason: element width does not include padding. Read the spec.
ghoppe
@ghoppe that really doesn't change my answer. He didnt mention width, he asked the difference.
webdestroya
Nice pic here: http://css-tricks.com/the-css-box-model/
ghoppe
@webdestroya of course it changes your answer, as it is demonstrably wrong. Look at the picture I linked above. Padding *IS NOT* inside the element!
ghoppe
@ghoppe ... your picture just confirms my post. Look at what everyone else is saying in theirs, we are all correct.
webdestroya
By inside, i meant inside the border.
webdestroya
You didn't say border, you said element.
ghoppe
There, I have clarified my response so that it is clear now.
webdestroya
@all thanks to everyone. however, could you elaborate more on why the width of element is not changed by padding? My understanding is that the more is padding the more width/height of the element. Is it not true?
Michael
@Michael Sort of. padding is more like the fat on a skeleton. It doesn't change the size of the skeleton, but makes the person appear larger. If you set Width=500, then added 100px of padding to both sides, your element would be 700px wide, but the width of the content-area inside would still be 500px.
webdestroya
I guess padding is just squeezing the content inside...
Michael
The difference is the definition of "width". If you define something with a fixed width (Say 500px) in css with no padding, it will be 500px on screen. If you give it padding, then 10px of padding will be filled in around it.
ghoppe
@Michael no it's not. Unless you're a broken MSIE box model. The opposite happens -- padding makes the content take up more space. ;-)
ghoppe
Yes complete width of the box is the `leftMargin` + `leftBorder` + `leftPadding` + `width` + `rightPadding` + `rightBorder` + `rightMargin`
rebus
@all. got it! Wish I could accept all of the answers... anyway, at least upvoted :)
Michael
the padding makes the border to move(expand in particular). the margin makes surrounding elements to move.
Michael
@Michael - yes, that is correct :)
webdestroya
Why would you accept the least precisely worded answer? Padding is *not* included in the css width of an element.
ghoppe
@ghoppe: because this guy put a lot of efforts to defend his answer. the actual answer is within this conversation, not in initial post.
Michael
@Michael: fair enough, I hope someone does the same for me when I defend an imprecise answer. :)
ghoppe
@Michael - Thanks, I really appreciate it :)
webdestroya
By the way, one more thing to think about proving this answer is still quite confusing and imprecise. Is the border of an element inside or outside the element?
ghoppe
It is part of the element.. give it a rest already.
webdestroya
@webdestroya and so is margin. that's my whole point.
ghoppe
@webdestroya @Michael said "he padding makes the border to move(expand in particular). the margin makes surrounding elements to move." -- padding also causes surrounding elements to move. This trips up many web designers.
ghoppe
Another down vote for this - I don't think the right answer has been accepted here. If a novice reader was to read only the question and this 'answer', they'd come away with a completely incorrect understanding of this important aspect of the box model.ghoppe's answer is the best - it clearly explains the key points without ambiguity.
Beejamin
+2  A: 

Padding is the area between the content and the border while the margin is the area outside the border.

See CSS box model for clarification.

rebus
+14  A: 

I recommend this article for you. The diagram is from that article.

alt text

http://elegantcode.com/2010/01/26/css-basics-the-box-model/

CodeToGlory
A: 

padding is part of the width of an element. margin is outside and isn't part of the width

Here's an interesting box model demo that will help you understand.

http://redmelon.net/tstme/box_model/

Galen
Padding is not part of the width of an element. `#div1 {width: 500px; padding: 10px;}` will look bigger on the screen than `#div2 {width: 500px; padding: 0px;}` (unless you're a broken MSIE box model: http://www.456bereastreet.com/archive/200612/internet_explorer_and_the_css_box_model/)
ghoppe
yes padding will make that box "wider". that box will be 520px wide. the broken ie box model will still be 500px wide, it makes room for the padding. its semantics. i know what you're saying, i guess it's just hard to put into words correctly. needs a picture.
Galen
+1  A: 

OK, several of these answers use confusing terminology and are wrong.

The css width of an element does not include padding, borders, or margin.

Therefore it is imprecise to say "padding is inside an element".

It is correct to say "padding is inside the border of an element" and "margin is outside the border of an element".

To calculate how much space a box takes up (for example, just horizontally):

horiz. space = width + 2(padding) + 2(border) + 2(margin)

It bugs me when people say "padding is space inside an element" because: the element has padding, it has a border, and it has margin. All of that stuff is outside the content width of the element, and must be accounted for when calculating how much space the element takes up.

If you say "padding is inside the element" then you are making the same mistake MSIE did in its broken box model, causing untold web designers many headaches.

http://www.456bereastreet.com/archive/200612/internet_explorer_and_the_css_box_model/

ghoppe