The CSS Box Model consists of the following components:
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid blue;
margin: 30px;
background-color: lightyellow;
}
The box-sizing
property defines how width and height are calculated.
.box {
width: 200px;
height: 100px;
padding: 20px;
border: 5px solid blue;
margin: 30px;
box-sizing: border-box;
}
The display
property determines how an element is displayed in the layout.
.block-element {
display: block;
width: 200px;
background-color: lightblue;
}
.inline-element {
display: inline;
background-color: lightcoral;
}
.inline-block-element {
display: inline-block;
width: 100px;
height: 50px;
margin: 10px;
background-color: lightgreen;
}
The position
property controls the positioning of elements.
.relative {
position: relative;
top: 20px;
left: 30px;
background-color: lightblue;
}
.absolute {
position: absolute;
top: 50px;
left: 100px;
background-color: lightcoral;
}
.fixed {
position: fixed;
top: 10px;
right: 10px;
background-color: lightgreen;
}
.sticky {
position: sticky;
top: 0;
background-color: lightyellow;
}
The z-index
property controls the stacking order of elements when they overlap.
.element1 {
position: relative;
z-index: 1;
background-color: lightblue;
width: 200px;
height: 100px;
}
.element2 {
position: absolute;
z-index: 2;
background-color: lightcoral;
width: 200px;
height: 100px;
top: 20px;
left: 20px;
}
.element3 {
position: absolute;
z-index: 3;
background-color: lightgreen;
width: 200px;
height: 100px;
top: 40px;
left: 40px;
}