简体   繁体   English

浮动DIV宽度= 100%-其他两个浮动div的宽度

[英]Floated DIV width = 100% - widths of two other floated divs

OK, so here is my problem, 好,这是我的问题,

I need to have four DIV s in one line. 我需要一行中有四个DIV The First three are float:left and the fourth one is float:right . 前三个是float:left ,第四个是float:right The container has a specified width. 容器具有指定的宽度。

I need the third div to fill all the space from the second div that is floated to the left, to the fourth div that is floated right. 我需要第三个div来填充从浮动到左边的第二个div到浮动到右边的第四个div所有空间。

EDIT: DIVs #1, #2 and #4 have dynamic width as well... They have a certain padding and the content defines the width. 编辑: DIV#1,#2和#4也具有动态宽度...它们具有一定的填充,并且内容定义了宽度。

Why not turn the question on its head, and establish how to create the layout you want- in which case, likely the simplest approach would be: 为什么不直接提出问题,并确定如何创建所需的布局,在这种情况下,最简单的方法可能是:

Demo Fiddle 演示小提琴

HTML 的HTML

<div class='table'>
    <div class='cell'>fit</div>
    <div class='cell'>fit</div>
    <div class='cell'>expand</div>
    <div class='cell'>fit</div>
</div>

CSS 的CSS

.table {
    display:table;
    width:100%; /* <-- will make the divs align across the full browser width */
    height:50px;
}
.cell {
    display:table-cell;
    border:1px solid red;
    width:1%; /* <-- will make 1, 2, 4 only fit their content */
}
.cell:nth-child(3) {
    width:100%; /* <-- will make 3 expand to the remaining space */
}

Solution Using Floated Elements 使用浮动元素的解决方案

Here is one way of doing this using floats. 这是使用浮点数的一种方法。

Arrange your HTML as follows: 如下安排HTML:

<div class="panel-container">
    <div class="panel p1">Panel 1 - and a word</div>
    <div class="panel p2">Panel 2 - Done. </div>
    <div class="panel p4">Panel 4 - End!</div>
    <div class="panel p3">Panel 3</div>
</div>

and apply the following CSS: 并应用以下CSS:

.panel-container {
    width: 600px;
    border: 1px dotted blue;
    overflow: auto;
}
.panel {
    background-color: lightgray;
    padding: 5px;
}
.p1 {
    float: left;
}
.p2 {
    float: left;
}
.p3 {
    background-color: tan;
    overflow: auto;
}
.p4 {
    float: right;
}

The trick is to place the floated elements ( .p1 , .p2 . .p4 ) ahead of the in-flow content ( .p3 ). 关键是要放置浮动元素( .p1.p2.p4 )提前在流内容的( .p3 )。

Use overflow: auto on the parent container to keep the floated child elements from affecting the layout outside of the parent element. 在父容器上使用overflow: auto ,以防止浮动子元素影响父元素外部的布局。

I added overflow: auto on .p3 so that the padding gets included within the containing block. 我添加了overflow: auto .p3 overflow: auto ,以便填充包含在包含块中。

See fiddle at: http://jsfiddle.net/audetwebdesign/9G8rT/ 请参阅以下网址的小提琴: http : //jsfiddle.net/audetwebdesign/9G8rT/

Comments 评论

The one disadvantage of this approach is that the order of the content is altered, that is, .p3 appears after .p4 in the code order. 这种方法的一个缺点是内容的顺序被更改,即, .p3以代码顺序出现在.p4之后。

Another side effect, which may be desirable in a responsive design, is that the child elements will wrap onto 2 or more lines as the parent container width gets smaller. 在响应式设计中可能需要的另一个副作用是,随着父容器宽度变小,子元素将缠绕在2行或更多行上。

If you need to retain the content order in the HTML code, the CSS table-cell solution is a good alterantive. 如果您需要保留HTML代码中的内容顺序,那么CSS表格单元解决方案是一个很好的替代方案。

The table-cell solution will keep the child elements on a single line regardless of the width of the parent container. 表格单元解决方案会将子元素保持在一行上,而不管父容器的宽度如何。

One final advangtage of the floated element solution is that it is more backward compatible than a CSS table-cell solution, but as we move forward, this is becoming less of a compelling argument. 浮动元素解决方案的最后一项优势是,它比CSS表格单元解决方案更向后兼容,但是随着我们的前进,这一点已不再具有吸引力。

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

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