Display Two
s Side-by-Side

Go To StackoverFlow.com

29

My goal is to display two <div>s side-by-side with the content from both <div>s aligned at the top. I do not want long text in the second <div> to wrap underneath the first one.

Finally, I do not want to set the width of the second <div> because I need the markup to work in different widths.

Sample markup is below and at http://jsfiddle.net/rhEyM/.

CSS

.left-div {
    float: left;
    width: 100px;
    height: 20px;
    margin-right: 8px;
    background-color: linen;
}
.right-div {
    float: left;
    margin-left: 108px;
    background-color: skyblue;
}​

HTML

<div class="left-div">
    &nbsp;
</div>
<div class="right-div">
    My requirements are <b>[A]</b> Content in the two divs should line
    up at the top, <b>[B]</b> Long text in right-div should not wrap
    underneath left-div, and <b>[C]</b> I do not want to specify a
    width of right-div. I don't want to set the width of right-div
    because this markup needs to work within different widths.
</div>

2012-04-04 06:47
by Jonathan Wood
i have updated your Fiddle : http://jsfiddle.net/rhEyM/10 - Jignesh Rajput 2012-04-04 06:56


19

I removed the float from the second div to make it work.

http://jsfiddle.net/rhEyM/2/

2012-04-04 06:50
by Ray Toal
D'oh! You're exactly right. In my actual project, I wasn't setting the margin-left of the right div. That's all that's needed. Thanks - Jonathan Wood 2012-04-04 07:02


26

Try to Use Flex as that is the new standard of html5.

http://jsfiddle.net/maxspan/1b431hxm/

<div id="row1">
    <div id="column1">I am column one</div>
    <div id="column2">I am column two</div>
</div>

#row1{
    display:flex;
    flex-direction:row;
justify-content: space-around;
}

#column1{
    display:flex;
    flex-direction:column;

}


#column2{
    display:flex;
    flex-direction:column;
}
2014-09-18 00:43
by maxspan
I think this is the better solution, even if I know it does not work everytime (older Browsers and CSS3). But it gives you more flexibility concerning the height of the divs - r00tandy 2015-03-25 22:12


9

Try this : (http://jsfiddle.net/TpqVx/)

.left-div {
    float: left;
    width: 100px;
    /*height: 20px;*/
    margin-right: 8px;
    background-color: linen;
}
.right-div {

    margin-left: 108px;
    background-color: lime;
}​​

<div class="left-div">
    &nbsp;
</div>
<div class="right-div">
    My requirements are <b>[A]</b> Content in the two divs should line up at the top, <b>[B]</b> Long text in right-div should not wrap underneath left-div, and <b>[C]</b> I do not want to specify a width of right-div. I don't want to set the width of right-div because this markup needs to work within different widths.
</div>
<div style='clear:both;'>&nbsp;</div>

Hints :

  • Just use float:left in your left-most div only.
  • No real reason to use height, but anyway...
  • Good practice to use <div 'clear:both'>&nbsp;</div> after your last div.
2012-04-04 06:50
by Dr.Kameleon
Any reason why 'clear:both' has to be used after last div? (Why is this a good practice? - Raj Pawan Gumdal 2017-02-02 12:48
Ads