简体   繁体   English

使div填充剩余空间

[英]Make div fill the remaining space

This question has been asked a million times but I can't seem to find the solution for my specific version. 这个问题已被问过一百万次,但我似乎找不到我特定版本的解决方案。 Here is what I have: 这是我有的:

<div>
    <div id="div1"></div>
    <div id="div2"></div>
    <div id="div3"></div>
</div>

div1 and div2 are always shown and div3 is only sometimes shown. 始终显示div1和div2,有时仅显示div3。 div2 and div3 have fixed widths. div2和div3有固定的宽度。 I want div1 to fill the remaining space that div2 and possible div3 are not taking up. 我希望div1填充div2和可能的div3没有占用的剩余空间。 I want them to appear in the above order left to right ([div1][div2][div3]). 我希望它们以从左到右的顺序出现([div1] [div2] [div3])。

Can anyone help with this? 有人能帮忙吗?

Answer: 回答:

See in action: http://jsfiddle.net/C9Q3F/3/ 见作者: http//jsfiddle.net/C9Q3F/3/

<style>
    div { height:100px; }
    #div1 { background-color:red; }
    #div2 { background-color:blue; width:75px; float:right; }
    #div3 { background-color:green; width:100px; float:right; }
</style>
​
<div>
    <div id="div2"></div>
    <div id="div3"></div>
    <div id="div1"></div>
</div>​

Explanation: 说明:

The reason this works, is - the order of the divs changed - so it writes div2 and div3 to the page, both with fixed widths, and both float ing. 这个工作的原因是 - div的顺序改变了 - 所以它将div2div3写入页面,两者都有固定的宽度,并且都float So by the time div1 is written (now last), it's default 100% width will fill the 100% of the available width. 因此,当写入div1 (现在是最后一个)时,默认的100%宽度将填充100%的可用宽度。 Since the other divs are taking up their space already, the "available width" is what's left. 由于其他div已经占据了他们的空间,“可用宽度”就是剩下的。

For other cases (eg. last div fills space): 对于其他情况(例如,最后一个div填充空间):

If you want the last div to fill the remaining space instead of the first, like this question/answer, just tweak it to use float:left; 如果你想让最后一个div填充剩下的空间而不是第一个,就像这个问题/答案一样,只需调整它就可以使用float:left; instead of float:right; 而不是float:right; and change the div order and widths - same concept as this and works just fine. 并改变div的顺序和宽度 - 与此相同的概念,并且工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM