Complete Beginner Guide with Examples
Margin is the space outside an element's border. It creates distance between one element and another element.
.box{
margin:20px;
}
Padding is the space inside the element between the content and its border. It increases internal spacing.
.box{
padding:40px;
}
.combo-box{
margin:30px;
padding:30px;
}
margin-top:20px; margin-right:20px; margin-bottom:20px; margin-left:20px; padding-top:20px; padding-right:20px; padding-bottom:20px; padding-left:20px;
margin:10px 20px 30px 40px; /* top right bottom left */ padding:20px 40px; /* top-bottom | left-right */
| Feature | Margin | Padding |
|---|---|---|
| Location | Outside Border | Inside Border |
| Controls | Space between elements | Space inside element |
| Background Color | Not affected | Background visible |
| Purpose | Layout spacing | Content spacing |
👉 Margin = Outside Space (Neighbors)
👉 Padding = Inside Space (Comfort)