The CSS Box Model is a fundamental concept in web design that defines how elements are structured and spaced in a webpage. Every HTML element is considered a rectangular box consisting of four key areas: Content, Padding, Border, and Margin.
Each HTML element is composed of the following layers:
1️⃣ Content – The actual text, image, or element inside the box.
2️⃣ Padding – Space between the content and the border.
3️⃣ Border – A visible line surrounding the padding and content.
4️⃣ Margin – Space outside the border, creating distance between elements.
+-----------------------------+ ← Margin
| (Outer Space) |
| +---------------------+ ← Border
| | (Inner Space) | ← Padding
| | [ Actual Content ] |
| +---------------------+
+-----------------------------+
Property | Description | Example |
---|---|---|
width |
Sets the width of the content box. | width: 200px; |
height |
Sets the height of the content box. | height: 100px; |
padding |
Space inside the element, around the content. | padding: 20px; |
border |
Defines the thickness and style of the border. | border: 5px solid black; |
margin |
Space outside the border, separating the element from others. | margin: 30px; |