CSS lays out every element as a box made of four nested areas, each bounded by an edge:
- Content: text, child boxes, or an image. Its edge is what
widthandheightsize by default. - Padding: space between content and border. It shows the background.
- Border: drawn around the padding. The background paints under it too, by default.
- Margin: transparent space outside the border, separating the box from its neighbours. It can be negative, and vertical margins between blocks can collapse.
box-sizing decides which edge the sizing properties measure:
content-box(the initial value):width: 200pxis the content. Add20pxpadding and a5pxborder and the box takes250px.border-box:width: 200pxis the border box. Padding and border fit inside, and the content gets what's left:150px.
It changes the meaning of width, height, min-*, max-* and flex-basis, never the margin. It doesn't change auto widths either: a block with width: auto fills its container the same way under both values.
Teams set border-box globally because it matches how designs are specified ("this card is 300px wide") and because width: 100% plus padding stops overflowing the parent. Percentages are the main reason: with content-box, width: 50% and padding: 20px can't be combined without calc().
Two details interviewers like. The content box can't go negative: with border-box, if padding and border exceed the width, the content floors at 0 and the box grows. And getComputedStyle() reports width for the box box-sizing names, so under border-box it includes the padding and border.