What is the difference between margin and padding in CSS?
Padding is the space inside the element, between the content and the border. Margin is the space outside the border, between this element and others.
| Point | Padding | Margin |
|---|---|---|
| Position | Inside the border | Outside the border |
| Background colour | Shows in the padding | Margin is always transparent |
| Negative value | Not allowed | Allowed |
auto | No | Yes, to centre a block |
Output in the browser
padding 20px, margin 12px
Learn more about the similar topics:
| Tutorials |
|---|
| CSS Borders |
| Rounded Corners in CSS |
| CSS Height and Width |
| CSS Padding |
| CSS Margins |
| CSS Box Model |
| CSS Outline |
| Exercises & Assignments |
|---|
| Write CSS to create a box with border, padding and rounded corners |
| Write CSS to create a circle and a pill-shaped button |
| Write CSS to style a login form |
| Interview Questions & Answers |
|---|
| What is the CSS box model? |
| What is the difference between margin and padding in CSS? |
| What is box-sizing: border-box in CSS? |