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)?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
One of the key differences between margin and padding is not mentioned in any of the answers: clickability and hover detection
Increasing the padding increases the effective size of the element. Sometimes I have a smallish icon that I don't want to make visibly larger but the user still needs to interact with that icon. I increase the icon's padding to give it a larger footprint for clicks and hover. Increasing the icon's margin will not have the same effect.
An answer to another question on this topic gives an example.
padding
is the space between the content and theborder
, whereasmargin
is the space outside the border. Here's an image I found from a quick Google search, that illustrates this idea.Padding
Padding is a CSS property that defines the space between an element content and its border (if it has a border). If an element has a border around it, padding will give space from that border to the element content which appears in that border. If an element does not have a border around it, then adding padding has no effect at all on that element, because there is no border to give space from.
Margin
Margin is a CSS property that defines the space of outside of an element to its next outside element.
Margin affects elements that both have or do not have borders. If an element has a border, margin defines the space from this border to the next outer element. If an element does not have a border, then margin defines the space from the element content to the next outer element.
Difference Between Padding and Margin
So the difference between margin and padding is that while padding deals with the inner space, margin deals with the outer space to the next outer element.
It is good to know about the differences between
margin
andpadding
. As I know:auto
value to margin. However, it's not allowed for padding. See this.Note: Use
margin: auto
to center a block element inside its parent horizontally. Also, it's possible to center an element inside a flexbox vertically or horizontally or both, by setting margin to auto. See this.background-color
property to black, its inner space (i.e. padding) will be black, but not its outer space (i.e. margin).There is one important difference:
Margin- is on the outside of the element i.e. one will apply the whitespace shift "after" the element begins. Padding- is on the inside, the other will apply the whitespace "before" the element begins.
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