
Inheriting box-sizing Probably Slightly Better Best-Practice
Inheriting box-sizing Probably Slightly Better Best-Practice êŽë š
Iâm a big fan of resetting box-sizing to border-box, so much that we have a special day of the year around here. But there is a little adjustment to setting it that seems like a pretty good idea.
Hereâs the adjusted version:
html {
box-sizing: border-box;
}
*, *:before, *:after {
box-sizing: inherit;
}
Credit on the inheritence idea to Jon Neal here, who says:
::: Jon Neal (blog.teamtreehouse.com
)
This will give you the same result, and make it easier to change the box-sizing in plugins or other components that leverage other behavior.

:::
Explaining further, letâs say you have a component that was just designed to work with the default content-box
box-sizing
. You just wanted to use it and not mess with it.
.component {
/* designed to work in default box-sizing */
/* in your page, you could reset it to normal */
box-sizing: content-box;
}
The trouble is, this doesnât actually reset the entire component. Perhaps there is a <header>
inside that component that expects to be in a content-box
world. If this selector is in your CSS, in âthe old wayâ of doing a box-sizing
resetâŠ
/* This selector is in most "old way" box-sizing resets */
* {
box-sizing: border-box;
}
Then that header isnât content-box
as you might expect, itâs border-box
. Like:
<div class="component"> <!-- I'm content-box -->
<header> <!-- I'm border-box still -->
</header>
</div>
In order to make that reset easier and more intuitive, you can use the inheriting snippet up at the top there, and the inheriting will be preserved.
It works:
This isnât a majorly huge thing. You might already be using the box-sizing
reset the âold wayâ and never have gotten bit by it. Thatâs the case for me. But as long as weâre promoting a âbest practiceâ style snippet, we might as well hone it to be the best it can be.