The CSS Box Model is one of the most fundamental concepts in web design and layout. Every HTML element on a webpage is treated as a box, and understanding how these boxes work helps you control spacing, alignment, borders, and sizing accurately.
The Box Model is made up of four layers:
This is the actual content inside the element — text, images, or other items you place inside a tag.
p {
width: 200px;
}
Padding is the space between the content and the border. It increases the clickable or readable area without affecting the border.
p {
padding: 20px;
}
The border surrounds the padding and the content. You can style it with width, color, and type.
div {
border: 2px solid #000;
}
Margin is the space outside the border, used to create space between elements.
div {
margin: 30px;
}
By default, width and height apply only to the content box. To make width include padding + border, use:
* {
box-sizing: border-box;
}
This makes layout easier and is recommended by most developers.
| Property | Description |
|---|---|
| width / height | Size of content area |
| padding | Space between content and border |
| border | Line around padding |
| margin | Space outside the border |
| box-sizing | Controls how width/height are calculated |