Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline-block div being pushed down if a child div is added into it

Tags:

html

css

I have a container here which has three child divs which are inline-block. Now when I add a child like div or paragraph inside child one, the parent div pushes down. I know the solution already. if I use property vertical-align top the problem is fixed. But I am just curious to know what is the reason? jsfiddle here

body{ 
  margin:0
}
#navbar {
  width:100%;
  background-color:black;
  height:50px;
  padding: 0 150px 0 150px;
  position:relitive;
}
#logo {
  height:60px;
  width:90px;
}
#container {
  background-color:#E6E6E6;
  width:78%;
  margin: auto;
  height:1000px;
  text-align:center;
}
#navTable { 
  color:white;
  position:absolute;
  top:10px;
  left:300px;  
  font-size:1.5em;
}
#navTable td {
  padding:0 10px 0 10px;
  border-right:1px solid white;
}
#container>div {
  width:32%;
  height:100%;
  border:1px solid black;
  display:inline-block;

}

.content { 
  background-color:yellow;
  height:300px;
}	
<div id='navbar'>
  <img id='logo' src="http://i.cdn.cnn.com/cnn/.e/img/3.0/global/misc/cnn-logo.png"/>
  <table id='navTable'>
      <tr>   
        <td>Home</td> 
        <td>News</td> 
        <td>Money</td> 
        <td>Entertainment</td> 
        <td>Travel</td>   
      </tr>
  </table>
</div>

<div id='container'>
  <div id='leftDiv'>
    <p>HHHHHH</p>
  </div>

 <div id='middleDiv'></div>
 <div id='rightDiv'></div>
</div>
like image 371
judy Avatar asked Sep 14 '17 04:09

judy


People also ask

How do you prevent inline-block divs from wrapping?

Add white-space: nowrap; to your . layout style declaration. This will do exactly what you need: preventing the divs from wrapping.

Can div be inline-block?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do you fit a div to a child?

It's actually a very simple fix - just make the #content div overflow:auto . By default it won't expand to cover the child divs because they are both floated left, which effectively collapses them. Making the parent overflow:auto forces to expand to cover the width and height of the floated divs too.


1 Answers

The reason is that inline-blocks are vertically aligned along their baseline. If there is text inside an inline-block element, the * last* line of that text will be the baseline. If an inline-block is empty, the baseline is (almost, not completely) at the bottom of the element, (the little offset is the height that would be needed for descenders in letters like g, y, p etc. - which is also the case for the last line of text, BTW).

So the bottom text line in your first inline-block is aligned with the "almost-bottom" of the empty inline-blocks.

To avoid that and align them properly (as you already wrote), you can apply vertical-align: top to all inline-blocks.

like image 81
Johannes Avatar answered Nov 03 '22 01:11

Johannes