what's the difference between padding and margin?

不问归期 提交于 2019-11-29 09:33:12

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

Margin is the space OUTSIDE(Around) an element.

Srikar Doddi

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

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

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.

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/

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/

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!