Make a div fill up the remaining width
Try out something like this:
<style>
#divMain { width: 500px; }
#left-div { width: 100px; float: left; background-color: #fcc; }
#middle-div { margin-left: 100px; margin-right: 100px; background-color: #cfc; }
#right-div { width: 100px; float: right; background-color: #ccf; }
</style>
<div id="divMain">
<div id="left-div">
left div
</div>
<div id="right-div">
right div
</div>
<div id="middle-div">
middle div<br />bit taller
</div>
</div>
divs will naturally take up 100% width of their container, there is no need to explicitly set this width. By adding a left/right margin the same as the two side divs, it's own contents is forced to sit between them.
Note that the "middle div" goes after the "right div" in the HTML
Up-to-date solution (October 2014) : ready for fluid layouts
Introduction:
This solution is even simpler than the one provided by Leigh
. It is actually based on it.
Here you can notice that the middle element (in our case, with "content__middle"
class) does not have any dimensional property specified - no width, nor padding, nor margin related property at all - but only an overflow: auto;
(see note 1).
The great advantage is that now you can specify a max-width
and a min-width
to your left & right elements. Which is fantastic for fluid layouts.. hence responsive layout :-)
note 1: versus Leigh's answer where you need to add the margin-left
& margin-right
properties to the "content__middle"
class.
Code with non-fluid layout:
Here the left & right elements (with classes "content__left"
and "content__right"
) have a fixed width (in pixels): hence called non-fluid layout.
Live Demo on http://jsbin.com/qukocefudusu/1/edit?html,css,output
<style>
/*
* [1] & [3] "floats" makes the 2 divs align themselves respectively right & left
* [2] "overflow: auto;" makes this div take the remaining width
*/
.content {
width: 100%;
}
.content__left {
width: 100px;
float: left; /* [1] */
background-color: #fcc;
}
.content__middle {
background-color: #cfc;
overflow: auto; /* [2] */
}
.content__right {
width: 100px;
float: right; /* [3] */
background-color: #ccf;
}
</style>
<div class="content">
<div class="content__left">
left div<br/>left div<br/>left div<br/>left div<br/>left div<br/>left div<br/>
</div>
<div class="content__right">
right div<br/>right div<br/>right div<br/>right div<br/>
</div>
<div class="content__middle">
middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br />bit taller
</div>
</div>
Code with fluid layout:
Here the left & right elements (with classes "content__left"
and "content__right"
) have a variable width (in percentages) but also a minimum and maximum width: hence called fluid layout.
Live Demo in a fluid layout with the max-width
properties http://jsbin.com/runahoremuwu/1/edit?html,css,output
<style>
/*
* [1] & [3] "floats" makes the 2 divs align themselves respectively right & left
* [2] "overflow: auto;" makes this div take the remaining width
*/
.content {
width: 100%;
}
.content__left {
width: 20%;
max-width: 170px;
min-width: 40px;
float: left; /* [1] */
background-color: #fcc;
}
.content__middle {
background-color: #cfc;
overflow: auto; /* [2] */
}
.content__right {
width: 20%;
max-width: 250px;
min-width: 80px;
float: right; /* [3] */
background-color: #ccf;
}
</style>
<div class="content">
<div class="content__left">
max-width of 170px & min-width of 40px<br />left div<br/>left div<br/>left div<br/>left div<br/>left div<br/>left div<br/>
</div>
<div class="content__right">
max-width of 250px & min-width of 80px<br />right div<br/>right div<br/>right div<br/>right div<br/>
</div>
<div class="content__middle">
middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br/>middle div<br />bit taller
</div>
</div>
Browser Support
Tested on BrowserStack.com on the following web browsers:
- IE7 to IE11
- Ff 20, Ff 28
- Safari 4.0 (windows XP), Safari 5.1 (windows XP)
- Chrome 20, Chrome 25, Chrome 30, Chrome 33,
- Opera 20