If you think about it, ironically, the margins of a sheet of notebook paper are actually padding. So it can get kind of confusing.
Margins are exterior space. Padding is interior space.
Padding can undesirably affect the sizes of elements. Like for example, giving a div that’s 100px wide a padding of 20px will change the div’s width to 140px. The simplest workaround for this is to use box-sizing: border-box in your css. This will keep your elements at the dimensions you told them to be.
Margins, on the other hand, simply add exterior space around an element. Like a force field.
Also good to know (unless I'm misstaken) margins can collapse. So two elemens next to eachother with 20px margin won't have 40px between eachother, but just 20px.
2
u/HailAtlantis Mar 07 '18
If you think about it, ironically, the margins of a sheet of notebook paper are actually padding. So it can get kind of confusing.
Margins are exterior space. Padding is interior space.
Padding can undesirably affect the sizes of elements. Like for example, giving a div that’s 100px wide a padding of 20px will change the div’s width to 140px. The simplest workaround for this is to use box-sizing: border-box in your css. This will keep your elements at the dimensions you told them to be.
Margins, on the other hand, simply add exterior space around an element. Like a force field.