Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overflow:hidden on inline-block adds height to parent

I'm certain this has been asked before in some form or other, but I just can't find an answer..

I have some nested divs

<div class="parent">     <div class="child">A</div> </div> 

And the child has display:inline-block and overflow:hidden

.parent{     background-color:red; } .child{      background-color:green;      display:inline-block;     overflow:hidden;  } 

And it gets rendered like this: enter image description here

You can notice that the parent is 5px higher than the child.

Where does the extra height come from?

Here is the sample: http://jsfiddle.net/w8dfU/

Edit: I don't want to remove display:inline-block or overflow:hidden, this is a simplified example to illustrate the problem, but in my real layout I need them both. I just want to understand why this extra height appears. Is it specified somewhere that it should be like this? Is it a consequence of some other css feature?

like image 613
stralsi Avatar asked Dec 01 '13 09:12

stralsi


People also ask

What does overflow hidden does?

overflow: hidden With the hidden value, the overflow is clipped, and the rest of the content is hidden: You can use the overflow property when you want to have better control of the layout. The overflow property specifies what happens if content overflows an element's box.

What is the purpose of the hidden value in the overflow property?

The hidden value of the overflow property hides all the content that is exceeding the box area.

How do I hide content of overflow?

Set the div with a width or height, (otherwise it won't know whether something is overflowing). Then, add the overflow:hidden; CSS property-value pair.

What is overflow inherit?

The overflow property specifies what should happen if content overflows an element's box. This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area. Note: The overflow property only works for block elements with a specified height.


1 Answers

I had this issue when building a horizontal slider. I fixed it with vertical-align:top on my inline-block elements.

ul {     overflow-x: scroll;     overflow-y:hidden;     white-space: nowrap;     -webkit-overflow-scrolling: touch; }  ul&::-webkit-scrollbar {     display: none; }  li {     display: inline-block;     vertical-align: top;     width: 75px;     padding-right: 20px;     margin:20px 0 0 0; } 
like image 66
user3257701 Avatar answered Sep 25 '22 20:09

user3257701