CSS Box 5 Model Basics
CSS Box 5 Model Basics
The CSS Box Model is the foundation of layout in CSS. It defines how elements are structured
and spaced on a webpage. The key components of the box model are:
1. Padding
Padding controls the space inside the element, between the content and the border.
css
.box {
padding: 20px; /* Applies 20px padding on all sides */
}
css
.box {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 20px;
padding-left: 25px;
}
1/3
2. Border
The border wraps around the padding and content, defining the element's visible edge.
css
.box {
border: 2px solid black; /* 2px width, solid style, black color */
}
css
.box {
border-top: 5px dashed red;
border-right: 3px solid blue;
border-bottom: 4px double green;
border-left: 2px dotted purple;
}
3. Box Sizing
By default, the width and height only apply to the content, but box-sizing controls how
padding and borders are included.
css
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: border-box; /* Ensures total width remains 200px */
}
2/3
4. Example of Box Model
css
.box {
width: 300px;
padding: 20px;
border: 5px solid blue;
margin: 10px;
box-sizing: border-box;
}
lua
+---------------------------------+ (Margin)
| +-------------------------+ | (Border)
| | +-------------------+ | | (Padding)
| | | Content | | | (Content)
| | +-------------------+ | |
| +-------------------------+ |
+---------------------------------+
3/3