CSS Box Model
CSSEvery HTML element is a box. The CSS Box Model describes the space around elements:
- Content — The actual text/image
- Padding — Space between content and border
- Border — The edge of the box
- Margin — Space outside the border
Understanding the box model is essential for layout control.
.card {
/* Content width */
width: 300px;
/* Padding: space inside */
padding: 20px;
/* Border */
border: 2px solid #e2e8f0;
border-radius: 12px;
/* Margin: space outside */
margin: 16px auto;
/* Box sizing (important!) */
box-sizing: border-box;
}
padding: 20px, border: 2px, border-radius: 12px
Interview Questions — CSS Box Model
5 questions commonly asked in interviews
The CSS box model describes how every HTML element is treated as a box with content, padding, border, and margin.
The four parts are content, padding, border, and margin.
Padding is the space between the content and the border of an element.
Margin is the space outside the border that separates one element from another.
It includes padding and border inside the element width and height, making layout calculations easier.
Frequently Asked Questions
Common doubts about CSS Box Model
Margin is outside the border.
Padding is inside the border, between content and border.
Yes, margin can have negative values, but it should be used carefully.
No, padding cannot have negative values.
It makes element width easier to manage by including padding and border inside the declared width.
Test Your Knowledge
5 questions · Earn 50 XP
More on CSS Box Model
Cheatsheet, tips, resources & what to learn next
Quick Cheatsheet
.box { padding: 20px; }
.box { margin: 20px; }
.box { border: 1px solid #000; }
* { box-sizing: border-box; }
.box { width: 300px; padding: 20px; border: 1px solid #ccc; }
Pro Tips
Use box-sizing: border-box globally in most projects.
Use padding for internal spacing.
Use margin for spacing between elements.
Avoid using margin when padding is more suitable.
Inspect box model in browser developer tools.