1

How to align two div elements side by side while specifying the width of only the right div? I want the right div to be of fixed width and the left div to take the remaining space.

<div style="width:100%;"><div class="left">left div</div>
<div class="right">right div</div></div>
.left{
float:left;
background:green;
width:100%;
}
.right{    
background:red;
width:200px;
}

Thanks.

Vijay
  • 385
  • 1
  • 4
  • 15

4 Answers4

3

float:right; your "right" div, and put it first:

<div style="height:100px; width:100px; background-color:red; float:right;"></div>
<div style="height:100px; background-color:blue;"></div>
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
2

You can use negative margins to negate the width of the fixed with element, then float the fixed width element to the right.

.left{
    float: left;
    background: green;
    width: 100%;
    margin-right: -200px;
}
.right{    
    background: red;
    width: 200px;
    float: right;
}
<div style="width:100%;">
    <div class="left">left div</div>
    <div class="right">right div</div>
</div>
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
1

You can use CSS calc to do this with pretty good browser support(http://caniuse.com/#feat=calc):

.left{
    float:left;
    background:green;
  
    width: calc(100% - 200px);
}
.right{    
    background:red;
    width:200px;
  
    float: right;
}
<div style="width:100%;"><div class="left">left div</div>
<div class="right">right div</div></div>
Turnip
  • 35,836
  • 15
  • 89
  • 111
0

To align to Divs next to eachother you give them both display: inline or display: inline-block or float: left but you cannot align anything next to a div with width: 100%. Because that basically means you want that Div to take up the entire row. so nothing can fix next to it. By changing the width to be something other than 100% and giving both divs float: left you will accomplish what you want like this http://jsfiddle.net/6707pn01/

Alizoh
  • 1,562
  • 1
  • 13
  • 16