I have a problem with the margin and padding

I have a problem with the margin and padding


Guys I have a problem to understand what is the function of margin and padding?


Both are parts of the CSS box model. Basically, every HTML element is a rectangle with the following parts:

  • the content (the innermost part). This is where the actual content of the element reside (e.g., text, other nested elements).
  • the padding. This is the space around the content. This part is still inside the rectangle and is therefore affected by things like the background color of the element. Tweak this to adjust the space between the content and the border.
  • the border. The space around the padding. Should be pretty self-explanatory.
  • the margin. The space around the border. This part is outside the box, and is transparent. If you want to add space between two elements, this is what you’ll tweak.

You can see the box model in action when you open your browser’s devtools. You can select an element from a page and you can see its box model. Here’s what it looks like in Firefox. The numbers on the edges are sizes of the box model parts in all sides.

More reading: