0% completed
The box-sizing property controls how the total width and height of an element are calculated. There are two main values for box-sizing:
Using box-sizing properly helps you control your layout and avoid unexpected element sizes.
Follow the code block below to set box-sizing in CSS:
/* Default box model, content-box (padding and border are extra) */ box-sizing: content-box; /* Alternative box model, border-box (padding and border are included) */ box-sizing: border-box;
Explanation:
In this example, we set a <div> with box-sizing: content-box. We define the element's width as 200px, add a padding of 20px, and a border of 5px. Let's calculate the total width mathematically:
Explanation:
In this example, we use box-sizing: border-box with the same dimensions. The width is still declared as 200px, but now padding and border are included in that value.
To calculate the available content width:
Explanation:
This lesson shows how the box-sizing property affects the calculation of an element's size. Using content-box adds padding and borders outside the defined width, while border-box incorporates them, making layout calculations more predictable and easier to manage.
.....
.....
.....