Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop div from shifting down when text is added

Tags:

html

css

I am trying to place some text inside a square div. The only problem is that it shifts the div that the text is in down a lot. Does anyone know how I can get the div back to its original position?

Here is the html:

<div id="squarecontainer">
    <div id="square1">
        <p>Easy to Manage</p>
    </div>
    <div id="square2">

    </div>
    <div id="square3">

    </div>
</div>

Here is the css:

#squarecontainer{
    text-align: center;
}

#square1{
    display: inline-block;
    position: relative;
    margin-right: 100px;
    height: 310px;
    width: 310px;
    margin-top: 72px;
    background-color: #fff;
}

#square2{
    display: inline-block;
    position: relative;
    height: 310px;
    width: 310px;
    margin-top: 72px;
    background-color: #fff;
}

#square3{
    display: inline-block;
    position: relative;
    margin-left: 100px;
    height: 310px;
    width: 310px;
    margin-top: 72px;
    background-color: #fff;
}

#square1 is the div that shifts really far down when I added the "Easy to Manage" text.

Fiddle link demonstrating the problem: http://jsfiddle.net/jhunlio/RgywE/

like image 246
Zach Starnes Avatar asked Mar 05 '13 06:03

Zach Starnes


People also ask

How do I stop divs from moving?

Basically, by giving an element absolute positioning, it is no longer being taken into account when positioning the rest of the page. It will take up its alloted space in its set position no matter what.

How do I keep my div from wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

Can I put text directly in a div?

Yes, you can directly add content text into a div tag, although using p tags would be preferable in most circumstances.


1 Answers

I found the solution. By adding vertical-align: top; it moved the div back. don't know why it worked, but it did.

like image 54
Zach Starnes Avatar answered Sep 19 '22 13:09

Zach Starnes