Difference between margin and padding?

2018-12-31 21:30发布

What exactly is the difference between margin and padding in CSS? It really doesn't seem to serve much purpose. Could you give me an example of where the differences lie (and why it is important to know the difference)?

标签: css
19条回答
倾城一夜雪
2楼-- · 2018-12-31 21:46

Padding is the space between nearest components on the web page and margin is the space from the margin of the webpage.

查看更多
宁负流年不负卿
3楼-- · 2018-12-31 21:47

Margin is applied to the outside of you element hence effecting how far your element is away from other elements.

Padding is applied to the inside of your element hence effecting how far your element's content is away from the border.

Also, using margin will not affect your element's dimensions whereas padding will make your elements dimensions (set height + padding) so for example if you have a 100x100px div with a 5 px padding, your div will actually be 105x105px

查看更多
还给你的自由
4楼-- · 2018-12-31 21:48

Margin is space outside the box; padding is space inside the box. It's hard to see the difference with a white fill, but with a colored fill you can see it fine.

查看更多
明月照影归
5楼-- · 2018-12-31 21:49

margin = space around (outside) the element from border outwards.

padding = space around (inside) the element from text to border.

see here: http://jsfiddle.net/robx/GaMpq/

查看更多
无色无味的生活
6楼-- · 2018-12-31 21:49

Padding is the space between you content and the border. Where as Margin is the space between the border and the other element.

查看更多
怪性笑人.
7楼-- · 2018-12-31 21:52

Basically, the difference between padding and margin come in terms of the background. Padding will decide the space between content, while margin decide the outside edge of elements!

查看更多
登录 后发表回答