CSS Box Model Explained: Margin, Border, Padding
The CSS Box Model is a fundamental concept that describes the rectangular boxes generated for elements in the document tree and how they are displayed on a webpage. Understanding the box model is crucial for controlling layout and design in CSS.
Components of the Box Model
- Content:
- The actual content of the box, such as text, images, or other elements.
- Padding:
- Clears an area around the content. The padding is inside the box and affects the box’s background color.
- Border:
- A border surrounding the padding (if any) and content. The border’s style, width, and color can be customized.
- Margin:
- Clears an area outside the border. Margins are transparent and separate the element from other elements on the page.
Visualization of the Box Model
Explanation
- Margin: The outermost
divwith a label in the center reading “Margin.” - Border: The blue
divinside the margin with a centered label reading “Border.” - Padding: The green
divinside the border with a centered label reading “Padding.” - Content: The innermost
divwith a centered label reading “Content.”
Box Model Properties
widthandheight:- Define the width and height of the content area.
element { width: 300px; height: 200px; }
- Define the width and height of the content area.
padding:- Adds space around the content, inside the border.
element { padding: 20px; /* Applies 20px padding to all sides */ }
- Adds space around the content, inside the border.
border:- Sets the border around the padding and content.
element { border: 2px solid black; /* 2px wide solid black border */ }
- Sets the border around the padding and content.
margin:- Creates space around the element, outside the border.
element { margin: 10px; /* Applies 10px margin to all sides */ }
- Creates space around the element, outside the border.
Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.box {
width: 300px;
height: 200px;
padding: 20px;
border: 5px solid blue;
margin: 30px;
background-color: lightgray;
}
</style>
<title>CSS Box Model Example</title>
</head>
<body>
<div class="box">
This is a box model example.
</div>
</body>
</html>
Explanation
- Content: The text “This is a box model example.”
- Padding: 20px space between the content and the border.
- Border: 5px solid blue border around the padding.
- Margin: 30px space between the box and other elements or the page edge.
Notes
The total width and height of an element are calculated as:
Total Width = width + padding-left + padding-right + border-left + border-right + margin-left + margin-right Total Height = height + padding-top + padding-bottom + border-top + border-bottom + margin-top + margin-bottom
box-sizingProperty:- Controls how the total width and height are calculated.
- By default,
box-sizingiscontent-box, which includes only the content’s width and height. - Setting
box-sizing: border-box;includes padding and border in the element’s total width and height.element { box-sizing: border-box; }