← Back tothis vs that

border-box vs content-box

Written byPhuoc Nguyen
Category
CSS
Created
03 Sep, 2020
`border-box` and `content-box` are two values of the `box-sizing` property. Unlike the `content-box`, the `border-box` value indicates that the dimension of an element will also include the border and padding.
Let's assume that we have a div element whose size is `200px x 100px`, the border and padding are `5px` and `10px` respectively.
.div {
width: 200px;
border: 5px;
padding: 10px;
}
In the content box model, the content inside of element will have the same dimension as the element.
.div {
/* ... */
box-sizing: content-box;
}
// Content box
◀︎──── 200px ────▶︎

┌───────────────────────────────────────────────────────┐
| |
| ┌───────────────────────────────────────┐ |
| | | |
| | ┌───────────────┐ | |
| Border| Padding | Content | | |
| | | | | |
| 5px | 10px | 200px | 10px | 5px |
|◀︎─────▶︎|◀︎─────────▶︎|◀︎─────────────▶︎|◀︎─────────▶︎|◀︎─────▶︎|
| | | | | |
| | └───────────────┘ | |
| | | |
| └───────────────────────────────────────┘ |
| |
└───────────────────────────────────────────────────────┘
In the border box model, the content's dimension has to subtract the border and padding from the element's dimension. Specifically, the content's width is `200 - 5 * 2 - 10 * 2 = 170px`.
// Border box
◀︎──────────────────────── 200px ────────────────────────▶︎

┌───────────────────────────────────────────────────────┐
| |
| ┌───────────────────────────────────────┐ |
| | | |
| | ┌───────────────┐ | |
| Border| Padding | Content | | |
| | | | | |
| 5px | 10px | 170px | 10px | 5px |
|◀︎─────▶︎|◀︎─────────▶︎|◀︎─────────────▶︎|◀︎─────────▶︎|◀︎─────▶︎|
| | | | | |
| | └───────────────┘ | |
| | | |
| └───────────────────────────────────────┘ |
| |
└───────────────────────────────────────────────────────┘

See also

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn