make div fill up remaining space

i have 3 divs conatined within an outer div. i am aligning them horizontally by floating them left. and div3 as float right

<div id="outer">

  <div id="div1">always shows</div>
  <div id="div2">always shows</div>
  <div id="div3">sometimes shows</div>
</div>

div1 and div3 have fixed sizes. if div3 is left out i want div 2 to fill up the remaining space. how can i do it?


What about something like this? https://jsfiddle.net/Siculus/9vs5nzy2/

CSS:

#container{
    width: 100%;
    float:left;
    overflow:hidden; /* instead of clearfix div */
}
#right{
    float:right;
    width:50px;
    background:yellow;
}
#left{
    float:left;
    width:50px;
    background:red;
}
#remaining{
    overflow: hidden;
    background:#DEDEDE;
}

Body:

<div id="container">
    <div id="right">div3</div>

    <div id="left">div1</div>

    <div id="remaining">div2, remaining</div>
</div>

You don't need to float #div2 , it'll automatically fill up the remaining space.

If you want borders/padding, you ought to give #div2 a child element.


This is a technique using display: table; https://jsfiddle.net/sxk509x2/

Browser support (ie 11+): http://caniuse.com/#feat=css-table

HTML

<div class="outer">
    <div class="static pretty pretty-extended">$</div>
    <input class="dynamic pretty" type="number" />
    <div class="static pretty">.00</div>
</div>

CSS

.outer{
    width:300px;
    height:34px;
    display:table;
    position: relative;
    box-sizing: border-box;
}
.static{
    display:table-cell;
    vertical-align:middle;
    box-sizing: border-box;
}
.dynamic{
    display:table-cell;
    vertical-align:middle;
    box-sizing: border-box;
    width: 100%;
    height:100%;
}
.pretty{
    border: 1px solid #ccc;
    padding-left: 7px;
    padding-right: 7px;
    font-size:16px;
}
.pretty-extended{
    background: #eee;
    text-align:center;
}

The classes that contain "pretty" are not required to accomplish what you are trying to do. I just added them for appearances.

链接地址: http://www.djcxy.com/p/88272.html

上一篇: 具有等间距DIV的流体宽度

下一篇: 使div充满剩余的空间