Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn’t height:0 hide my padded <div>, even with box-sizing:border-box?

Tags:

css

I’ve got a <div> with padding. I‘ve set it to height: 0, and given it overflow: hidden and box-sizing: border-box.

div {   -webkit-box-sizing: border-box;   -moz-box-sizing: border-box;   box-sizing: border-box;   height: 0;   overflow: hidden;   padding: 40px;   background: red;   color: white; }
<div>Hello!</div>

As I understand, this should make the <div> disappear.

However, it’s still visible (in Chrome 31 and Firefox 25 on my Mac). The height declaration doesn’t appear to be applying to the padding, despite the box-sizing declaration.

Is this expected behaviour? If so, why? The MDN page on box-sizing doesn’t seem to mention this issue.

Nor, as far as I can tell, does the spec — it reads to me like both width and height should include padding when box-sizing: border-box (or indeed padding-box) are set.

like image 451
Paul D. Waite Avatar asked Dec 29 '13 16:12

Paul D. Waite


People also ask

Should I always use box-sizing border-box?

CSS border-box is the most popular choice for setting box-sizing . It guarantees that the content box shrinks to make space for the padding and borders. Therefore, if you set your element width to 200 pixels, border-box makes sure that the content, padding, and borders fit in this number.

How do you include the padding and border in an element total width and height?

With the CSS box-sizing Property 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!

Is padding included in border-box?

border-box tells the browser to account for any border and padding in the values you specify for an element's width and height. If you set an element's width to 100 pixels, that 100 pixels will include any border or padding you added, and the content box will shrink to absorb that extra width.


2 Answers

The exact definition of border-box is:

That is, any padding or border specified on the element is laid out and drawn inside this specified width and height. The content width and height are calculated by subtracting the border and padding widths of the respective sides from the specified ‘width’ and ‘height’ properties.

So you can modify the height and width properties, but padding and border never change.

As the content width and height cannot be negative ([CSS21], section 10.2), this computation is floored at 0.

Then, if height is 0, you can't make the padding be inside, because that implies the height will be negative.

like image 183
DaniP Avatar answered Sep 19 '22 06:09

DaniP


The declaration of height: 0; is applied. If you leave it at height: auto;, you would see a 20px difference (the height of the line with "Hello!"), making it a total 100px high. With height set to zero, it's only 80px high: padding-top + padding-bottom = 80px

So the answer is: Yes, it's expected behavior.

You could set width and height to any value between 0 and 80px (if you have 40px of padding) and still get the same dimensions.

Update: As Hardy mentioned, using an additional wrapper div gets around this issue.

Demo

HTML:

<div class="div-1">     <div class="div-2">         Hello!     </div> </div> 

CSS:

.div-1 {     padding: 40px;          /* This is not visible! */     border: 1px solid tomato; } .div-2 {     -webkit-box-sizing: border-box;     -moz-box-sizing: border-box;     box-sizing: border-box;     height: 0px;     width: 0px;     background: red;     color: white;     overflow: hidden; } 
like image 44
kleinfreund Avatar answered Sep 19 '22 06:09

kleinfreund