box-sizing: border-box
is most useful when you can't specify an exact content width based on the margins, borders and padding of a box, because you don't know in advance (and can't control) what these values are.
In your first example, if you didn't know how much padding was in the box or how thick its borders were, making it use the border-box model allows you to just set width: 50%
to ensure that the box will always take up 50% width of its container, regardless of borders and padding. In the second example, if you had set width: 50%
that's the width of the content; the borders and padding would add to it, causing it to actually expand beyond 50% of the width of its container.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…