The CSS Box Model is a fundamental concept that describes how elements are structured and displayed on a webpage. It defines the space an element occupies, including its content, padding, borders, and margins. Understanding the box model is crucial for correctly positioning and sizing elements.
The box model consists of the following four areas (from innermost to outermost):
+-------------------------------+
| Margin |
| +-------------------------+ |
| | Border | |
| | +-------------------+ | |
| | | Padding | | |
| | | +-------------+ | | |
| | | | Content | | | |
| | | +-------------+ | | |
| | +-------------------+ | |
| +-------------------------+ |
+-------------------------------+
width
and height
.Syntax:
padding: 10px; /* Applies padding to all sides */
padding-top: 10px; /* Applies padding only to the top */
padding-right: 20px; /* Applies padding only to the right */
padding-bottom: 15px; /* Applies padding only to the bottom */
padding-left: 25px; /* Applies padding only to the left */
Code Example:
.box {
padding: 20px;
background-color: lightblue;
}
HTML:
<div class="box">This is content inside a box with padding.</div>
.box
will have 20px of space between the content and the border on all sides.Syntax:
border: 2px solid black; /* Creates a 2px solid black border */
border-top: 3px dashed red; /* Creates a dashed red border on top */
border-right: 4px solid blue; /* Creates a solid blue border on right */
Code Example:
.box {
padding: 20px;
border: 5px solid darkblue;
}
HTML:
<div class="box">This is content with a border around it.</div>
.box
element has a 5px solid dark blue border around its content and padding.Syntax:
margin: 20px; /* Applies margin to all sides */
margin-top: 15px; /* Applies margin only to the top */
margin-right: 25px; /* Applies margin only to the right */
margin-bottom: 30px; /* Applies margin only to the bottom */
margin-left: 10px; /* Applies margin only to the left */
Code Example:
.box {
margin: 30px;
padding: 20px;
border: 2px solid green;
}
HTML:
<div class="box">This is a box with margin around it.</div>
.box
element has 30px of margin around the border, creating space between this element and other elements.There are specific CSS properties for each section of the box model:
width
and height
: Define the dimensions of the content area.padding
: Defines the space inside the element, between the content and the border.border
: Defines the border around the element.margin
: Defines the space outside the element, between the border and other elements.By default, the width
and height
properties apply only to the content area. The padding and border are added to the overall size of the element.
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 10px solid black;
}
200px
20px
(on both sides), total 40px
added to width.10px
(on both sides), total 20px
added to width.200px (content) + 40px (padding) + 20px (border) = 260px
100px (content) + 40px (padding) + 20px (border) = 160px
.The box-sizing
property changes how the total width and height of an element are calculated. It can take two values:
content-box
(default):
.box {
width: 200px;
padding: 20px;
border: 10px solid black;
box-sizing: content-box; /* Default */
}
200px + 20px + 10px = 260px
border-box
:
.box {
width: 200px;
padding: 20px;
border: 10px solid black;
box-sizing: border-box;
}
200px
(includes content, padding, and border)100px
(includes content, padding, and border).box {
width: 200px;
height: 150px;
padding: 20px;
border: 5px solid blue;
margin: 30px;
background-color: lightgray;
}
<div class="box">Box Model Example</div>
200px
(content)150px
(content)20px
on all sides, expanding the element.5px solid blue
.30px
around the box, creating space between this element and others.The final size of the box (with the default box-sizing: content-box
) will be:
200px + 20px (left padding) + 20px (right padding) + 5px (left border) + 5px (right border) = 250px
150px + 20px (top padding) + 20px (bottom padding) + 5px (top border) + 5px (bottom border) = 200px
The CSS Box Model is essential for controlling the size, spacing, and layout of elements on a webpage. It defines how content, padding, borders, and margins are organized and how the dimensions of elements are calculated. By mastering the box model, you can have precise control over the structure and appearance of your web designs.
next -> Css Layout