Why isn't max-height
applied on the box in this example ? It seems like that the border-box mode is ignored (tested on Chrome), but it seems counterintuitive.
The width and height properties include the content, padding, and border, but do not include the margin.
The box-sizing property allows us to include the padding and border in an element's total width and height. If you set box-sizing: border-box; on an element, padding and border are included in the width and height: Both divs are the same size now! Hooray!
The W3C Box Model does not include padding and border in its total width or height. The IE Box Model does include padding and border in its total width or height. Neither Box model includes margin in their total width or height. We can use the box-sizing property to set all browsers to use one or the other box model.
As with height , the maximum height is based on the content area only — any vertical padding , border , or margin will be in addition.
The box-sizing
property isn't ignored, this is exactly how the border-box
value should behave:
Your padding is set to 100px on top and bottom, therefore 200px of your element's height
is consumed by the padding.
If you specify a height
of 200px, the computed height will be 0 because 200 - 200 is 0.
If you specify a height
of 201px, the computed height will be 1, etc.
From the box-sizing
documentation:
The content width and height are calculated by subtracting the padding widths of the respective sides from the specified ‘width’ and ‘height’ properties. As the content width and height cannot be negative, this computation is floored at 0.
This is easily demonstrated using borders instead of padding:
#test {
background: #000;
border-width: 100px 0;
border-style: solid;
border-color: red;
height: 200px;
box-sizing: border-box;
}
Here our element has a black background and a red border, however as its height is equal to the sum of the top and bottom border widths, the element ends up with 0px computed:
As you can see, the box is entirely red. The element has no height so there is no black background to be seen. If we adjust the element's height
to 250px
, we end up with:
The element's computed height here is 50px, so we see 50px of the background. The remaining 200px is consumed by the border.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With