107

How can I make a div fill up the remaining width?

<div id="Main" style="width: 500px;">
    <div id="div1" style="width: 100px;"></div>
    <div id="div2"></div>
    <div id="div3" style="width: 100px; float: right;"></div>
</div>

How can I get div2 to fill up the remainder?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
raklos
  • 28,027
  • 60
  • 183
  • 301
  • I needed the opposite, a fixed width centre div and fluid outside divs. I have added an answer at the bottom if anyone else needs it. – damndaewoo Jul 31 '15 at 01:17

7 Answers7

67

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

Adriano
  • 19,463
  • 19
  • 103
  • 140
Leigh
  • 12,859
  • 3
  • 39
  • 60
  • 1
    I think I found an even better solution based on your (great) solution :) see below in the answer I provide – Adriano Mar 28 '14 at 17:31
  • is there a way to do this without changing the order of the items? i.e. left-div, middle-div, right-div. This is important if you want to do different things on different screen sizes. – Eliezer Steinbock Mar 30 '16 at 22:15
  • Yeah this order of left-right-mid is really the trick. It disallows the right div not go under the two divs, right? – Ansjovis86 Aug 02 '16 at 12:54
57

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
Adriano
  • 19,463
  • 19
  • 103
  • 140
  • 3
    I would recommend all developers to utilise this method, especially as Responsive Web Design has become a necessity. :) – aullah Jan 18 '15 at 00:13
  • 1
    Careful with this one. In some cases the scrollbar will appear in the content__middle div on Firefox if heights aren't accurate because of overflow: auto; – Jason Feb 27 '15 at 17:48
  • Hi Jason, that's very interesting, thx for pointing that out. Can u provide a jsfiddle to show a live example when this happens? – Adriano Feb 27 '15 at 17:53
  • is this because `overflow` creates a block formatting context? would `display: flex` have the same effect? – Jayen May 20 '15 at 00:55
  • @Jayen I'm not sure actually. For your second question, there is only one way to find out ;) – Adriano May 20 '15 at 06:49
  • 1
    Yup it did. https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_formatting_context was very helpful. ended up using `overflow-y: hidden` as it a) allowed width to be set, b) has the best browser support and c) wasn't a table – Jayen May 21 '15 at 05:27
46

Flex-boxes are the solution - and they're fantastic. I've been wanting something like this out of css for a decade. All you need is to add display: flex to your style for "Main" and flex-grow: 100 (where 100 is arbitrary - its not important that it be exactly 100). Try adding this style (colors added to make the effect visible):

<style>
    #Main {
        background-color: lightgray;
        display: flex;
    }

    #div1 {
        border: 1px solid green;   
        height: 50px; 
        display: inline-flex; 
    }
    #div2 {
        border: 1px solid blue;    
        height: 50px;
        display: inline-flex;
        flex-grow: 100;
    }
    #div3 {
        border: 1px solid orange;        
        height: 50px;
        display: inline-flex;
    }
</style>

More info about flex boxes here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

dyer
  • 284
  • 1
  • 12
B T
  • 57,525
  • 34
  • 189
  • 207
  • Flexbox is great but it only takes up as much space as it needs instead of all the space that remains. Thus I went with the overflow:auto solution. – ThinkBonobo May 25 '16 at 17:06
  • 1
    @ThinkBonobo what do you mean by "it"? A flex box itself will act like a 'block', and items inside a flex box can definitely take up more space than it requires. – B T May 25 '16 at 22:12
  • @BT it meaning the main central div. It's definitely a solution the works for many cases but not for my particular use case. – ThinkBonobo May 27 '16 at 13:12
  • 1
    @ThinkBonobo Bet I could make it work for you if you posted a jsFiddle – B T May 27 '16 at 23:08
34

Use the CSS Flexbox flex-grow property to achieve this.

.main {
  display: flex;
}
.col-1, .col-3 {
  flex: 0 0 100px;
}
.col-2 {
  flex-grow: 1;
}
<div class="main">
  <div class="col-1" style="background: #fc9;">Left column</div>
  <div class="col-2" style="background: #eee;">Middle column</div>
  <div class="col-3" style="background: #fc9;">Right column</div>
</div>
Reggie Pinkham
  • 11,985
  • 4
  • 38
  • 36
  • 1
    Best answer tbh. Works 4 years later and cleaner than other solutions. +1 – Harry J Mar 18 '20 at 04:54
  • @HarryJ This solution is not good, bc if Middle column content is too long, it will compress the width of left and right div. If you want left and right colum width fixed all the time, this solution doesn't work. – Lying_cat Nov 29 '21 at 06:24
  • 1
    @HarryJ If you want to achieve fixed width of left and right div. You have to set `flex: 0 0 100px;` for them, see this: https://stackoverflow.com/a/25266284/6304805 – Lying_cat Nov 29 '21 at 06:46
  • @SkuraZZ shows where my expertise lack! Thanks for the suggestion. Nonetheless, this did fix my problem. – Harry J Nov 29 '21 at 22:03
4

Although a bit late in posting an answer, here is an alternative approach without using margins.

<style>
    #divMain { width: 500px; }
    #div1 { width: 100px; float: left; background-color: #fcc; }
    #div2 { overflow:hidden; background-color: #cfc; }
    #div3 { width: 100px; float: right; background-color: #ccf; }
</style>

<div id="divMain">
    <div id="div1">
        div 1
    </div>
    <div id="div3">
        div 3
    </div>
    <div id="div2">
        div 2<br />bit taller
    </div>
</div>

This method works like magic, but here is an explanation :)\

Fiddle with a similar sample here.

Sunny R Gupta
  • 5,026
  • 1
  • 31
  • 40
4

The Div that has to take the remaining space has to be a class.. The other divs can be id(s) but they must have width..

CSS:

#main_center {
    width:1000px;
    height:100px;
    padding:0px 0px;
    margin:0px auto;
    display:block;
}
#left {
    width:200px;
    height:100px;
    padding:0px 0px;
    margin:0px auto;
    background:#c6f5c6;
    float:left;
}
.right {
    height:100px;
    padding:0px 0px;
    margin:0px auto;
    overflow:hidden;
    background:#000fff;
}
.clear {
    clear:both;
}

HTML:

<body>
    <div id="main_center">
        <div id="left"></div>
        <div class="right"></div>
        <div class="clear"></div>
    </div>
</body>

The following link has the code in action, which should solve the remaining area coverage issue.

jsFiddle

Community
  • 1
  • 1
2

I was looking for a solution to the opposite problem where I needed a fixed width div in the centre and a fluid width div on either side, so I came up with the following and thought I'd post it here in case anyone needs it.

#wrapper {
  clear: both;
  width: 100%;
}

#wrapper div {
  display: inline-block;
  height: 500px;
}

#center {
  background-color: green;
  margin: 0 auto;
  overflow: auto;
  width: 500px;
}

#left {
  float: left;
}

#right {
  float: right;
}

.fluid {
  background-color: yellow;
  width: calc(50% - 250px);
}
<div id="wrapper">
  <div id="center">
    This is fixed width in the centre
  </div>
  <div id="left" class="fluid">
    This is fluid width on the left
  </div>
  <div id="right" class="fluid">
    This is fluid width on the right
  </div>
</div>

If you change the width of the #center element then you need to update the width property of .fluid to:

width: calc(50% - [half of center width]px);
Adriano
  • 3,788
  • 5
  • 32
  • 53
damndaewoo
  • 520
  • 3
  • 11