"Padding" - defines space between border and element content. "Margin" - defines space between border and other outer elements.
CSS provides two properties named margin and padding for keeping space between HTML Box type elements.
No. They have a main difference.
Above visualization is built using below code.
In the first <div>, space is set between the border and outside elements but in the second one it is between the border and text. Third one is a combination of both.
Hope this will help.
But why do we have two properties for the same reason? Are they similar?
No. They have a main difference.
- Padding - defines space between border and element content
- Margin - defines space between border and other outer elements
Demonstration
Look at the code example for visual clarification.Margin set to 25px
Padding set to 25px
Padding & Margin set to 25px
Above visualization is built using below code.
<div style="margin:25px; background:#ffff81; border:#000000 2px dashed;">
Margin set to 25px
</div>
<div style="padding:25px; background:#ffff81; border:#000000 2px dashed;">
Padding set to 25px
</div>
<div style="padding:25px; margin:25px; background:#ffff81; border:#000000 2px dashed;">
Padding & Margin set to 25px
</div>
In the first <div>, space is set between the border and outside elements but in the second one it is between the border and text. Third one is a combination of both.
Hope this will help.
COMMENTS