Difference between margin and padding?

后端 未结 22 2164
广开言路
广开言路 2020-11-22 12:58

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 examp

22条回答
  •  迷失自我
    2020-11-22 13:07

    It is good to know about the differences between margin and padding. As I know:

    • Margin is the outer space of an element, while padding is the inner space of an element. In other words, margin is the space outside of an element's border, while padding is the space inside of its border.
    • You can set 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.
    • Margin can be any float number, but padding must not be negative.
    • When you style an element, padding will be styled also; but not margin. Margin gets the parent element's style. For example, when you set the background-color property to black, its inner space (i.e. padding) will be black, but not its outer space (i.e. margin).

提交回复
热议问题