将 div 宽度设置为 100% 减去一定量的 px

发布于 2024-11-18 13:16:39 字数 319 浏览 2 评论 0原文

过去几个小时一直在网上搜索以找到解决方案,但找不到,所以我在这里。

我需要 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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

栀子花开つ 2024-11-25 13:16:39

简单的解决方案:

<div id="content">
    <div class="padder">

    </div><!-- .padder -->
</div>
<div id="sidebar">
    <div class="padder">

    </div><!-- .padder -->
</div>

CSS:

div#content {
    float: right;
    width: 100%;
}

div#content .padder {
    margin-left: 330px;
    padding: 0 30px 0 0;
}

div#sidebar {
    float: left;
    width: 300px;
    margin-top: -30px;
    padding-left: 30px;
    margin-right: -330px;
}

这将允许您拥有固定的侧边栏宽度和全宽内容区域。我已经使用过它很多次了,它就像一个魅力。

Simple solution:

<div id="content">
    <div class="padder">

    </div><!-- .padder -->
</div>
<div id="sidebar">
    <div class="padder">

    </div><!-- .padder -->
</div>

CSS:

div#content {
    float: right;
    width: 100%;
}

div#content .padder {
    margin-left: 330px;
    padding: 0 30px 0 0;
}

div#sidebar {
    float: left;
    width: 300px;
    margin-top: -30px;
    padding-left: 30px;
    margin-right: -330px;
}

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.

注定孤独终老 2024-11-25 13:16:39

CSS 目前不支持这种类型的计算(当然 Chromium 12/Ubuntu 11.04 中也不支持),但 CSS 3 中定义了一个 calc() 函数,它允许这种行为,使用简单的数学函数:(

section {
  float: left;
  margin: 1em; border: solid 1px;
  width: calc(100%/3 - 2*1em - 2*1px);
}

p {
  margin: calc(1rem - 2px) calc(1rem - 1px);
  border: solid transparent; border-width: 2px 1px;
}
p:hover { border-color: yellow; }

以上示例直接取自 W3.org。)

我自己的(详尽的)测试表明:

+----------------+-------------------+
|  Browser       |  Ubuntu 11.04     |
+----------------+-------------------+
|  Chromium  12  |  Fails            |
|  Firefox  5    |  Fails            |
|  Opera  11.10  |  Fails            |
+----------------+-------------------+

上述结果是使用指定的浏览器和 css calc() demo,其代码如下:

HTML:

<div id="box">This is the box.</div>

CSS:

#box {
    width: calc(100% - 20%);
    background-color: #f90;
    font-weight: bold;
    color: #fff;
    line-height: 2em;
    text-align: center;
    margin: auto;
}

(如果有人想在浏览器中运行上述测试他们的平台,并提供结果,或将其编辑到此答案中,这将非常表示赞赏。)

正如clairesuzy,在评论中:

[看看]caniuse它确实可以在 Firefox 中运行如果你使用 width: -moz-calc() 但仅此而已;)

事实上,在 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:

section {
  float: left;
  margin: 1em; border: solid 1px;
  width: calc(100%/3 - 2*1em - 2*1px);
}

p {
  margin: calc(1rem - 2px) calc(1rem - 1px);
  border: solid transparent; border-width: 2px 1px;
}
p:hover { border-color: yellow; }

(Above example taken directly from the W3.org.)

My own (in-exhaustive) tests show:

+----------------+-------------------+
|  Browser       |  Ubuntu 11.04     |
+----------------+-------------------+
|  Chromium  12  |  Fails            |
|  Firefox  5    |  Fails            |
|  Opera  11.10  |  Fails            |
+----------------+-------------------+

The above results were obtained using the named browsers and a css calc() demo, the code of which is below:

HTML:

<div id="box">This is the box.</div>

CSS:

#box {
    width: calc(100% - 20%);
    background-color: #f90;
    font-weight: bold;
    color: #fff;
    line-height: 2em;
    text-align: center;
    margin: auto;
}

(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:

[Take] a look at caniuse it does work in Firefox if you use width: -moz-calc() but that's about it ;)

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:

む无字情书 2024-11-25 13:16:39
<div id="left">...</div>
<div id="content">...<br> more content</div>

#left {float: left; width: 390px; background: #f76922;}
#content {overflow: hidden; background: #eee;}

浮动左侧 div 并从右侧 div 中创建一个新的块格式上下文(溢出:隐藏是一种方法),这样它将占用剩余空间

小提琴示例

<div id="left">...</div>
<div id="content">...<br> more content</div>

#left {float: left; width: 390px; background: #f76922;}
#content {overflow: hidden; background: #eee;}

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

心凉怎暖 2024-11-25 13:16:39

现在在其他 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?

林空鹿饮溪 2024-11-25 13:16:39

也许这与问题没有直接关系,但我在排列列表中的项目时遇到了类似的问题。固定宽度元素位于每个项目的右侧。我已经设法用以下方法解决了这个问题。这个想法是平衡正的 padding-left 和负的 margin-left,同时宽度设置为 100%

.item {
  max-height: 40px;
}

.item-title {
  display: inline-block;
  height: 40px;
  width: 100%;
  margin-left: -70px;
  padding-left: 65px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* This one should be fixed-width. */
.item-params {
  width: 60px;
  height: 40px;
  float: right;
}

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 negative margin-left, while the width is set to 100%:

.item {
  max-height: 40px;
}

.item-title {
  display: inline-block;
  height: 40px;
  width: 100%;
  margin-left: -70px;
  padding-left: 65px;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* This one should be fixed-width. */
.item-params {
  width: 60px;
  height: 40px;
  float: right;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文