I have 2 divs: one in the left side and one in the right side of my page. The one in the left side has fixed width and I want the one of the right side to fill the remaining space.
#search {
width: 160px;
height: 25px;
float: left;
background-color: #ffffff;
}
#navigation {
width: 780px;
float: left;
background-color: #A53030;
}
<div id="search">Text</div>
<div id="navigation">Navigation</div>
I have been working on this problem for two days and have a solution that may work for you and anyone else trying to make a responsive Fixed width left and have the right side fill in the remainder of the screen without wrapping around the left side. The intention I assume is to make the page responsive in browsers as well as mobile devices.
Here is the Code
Here is my fiddle that may just work for you as it did for me. https://jsfiddle.net/Larry_Robertson/62LLjapm/
Since this is a rather popular question, I'm inclined to share a nice solution using BFC.
Codepen sample of the following here.
In this case,
overflow: auto
triggers context behavior and makes the right element expand only to the available remaining width and it will naturally expand to full width if.left
disappears. A highly useful and clean trick for many UI layouts, but perhaps hard to understand the "why it works" at first.@Boushley's answer was the closest, however there is one problem not addressed that has been pointed out. The right div takes the entire width of the browser; the content takes the expected width. To see this problem better:
http://jsfiddle.net/79hpS/
The content is in the correct place (in Firefox), however, the width incorrect. When child elements start inheriting width (e.g. the table with
width: 100%
) they are given a width equal to that of the browser causing them to overflow off the right of the page and create a horizontal scrollbar (in Firefox) or not float and be pushed down (in chrome).You can fix this easily by adding
overflow: hidden
to the right column. This gives you the correct width for both the content and the div. Furthermore, the table will receive the correct width and fill the remaining width available.I tried some of the other solutions above, they didn't work fully with certain edge cases and were just too convoluted to warrant fixing them. This works and it's simple.
If there are any problems or concerns, feel free to raise them.
I wonder that no one used
position: absolute
withposition: relative
So, another solution would be:
HTML
CSS
Jsfiddle example
I ran into this same problem trying to layout some jqueryUI controls. Although the common philosophy now is "use
DIV
instead ofTABLE
", I found in my case using TABLE worked much better. In particular, if you need to have straightforward alignment within the two elements (e.g., vertical centering, horizontal centering, etc.) the options available with TABLE give simple, intuitive controls for this.Here's my solution:
The solution comes from the display property.
Basically you need to make the two divs act like table cells. So instead of using
float:left
, you'll have to usedisplay:table-cell
on both divs, and for the dynamic width div you need to setwidth:auto;
also. The both divs should be placed into a 100% width container with thedisplay:table
property.Here is the css:
And the HTML:
IMPORTANT:For Internet Explorer you need to specify the float property on the dynamic width div, otherwise the space will not be filled.
I hope that this will solve your problem. If you want, you can read the full article I wrote about this on my blog.