Margin
CSS margin is used to create space around elements, outside of any defined borders. It's a crucial property for controlling the layout and spacing between elements on a web page.
Here's an example of using different margin properties:
.margin-example {
ㅤmargin-top: 10px;
ㅤmargin-right: 20px;
ㅤmargin-bottom: 15px;
ㅤmargin-left: 25px;
}
.margin-shorthand {
ㅤmargin: 10px 20px 15px 25px;
}
Margin is part of the CSS box model, which includes:
Margin collapse is a behavior where the vertical margins of two adjacent elements can overlap. For example:
.element1 {
ㅤmargin-bottom: 20px;
}
.element2 {
ㅤmargin-top: 30px;
}
In this case, the margin between the elements will be 30px, not 50px.
Understanding and effectively using CSS margin is essential for creating well-spaced, visually appealing layouts in web design.