As already mentioned, this has nothing to do with box-sizing: border-box . Your understanding is correct in that the calculations according to the size of the scale work only on boxes with given heights, which is not the case here.
This is because clearfix in the second field (with extra height) is trying to clear the float that is next to it. This will cause clearfix to be pressed all the way so that it is flush with the bottom edge of the field. Section 9.5.2 of the specification describes in detail the cleaning behavior, which takes into account a number of cases, but this boils down to moving the clearfix down, since it is necessary to βplace the boundary edge of the [cleaning element] even with the lower outer edge of the lowest float to be cleaned "
The extra height is just the bottom padding of the non-floating box - clearfix cannot bleed into the fill area, so the field needs to grow its content area to accommodate the new clearfix position.
If you added some content to clearfixes, you can see exactly where each is executed :
div[style]::before { content: 'clearfix'; }
If you specify a height for the fields so clearfix overflows the field as a result of trying to clear the floating field (and this happens regardless of whether it is box-sizing content-box or border-box , although this affects the calculation of the height itself):
.box { height: 100px; width: 50%; border: 1px solid green; padding: 10px; }
The reason why removing clearfix solves the problem is because without any gap in the second field there is nothing to move, and therefore its height does not increase.
The reason that floating both fields or setting overflow: hidden solves the problem is because each of these things causes windows to set their own block formatting contexts . One of the defining features of BFC is that those floating outside it can never interact with any elements within it. This includes all and all clearfix fields in the BFC.
Without setting the overflow property or floating second field,
- floating box (
#incorrect .box:nth-of-type(1) ), - non-floating block (
#incorrect .box:nth-of-type(2) ) - and its clearfix (
#incorrect .box:nth-of-type(2) div[style] )
all participate in the same block formatting context. For this reason, clearfix in a non-floating box is trying to clear the floating block. (Note once again that clearfix in a floating block is involved in the BFC, which is set in the floating point field, and not in which the three above-mentioned elements, which set the initial containing block , are involved.)