Briefly
With the box
property, you can change how the browser calculates the size of an element.
By default, the size of an element is considered to be the size of the content area. If you specify width
and height
along with padding
and border
, the browser will calculate the size of the element as width + padding * 2 + border * 2
and height + padding * 2 + border * 2
.
🤖 If you set the value border
for the box
property, the browser will change the calculation principle and padding
and border
will already be included in width
and height
.
You can read more about the specifics of size calculation in the article “Box Model”.
Example
Let's create two elements and apply identical styles to both:
<div class="element first"></div><div class="element second"></div>
<div class="element first"></div> <div class="element second"></div>
.element { display: inline-block; width: 100px; height: 100px; padding: 25px; border: 10px solid #ffffff;}
.element { display: inline-block; width: 100px; height: 100px; padding: 25px; border: 10px solid #ffffff; }
They will only differ in background and the value of the box
property.
.first { box-sizing: content-box; background-color: #2E9AFF;}.second { box-sizing: border-box; background-color: #F498AD;}
.first { box-sizing: content-box; background-color: #2E9AFF; } .second { box-sizing: border-box; background-color: #F498AD; }
As a result, the elements ended up being of different sizes! How so? We specified the same width, height, and padding, as well as border 🤔
How to understand
The reason is that with the value of the box
property equal to content
(the default value), the width of the element is calculated as follows:
Width of the content area (100) + padding left and right (25 + 25) + width of the right and left borders (10 + 10). Total: 170 pixels.
Similarly for height.
So the first element has dimensions of 170 x 170.
The size of the second element is calculated differently. Due to the value of the box
property, the browser perceives width
and height
as the final sizes of the element. As a result, the specified width of 100 pixels already includes both the inner side paddings and the side borders. The dimensions of the second element will be 100 x 100.
How it is written
As a value for the box
property, it is not possible to use anything other than keywords. The value can only be one.
Available values:
content
— default value. Final width = width + right padding + left padding + right border + left border- box border
— values of- box width
andheight
are the final sizes of the element. Final width = width
Tips
💡 The property cannot be animated using transition
😔
💡 The property is not inherited.
💡 With any value of the property, the outer margins (margin
) are not taken into account in the calculations.
💡 Default value: content
.
In practice
Advice 1
🛠 Due to the standard mechanism for calculating the size of an element, many beginner developers get sizes different from what they expected. There are two solutions:
- Adjust the element's size to the desired one, calculating by yourself what the internal paddings and border should be so that the required size is achieved.
- At the very beginning of work, “reset” the standard value by replacing it with a predictable
box
.- sizing
For the “reset,” you can use the universal selector. Write at the very beginning of the stylesheet:
* { box-sizing: border-box;}
* { box-sizing: border-box; }
Now all element sizes will be equal to those values set in the width
and height
properties. Thus, we will win 🙌🏻