In this example...
<body> <div> <div>foo bar</div> </div> </body>
body, html, div { height: 100%; margin: 0; } div div { display: inline-block; overflow: hidden; }
Why does overflow: hidden
cause a vertical scrollbar? Additionally, why is this height not attributed to anything on the page? It's like an invisible margin.
The 100% height of all the elements is intentional. In theory, that should cause the inner-most div to expand to meet the viewport. And it does! ...so long as overflow: hidden
is not there, Why?
hidden - The overflow is clipped, and the rest of the content will be invisible. scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content. auto - Similar to scroll , but it adds scrollbars only when necessary.
The hidden value of the overflow property hides all the content that is exceeding the box area. This property should be handled with care because the content that this property hide is completely invisible to the user, however, it is best suited for displaying content that is dynamic in nature.
It's because <body> has a default of 8px margin so when you hide the <body> the margin goes away.
Inline element properties: The height of an inline element is the height of the content. The width of an inline element is the width of the content. The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.
The extra height is the same height as the height difference between vertical-align: baseline
, and vertical-align: bottom
. The "descender line". That's where the seemingly random "5 extra pixels" comes from. If the font size is 10 times as large, this gap will also be 10 times as large.
Also, it seems that when overflow: hidden
is not there, the inline-block
element has its baseline as the same baseline of its last line of text.
This leads me to believe that overflow: hidden
forces the baseline of the entire inline-block
element to be at the bottom of the element. Even though there is no text there, the parent of the inline-block
element reserves space for the descender line. In the example given in the question, it cannot be easily seen since the parent of the inline-block
element has height: 100%
. So, instead, that extra space reserved for the descender line overflows out of that parent div.
Why is this space still there, even though there's no text? I think that's because the inline-block
creates an inline formatting context, which is what causes this space. Were this element to be a block
, it would only create this inline formatting context once it encounters an inline element or text.
This is just a theory, but it seems to explain it. It also explains why @Jonny Synthetic's answer works: adding overflow: hidden to the parent hides that extra descender line.
Thanks to @Hashem Qolami for the jsbins that gave me this theory.
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