如何让div填充剩余的水平空间?
我有 2 个 div:一个位于页面左侧,一个位于页面右侧。 左侧的宽度固定,我希望右侧的宽度填充剩余空间。
#search {
width: 160px;
height: 25px;
float: left;
background-color: #ffffff;
}
#navigation {
width: 780px;
float: left;
background-color: #A53030;
}
<div id="search">Text</div>
<div id="navigation">Navigation</div>
I have 2 divs: one in the left side and one in the right side of my page. The one in the left side has fixed width and I want the one of the right side to fill the remaining space.
#search {
width: 160px;
height: 25px;
float: left;
background-color: #ffffff;
}
#navigation {
width: 780px;
float: left;
background-color: #A53030;
}
<div id="search">Text</div>
<div id="navigation">Navigation</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(26)
我在鲍什利的答案中发现的问题是,如果右列比左列长,它只会环绕左列并恢复填充整个空间。 这不是我想要的行为。 在搜索了大量的“解决方案”后,我找到了一个关于创建三栏页面的教程(现在链接已失效)。
作者提供了三种不同的方式,一种固定宽度,一种具有三个可变列,一种具有固定外列和可变宽度中间。 比我发现的其他例子更加优雅和有效。 显着提高了我对 CSS 布局的理解。
基本上,在上面的简单情况下,将第一列向左浮动并为其指定固定宽度。 然后给右边的列一个比第一列宽一点的左边距。 就是这样。 完毕。 Ala Boushley 的代码:
这是 Stack Snippets 中的演示 jsFiddle
在鲍什利的例子中,左列将另一列固定在右边。 一旦左列结束,右列就开始再次填充整个空间。 在这里,右列只是进一步与页面对齐,左列占据了它的大边距。 无需流程交互。
The problem that I found with Boushley's answer is that if the right column is longer than the left it will just wrap around the left and resume filling the whole space. This is not the behavior I was looking for. After searching through lots of 'solutions' I found a tutorial (now link is dead) on creating three column pages.
The author offer's three different ways, one fixed width, one with three variable columns and one with fixed outer columns and a variable width middle. Much more elegant and effective than other examples I found. Significantly improved my understanding of CSS layout.
Basically, in the simple case above, float the first column left and give it a fixed width. Then give the column on the right a left-margin that is a little wider than the first column. That's it. Done. Ala Boushley's code:
Here's a demo in Stack Snippets & jsFiddle
With Boushley's example the left column holds the other column to the right. As soon as the left column ends the right begins filling the whole space again. Here the right column simply aligns further into the page and the left column occupies it's big fat margin. No flow interactions needed.
如今,您应该使用
flexbox
方法(可能适用于带有浏览器前缀的所有浏览器)。更多信息:https://css-tricks.com/snippets/css /弹性盒指南/
These days, you should use the
flexbox
method (may be adapted to all browsers with a browser prefix).More info: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
解决方案来自于显示属性。
基本上,您需要使两个 div 充当表格单元格。 因此,您必须在两个 div 上使用
display:table-cell
,而不是使用float:left
,并且对于动态宽度 div,您需要设置宽度:自动;
也。 这两个 div 都应放置在具有display:table
属性的 100% 宽度容器中。这是 css:
和 HTML:
重要提示:对于 Internet Explorer,您需要在动态宽度 div 上指定 float 属性,否则空间将不会被填充。
我希望这能解决您的问题。
如果您愿意,您可以阅读我在 我的博客。
The solution comes from the display property.
Basically you need to make the two divs act like table cells. So instead of using
float:left
, you'll have to usedisplay:table-cell
on both divs, and for the dynamic width div you need to setwidth:auto;
also. The both divs should be placed into a 100% width container with thedisplay:table
property.Here is the css:
And the HTML:
IMPORTANT:For Internet Explorer you need to specify the float property on the dynamic width div, otherwise the space will not be filled.
I hope that this will solve your problem.
If you want, you can read the full article I wrote about this on my blog.
由于这是一个相当受欢迎的问题,我倾向于使用 BFC 分享一个很好的解决方案。
此处的 Codepen 示例。
在这种情况下,
overflow: auto
会触发上下文行为,并使右侧元素仅扩展至可用的剩余宽度,并且如果.left 它将自然扩展至全宽消失。 对于许多 UI 布局来说,这是一个非常有用且简洁的技巧,但一开始可能很难理解“它为什么起作用”。
Since this is a rather popular question, I'm inclined to share a nice solution using BFC.
Codepen sample of the following here.
In this case,
overflow: auto
triggers context behavior and makes the right element expand only to the available remaining width and it will naturally expand to full width if.left
disappears. A highly useful and clean trick for many UI layouts, but perhaps hard to understand the "why it works" at first.这似乎可以实现你想要的目标。
This seems to accomplish what you're going for.
如果您不需要与某些浏览器的旧版本(例如 IE
108 或更低)兼容,您可以使用calc()
CSS 函数:If you don't need compatibility with older versions of certain browsers (IE
108 or less for example) you can use thecalc()
CSS function:@Boushley 的答案是最接近的,但是有一个问题没有得到解决,已经指出了。 right div 占据浏览器的整个宽度; 内容采用预期的宽度。 要更好地查看此问题:
http://jsfiddle.net/79hpS/
内容位于正确的位置(在 Firefox 中),但是宽度不正确。 当子元素开始继承宽度时(例如带有
width: 100%
的表格),它们的宽度等于浏览器的宽度,导致它们溢出页面右侧并创建水平滚动条(在 Firefox 中)或不浮动并被向下推(在 chrome 中)。您可以通过将
overflow:hidden
添加到右列来轻松修复此问题。 这将为您提供内容和 div 的正确宽度。 此外,表格将接收正确的宽度并填充剩余的可用宽度。我尝试了上面的一些其他解决方案,它们不能完全处理某些边缘情况,而且太复杂,无法保证修复它们。 这很有效而且很简单。
如果有任何问题或疑虑,请随时提出。
@Boushley's answer was the closest, however there is one problem not addressed that has been pointed out. The right div takes the entire width of the browser; the content takes the expected width. To see this problem better:
http://jsfiddle.net/79hpS/
The content is in the correct place (in Firefox), however, the width incorrect. When child elements start inheriting width (e.g. the table with
width: 100%
) they are given a width equal to that of the browser causing them to overflow off the right of the page and create a horizontal scrollbar (in Firefox) or not float and be pushed down (in chrome).You can fix this easily by adding
overflow: hidden
to the right column. This gives you the correct width for both the content and the div. Furthermore, the table will receive the correct width and fill the remaining width available.I tried some of the other solutions above, they didn't work fully with certain edge cases and were just too convoluted to warrant fixing them. This works and it's simple.
If there are any problems or concerns, feel free to raise them.
这是对已接受解决方案的一个小修复,可防止右列落在左列下方。 将
width: 100%;
替换为overflow: hide;
如果有人不知道的话,这是一个棘手的解决方案。http://jsfiddle.net/MHeqG/2600/
[编辑] 另请检查三列的示例布局:
http://jsfiddle.net/MHeqG/3148/
Here is a little fix for accepted solution, which prevents right column from falling under the left column. Replaced
width: 100%;
withoverflow: hidden;
a tricky solution, if somebody didn't know it.http://jsfiddle.net/MHeqG/2600/
[edit] Also check an example for three column layout:
http://jsfiddle.net/MHeqG/3148/
如果您尝试填充 Flexbox 中 2 个项目之间的剩余空间,请将以下类添加到要分隔的 2 个项目之间的空 div 中:
If you are trying to fill remaining space in a flexbox between 2 items, add the following class to a an empty div between the 2 you want to seperate:
使用
display:flex
Use
display:flex
鲍什利的答案似乎是使用浮动来安排此问题的最佳方法。 然而,它并非没有问题。 您将无法使用扩展元素内的嵌套浮动; 它会破坏页面。
当涉及到扩展元素时,所示的方法基本上是“伪造的”——它实际上并不是浮动的,它只是使用其边距与固定宽度的浮动元素一起播放。
那么问题就在于:扩展元素没有浮动。 如果您尝试在扩展元素中进行任何嵌套浮动,那么这些“嵌套”浮动项目根本就不是真正嵌套的; 当您尝试在“嵌套”浮动项目下粘贴
clear: Both;
时,您最终也会清除顶级浮动。然后,要使用 Boushley 的解决方案,我想补充一点,您应该放置一个 div,如下所示:
.fakeFloat
{
高度:100%;
宽度:100%;
向左飘浮;
}
并将其直接放置在展开的 div 中; 所有扩展的 div 内容都应该位于此 fakeFloat 元素内。
因此,我建议在这种特定情况下使用表格。 浮动元素实际上并不是为了进行您想要的扩展而设计的,而使用表格的解决方案却很简单。 人们通常认为浮动更适合布局,而不是表格..但无论如何你都没有在这里使用浮动,你在伪造它 - 这违背了这种特定情况下风格论证的目的,在我的拙见。
Boushley's answer seems to be the best way to go in order to arrange this using floats. However, it isn't without its problems. Nested floating within the expanded element will not be available to you; it will break the page.
The method shown basically "fakes it" when it comes to the expanding element - it isn't actually floating, it's just playing along with the fixed-width floated elements using its margins.
The problem then is exactly that: the expanding element isn't floated. If you try and have any nested floating within the expanding element, those "nested" floated items aren't really nested at all; when you try to stick a
clear: both;
under your "nested" floated items, you'll end up clearing the top-level floats as well.Then, to use Boushley's solution, I'd like to add that you should place a div such as the following:
.fakeFloat
{
height: 100%;
width: 100%;
float: left;
}
and place this directly within the expanded div; all the expanded div's contents should go then within this fakeFloat element.
For this reason, I'd recommend using tables in this specific case. Floated elements really aren't designed to do the expansion that you'd like, whereas the solution using a table is trivial. An argument is generally made that floating is more proper for layouts, not tables.. but you aren't using floating here anyways, you're faking it - and that sort of defeats the purpose of the stylistic argument for this specific case, in my humble opinion.
我尝试了上述液体左侧解决方案和固定右侧解决方案,但没有一个有效(我知道问题是相反的,但我认为这是相关的)。 这是有效的:
I tried the above solutions for a liquid left, and a fixed right but non of them worked (I am aware the question is for the reverse but I think this is relevant). Here's what did work:
我遇到了类似的问题,我在这里找到了解决方案:
https://stackoverflow.com/a/16909141/3934886
该解决方案适用于固定中心 div 和液体侧列。
如果您想要固定的左列,只需相应地更改公式即可。
I had a similar problem and I found the solution here:
https://stackoverflow.com/a/16909141/3934886
The solution is for a fixed center div and liquid side columns.
If you want a fixed left column, just change the formula accordingly.
您可以使用网格 CSS 属性,这是构造您的框的最清晰、干净和直观的方式。
You can use the Grid CSS properties, is the most clear, clean and intuitive way structure your boxes.
如果有人需要相同的解决方案,但没有左侧 div 的固定长度:
如果您希望左侧 div 占据所需的所有空间,您可以删除固定大小 180px。 请参阅下面的 CSS:
请参阅 JSFiddle:jsfiddle-div-space
If anyone needs the same solution but without the fixed length of left div:
If you want the left side div to take all the space it needs you can remove the fixed size 180px. See CSS below:
See here in JSFiddle: jsfiddle-div-space
我想知道没有人使用
position:absolute
和position:relative
所以,另一种解决方案是:
HTML
CSS
Jsfiddle 示例
I wonder that no one used
position: absolute
withposition: relative
So, another solution would be:
HTML
CSS
Jsfiddle example
/*
* CSS
*/
/*
* html
*/
/*
* css
*/
/*
* html
*/
我对此有一个非常简单的解决方案!
//HTML
//CSS
链接: http://jsfiddle.net/MHeqG/
I have a very simple solution for this !
//HTML
//CSS
Link: http://jsfiddle.net/MHeqG/
我遇到了类似的问题,并提出了以下效果很好的
CSS:
HTML:
当窗口缩小时,此方法不会换行,但会自动扩展“内容”区域。 它将保持站点菜单的静态宽度(左)。
对于自动扩展内容框和左侧垂直框(站点菜单)演示:
https://jsfiddle.net/tidan/ 332a6qte/
I had a similar issue and came up with the following which worked well
CSS:
HTML:
This method won't wrap when the window is shrunk but will auto expand the 'content' area. It will keep a static width for the site menu (left).
And for auto expanding content box and left vertical box(site menu) demo:
https://jsfiddle.net/tidan/332a6qte/
尝试添加position
relative
,删除右侧的width
和float
属性,然后添加left
和right
属性,值为0
。此外,您还可以添加
margin left
规则,其值基于左侧元素的宽度(如果需要中间的空间,则添加一些像素) 以保持其位置。这个例子对我有用:
Try adding position
relative
, removewidth
andfloat
properties of the right side, then addleft
andright
property with0
value.Also, you can add
margin left
rule with the value is based on the left element's width (+ some pixels if you need space in between) to maintain its position.This example is working for me:
尝试这个。 这对我有用。
Try this. It worked for me.
我已经研究这个问题两天了,并且有一个解决方案,可能适合您和其他任何尝试制作响应式固定宽度左侧并让右侧填充屏幕的其余部分而不环绕左侧的人。 我认为的目的是使页面在浏览器和移动设备中具有响应能力。
这是代码
这是我的小提琴,可能对你有用,就像对我一样。
https://jsfiddle.net/Larry_Robertson/62LLjapm/
I have been working on this problem for two days and have a solution that may work for you and anyone else trying to make a responsive Fixed width left and have the right side fill in the remainder of the screen without wrapping around the left side. The intention I assume is to make the page responsive in browsers as well as mobile devices.
Here is the Code
Here is my fiddle that may just work for you as it did for me.
https://jsfiddle.net/Larry_Robertson/62LLjapm/
物品和容器的规则;
对最后一项中的文本使用 white-space: nowrap; 进行解构。
项目 X% | 项目 Y% | 项目 Z%
总长度始终 = 100%!
如果
那么
Item X=70%
Item X 占主导地位(第一个项目在表格 CSS 布局中占主导地位)!
尝试内部 div 的 max-content; 属性,以便在容器中传播 div:
}
Rules for items and containers;
Use white-space: nowrap; for texts in last items for their undestructuring.
Item X% | Item Y% | Item Z%
Total length always = 100%!
if
then
Item X=70%
Item X is dominant (first items are dominant in table CSS layout)!
Try max-content; property for div inside for spreading div in container:
}
最简单的解决方案是使用保证金。 这也会有反应!
The easiest solution is to use margin. This will also be responsive!
最简单的解决方案是使左侧 div 宽度等于 100% - 右侧 div 的宽度加上它们之间的任何边距。
https://codepen.io/tamjk/pen/dybqKBN
Simplest solution is to just make the left div width equal 100% - the width of the right div plus any margin between them.
https://codepen.io/tamjk/pen/dybqKBN
我在尝试布局一些 jqueryUI 控件时遇到了同样的问题。 尽管现在普遍的理念是“使用
DIV
而不是TABLE
”,但我发现在我的案例中使用 TABLE 效果更好。 特别是,如果您需要在两个元素内进行直接对齐(例如,垂直居中、水平居中等),TABLE 中可用的选项可以为此提供简单、直观的控制。这是我的解决方案:
I ran into this same problem trying to layout some jqueryUI controls. Although the common philosophy now is "use
DIV
instead ofTABLE
", I found in my case using TABLE worked much better. In particular, if you need to have straightforward alignment within the two elements (e.g., vertical centering, horizontal centering, etc.) the options available with TABLE give simple, intuitive controls for this.Here's my solution: