Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Floating two elements side by side

Tags:

css

css-float

I have two divs side by side. I want div that is on left hand side to take up as much room as it needs without pushing the other div (on right) to next line.

Here is what I have right now: http://jsfiddle.net/RALza/

HTML

<div id="container">
    <div id="divA"> left text </div>
    <div id="divB">  right text </div>
</div>

CSS

#divA
{
  float:left;
  border:1px solid blue;
  width:100%;
}

#divB
{
  float:right;
  border:1px solid red;
}
like image 339
dev.e.loper Avatar asked Aug 23 '11 22:08

dev.e.loper


3 Answers

<div id="container">
    <div id="divB">  right text </div>
    <div id="divA"> left text </div>
</div>

#divA
{
  overflow:auto;
  border:1px solid blue;
}

#divB
{
  float:right;
  border:1px solid red;
}

will work.

But you should specify width of floating elements.

like image 185
MatTheCat Avatar answered Oct 20 '22 18:10

MatTheCat


You can use CSS flexible boxes:

#container {
  display: flex;
  justify-content: space-between;
}
<div id="container">
    <div id="divA">left text</div>
    <div id="divB">right text</div>
</div>
like image 37
Aleksandr Avatar answered Oct 20 '22 18:10

Aleksandr


Try this fiddle: http://jsfiddle.net/RALza/6/

It works by changing the order of the two divs and making the first div a normal block element without a float.

<div id="container">
    <div id="divB">  right text </div>
    <div id="divA"> left text </div>
</div>

and

#divA
{
  border:1px solid blue;
}

#divB
{
  float:right;
  border:1px solid red;
}
like image 39
Alp Avatar answered Oct 20 '22 18:10

Alp