1

HTML

<div class="menu">
    <p>normal</p>
</div>
<div class="menu">
    <p>normal</p>
</div>
<div class="menu">
    <p>normal</p>
</div>
<div class="menu">
    <p>Why does this div stays at the top</p>
</div>

CSS

.menu{
    width:120px;
    background-color:red;
    display:inline-block;
    height:400px;
}

http://jsfiddle.net/jezVt/

I have four divs aligned next to each other using inline-block. When I enter text inside the div using p tag, the div with two lines stays at the top while the other three divs(has just one line text) are aligned properly.

Help please..

1
  • 1
    add float:left; in .menu Commented Jun 26, 2014 at 9:00

6 Answers 6

2

add you code vertical-align:top; demo

Sign up to request clarification or add additional context in comments.

Comments

1

The reason is that just like every inline element, your .menu elements have the default value baseline for their vertical-align property. This means that when the browser calculates layout for the .menu elements that appear side-by-side, each element is positioned so that the baseline of their contents is vertically aligned with that of the others.

In this specific case, this means that the baseline of last line of text in each .menu is aligned with that of the others. You will notice that by adding more text and making it to occupy three or more lines, the element that contains this text is going to be "pulled upwards" more and more in relation to the others.

Finally, as everyone has said using vertical-align: top lets the browser know that you want the divs to be aligned with respect to the top of their content, which produces the desired result.

Comments

1
write vertical-align:top; in css:http://jsfiddle.net/aashi/jezVt/1/

Comments

1

From my first look, is that you have to much text in the fourth column.

But use "vertical-align: top;" as the two previous answers.

Comments

0

Why do you want to make divs as inline-block. When div is a block level element you can use that property itself.

[http://jsfiddle.net/jezVt/12/][1] 

Comments

0

Alternatively you can try:

.menu{
    width:120px;
    background-color:red;
    display:inline-block;
    height:400px;
    float:left;
    margin: 2px;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.