将 div 宽度设置为 100% 减去一定量的 px
过去几个小时一直在网上搜索以找到解决方案,但找不到,所以我在这里。
我需要 div
的宽度为 100%
减去 left div
的宽度。
这样,其左侧的 div
保持相同的宽度 (390px),但另一个 div
根据分辨率调整其大小。我找到了两侧都有固定宽度 div
的解决方案,但就是无法弄清楚。
Have been searching the net for the past hours to find a solution to this, but couldn't find it, so here I am.
I need the width of the div
to be 100%
minus the width of the left div
.
So that the div
to the left of it stays the same width (390px) but the other div
adjusts its size depending on the resolution . I have found the solution where it has a fixed width div
on both sides, but just cant figure this out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
简单的解决方案:
CSS:
这将允许您拥有固定的侧边栏宽度和全宽内容区域。我已经使用过它很多次了,它就像一个魅力。
Simple solution:
CSS:
This will allow you to have a fixed sidebar width and a full width content area. I have used it many times and it works like a charm.
CSS 目前不支持这种类型的计算(当然 Chromium 12/Ubuntu 11.04 中也不支持),但 CSS 3 中定义了一个 calc() 函数,它允许这种行为,使用简单的数学函数:(
以上示例直接取自 W3.org。)
我自己的(详尽的)测试表明:
上述结果是使用指定的浏览器和 css
calc()
demo,其代码如下:HTML:
CSS:
(如果有人想在浏览器中运行上述测试他们的平台,并提供结果,或将其编辑到此答案中,这将非常表示赞赏。)
正如clairesuzy,在评论中:
事实上,在 Firefox 5 (Ubuntu 11.04) 中它确实可以工作(其他供应商前缀似乎对 Opera 或不过,遗憾的是 Webkit):修订后的供应商前缀演示。
参考:
This type of calculation isn't currently supported in CSS (certainly not in Chromium 12/Ubuntu 11.04), but there is a
calc()
function defined in CSS 3, which would allow for this kind of behaviour, using simple mathematical functions:(Above example taken directly from the W3.org.)
My own (in-exhaustive) tests show:
The above results were obtained using the named browsers and a css
calc()
demo, the code of which is below:HTML:
CSS:
(If anyone would like to run the above test in the browsers on their platform, and supply the results, or edit them in to this answer, that would be much appreciated.)
As pointed out, by clairesuzy, in comments:
And, indeed, in Firefox 5 (Ubuntu 11.04) it does work (the other vendor prefixes don't appear to do anything for Opera or Webkit, though; sadly): Revised vendor-prefixed demo.
Reference:
浮动左侧 div 并从右侧 div 中创建一个新的块格式上下文(溢出:隐藏是一种方法),这样它将占用剩余空间
小提琴示例
float the left div and make a new block formatting context out of the right div (overflow: hidden is one way to do that), that way it will take the remaining space
Example Fiddle
现在在其他 FireFox 浏览器中确实没有办法直接使用 CSS 来做到这一点(请参阅 MDN 文档)。您可以使用 javascript 来执行相同的操作,但我建议重新考虑您的布局。
编辑:实际上 IE 9 也可以处理它,请参阅 MSDN 文档。 IE 好用吗?
There really isn't a way of doing this with straight up CSS right now in browsers other FireFox (see the MDN docs). You could use javascript to do the same, but I'd suggest rethinking your layout.
EDIT: actually IE 9 can handle it as well, see MSDN docs. Yay for IE?
也许这与问题没有直接关系,但我在排列列表中的项目时遇到了类似的问题。固定宽度元素位于每个项目的右侧。我已经设法用以下方法解决了这个问题。这个想法是平衡正的
padding-left
和负的margin-left
,同时宽度设置为100%
:Maybe this is not directly related to the question but I had a similar problem to arrange items in a list. The fixed-width element is to the right of each item. I've managed to solve this with the following approach. The idea is to balance a positive
padding-left
with a negativemargin-left
, while the width is set to100%
: