Margin and Padding can be one of the most confusing concepts for a beginner To understand, It Is such a common error that a beginner can make getting both of these css functions mixed up, why is this? to put it simply they can appear to to work in a very similar manner, The content of this blog post will show you in theory how each of these css functions are designed to work and help you differentiate between the both of them.
Margin is a css function that is used to provide an element external space from itself to the nearest positioned element, for example If you I want a margin top of 100px you are essentially telling the webpage "give this element space of 100px from the top of the nearest element". (demonstration with an explanation below).
as can be seen In the example above the blue div has margin-top space of 50 pixels, this essentially means the blue div has an external space of 50px away from the red div In the diagram above.
padding Is used to provide space within the element, from the elements border to the elements content, for example If you I want a padding bottom of 50px you are essentially saying "give the elements content a space of 50px from the bottom border".
as can be seen In the example above the blue div has margin-top space of 50 pixels, this essentially means the blue div has an external space of 50px away from the red div.
Understanding the differences between padding and margin Is very beneficial and is a fundamental In web development.To summarise simply the differentiate between margin and padding, is that margin is utilised to create external space from one element to another, where as padding is designed to create internal space within an element.