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>
Add white-space: nowrap; to your . layout style declaration. This will do exactly what you need: preventing the divs from wrapping.
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.
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.
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.
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