i have 3 divs conatined within an outer div. i am aligning them horizontally by floating them left. and div3 as float right
我有一个外部div内的3个div。我通过向左浮动它们水平对齐它们。和div3为浮动权利
<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?
div1和div3具有固定的大小。如果div3被省略,我想要div 2来填补余下的空间。我该怎么做?
3 个解决方案
#1
26
What about something like this? https://jsfiddle.net/Siculus/9vs5nzy2/
这样的事情怎么样? 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>
#2
0
You don't need to float #div2
, it'll automatically fill up the remaining space.
你不需要浮动#div2,它会自动填满剩余的空间。
If you want borders/padding, you ought to give #div2
a child element.
如果你想要border / padding,你应该给#div2一个子元素。
#3
0
This is a technique using display: table;
https://jsfiddle.net/sxk509x2/
这是一种使用display:table; https://jsfiddle.net/sxk509x2/
Browser support (ie 11+): http://caniuse.com/#feat=css-table
浏览器支持(即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.
包含“pretty”的类不是完成您要执行的操作所必需的。我刚刚将它们添加到外观中。
#1
26
What about something like this? https://jsfiddle.net/Siculus/9vs5nzy2/
这样的事情怎么样? 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>
#2
0
You don't need to float #div2
, it'll automatically fill up the remaining space.
你不需要浮动#div2,它会自动填满剩余的空间。
If you want borders/padding, you ought to give #div2
a child element.
如果你想要border / padding,你应该给#div2一个子元素。
#3
0
This is a technique using display: table;
https://jsfiddle.net/sxk509x2/
这是一种使用display:table; https://jsfiddle.net/sxk509x2/
Browser support (ie 11+): http://caniuse.com/#feat=css-table
浏览器支持(即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.
包含“pretty”的类不是完成您要执行的操作所必需的。我刚刚将它们添加到外观中。