What does box-sizing: border-box actually do?
Tags: webdev, css, tutorial, frontend
Published: 2024-07-12 02:31:32

When I first started learning CSS, I saw box-sizing: border-box in almost every CSS file I encountered. Like many beginners, I copied it without understanding its purpose. If this sounds familiar, don't worry—you’re not alone.

**What is box-sizing?**
The _box-sizing_ property in CSS controls how the width and height of an element are calculated. There are three main values:

1. **content-box** (default): The width and height apply only to the content, not the padding or border. This can lead to unexpected sizes if you add padding or borders later. 
2. **border-box**: The width and height include padding and border, making the total size of the element more predictable.
3. **inherit**: The element inherits the box-sizing value from its parent.

**Why box-sizing: border-box?**
Here’s why box-sizing: border-box is so helpful:

- The width and height include padding and border. If you set an element's width to 200px, it will always be 200px, regardless of padding or border.
- No need to calculate the padding and borders to know the element’s total size. This makes designing and adjusting layouts much easier.
- Using border-box across all elements ensures a consistent design, making your CSS cleaner and more maintainable.

Here’s a simple example to illustrate the difference between **"content-box"**and **"border-box"**:

**Content box**
```
<div class="content-box">
  <p>content-box</p>
</div>
```
```
.content-box {
  background-color: red;
  width: 200px;
  padding: 20px;
  border: 10px solid black;
  text-align: center;
  color: white;
}
```

![example of content box](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hlkywdd5kxyazvoghow0.png)

As shown in the screenshot, `the box with box-sizing: content-box` has a total width of 260px. Here's why:

200px is the width set for the content area, 20px on each side adds up to 40px in total (20px + 20px), 10px on each side adds up to 20px in total (10px + 10px).

Total Width: 200px (content) + 40px (padding) + 20px (border) = 260px

**Why?** With _content-box_, the width you set is only for the content inside the box. Padding and border are added to this width, increasing the total size of the box.

**Border box**

```
<div class=" border-box">
  <p>border-box</p>
</div>
```
```
.border-box {
  box-sizing: border-box;
  background-color: green;
  width: 200px;
  padding: 20px;
  border: 10px solid black;
  text-align: center;
  color: white;
}
```


![an example of border box](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l37fbme9fzlunne2irob.png)

In contrast, the box with `box-sizing: border-box` has a total width of 200px. Here’s why:

200px is the width set for the entire box, including content, padding, and border, 20px on each side (included within the 200px width), 10px on each side (included within the 200px width).

Total Width: The width of 200px encompasses the content, padding, and border altogether. No additional space is added outside this width.

**Why?** With _border-box_, the width you set covers everything within the box, so the total size remains as specified, without extra padding or border extending beyond the given width.

Understanding and using box-sizing: border-box can simplify your CSS and make your layouts more predictable and easier to manage. If you had no idea about it I hope this explanation clears things up. 
You can view and experiment with the code used in this example on [CodePen](https://codepen.io/Bridgetamana/pen/JjQdddZ).
If you enjoyed this post, connect with me on [LinkedIn](https://www.linkedin.com/in/bridget-amana/), and [Twitter](https://twitter.com/amana_bridget)!