我有2个div:一个在左边,一个在右边。左边的宽度是固定的,我想用右边的填充剩余的空间。

#搜索{ 宽度:160 px; 高度:25 px; 浮:左; background - color: # ffffff; } #{导航 宽度:780 px; 浮:左; background - color: # A53030; } < div id = "搜索" > < / div >文本 < div id = "导航" > < / div >导航


当前回答

我也遇到过类似的问题,并想出了以下行之有效的方法

CSS:

.top {
width: auto;
height: 100px;
background-color: black;
border: solid 2px purple;
overflow: hidden;
}
.left {
float:left;
width:100px;
background-color:#ff0000;
padding: 10px;
border: solid 2px black;
}
.right {
width: auto;
background-color:#00FF00;
padding: 10px;
border: solid 2px orange;
overflow: hidden;
}
.content {
margin: auto;
width: 300px;
min-height: 300px;
padding: 10px;
border: dotted 2px gray;
}

HTML:

<div class=top>top </div>
<div>
    <div class="left">left </div>
    <div class="right">
        <div class="content">right </div>
    </div>
</div>

这个方法不会在窗口收缩时自动换行,但会自动展开“内容”区域。它将为站点菜单(左)保持一个静态宽度。

以及自动展开内容框和左侧垂直框(站点菜单)的演示:

https://jsfiddle.net/tidan/332a6qte/

其他回答

我在Boushley的答案中发现的问题是,如果右列比左列长,它就会围绕着左列并继续填充整个空间。这不是我想要的行为。在搜索了许多“解决方案”后,我找到了一个教程(现在链接死了),如何创建三列页面。

作者提供了三种不同的方法,一种是固定宽度,一种是三个可变列,一种是固定外列,中间可变宽。比我发现的其他例子更加优雅和有效。显著提高了我对CSS布局的理解。

基本上,在上面的简单例子中,将第一列向左浮动,并给它一个固定的宽度。然后给右边的一列留出比第一列稍宽的左距。就是这样。完成了。Ala Boushley的代码:

下面是Stack Snippets & jsFiddle中的一个演示

#{离开 浮:左; 宽度:180 px; } #{正确 margin-left: 180 px; } /*只是为了突出div,例如*/ #left {background-color:粉色;} #right {background-color:浅绿色;} <div id="left"> left </div> . <div id="right"> right </div> .

在鲍什利的例子中,左边一列表示右边另一列。只要左列结束,右列就开始再次填充整个空间。在这里,右列只是进一步对齐到页面中,而左列占据了它的大空白。不需要流交互。

物品和容器的规则;

Container: {*** display: table; ***}
Items: {*** display: table-cell; width: 100%; ***}

使用空白:nowrap;为最后一项的文本进行解构。

项目X% |项目Y% |项目Z%

总长度总是= 100%!

if

Item X=50%
Item Y=10%
Item z=20%

然后

项目X = 70%

项目X占主导地位(在表格CSS布局中,第一项占主导地位)!

试着max-content;用于在容器中传播div的div内部属性:

div[class="item"] {
...
  width: -webkit-max-content;
  width: -moz-max-content;
  width: max-content;
...

}

现在,您应该使用flexbox方法(可以适用于所有带有浏览器前缀的浏览器)。

.container {
    display: flex;
}

.left {
    width: 180px;
}

.right {
    flex-grow: 1;
}

更多信息:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

/* * css * /

#search {
 position: absolute;
 width: 100px;
}
.right-wrapper {
  display: table;
  width: 100%;
  padding-left:100px;
}
.right-wrapper #navigation {
 display: table-cell;
 background-color: #A53030;
}

/* * html * /

<div id="search"></div>
<div class="right-wrapper">
   <div id="navigation"></div>
</div>

如果您试图填充flexbox中2项之间的剩余空间,请将以下类添加到您想要分离的2项之间的空div:

.fill {
  // This fills the remaining space, by using flexbox. 
  flex: 1 1 auto;
}