【问题标题】:Why won't this div resize given position:relative; left:0px; right: 200px;为什么这个 div 不会调整给定位置的大小:相对;左:0px;右:200px;
【发布时间】:2017-03-17 13:47:07
【问题描述】:

我希望向下的第三个 div“内容”填充其容器,但在右侧留出 200 像素的空间以适应 fixedWidthButtons

到目前为止,无论我设置什么,它都不会影响 div 的宽度。

  • 如果我设置它的display:block;,它会完全填满容器,并且按钮会被推出容器。

  • 如果我设置display:inline-block;,容器将变为181.344 px 宽,并且无论我将right 设置为什么都不会调整大小。

    <div class="container" style="left:0; right:0; margin-bottom: 10px; height: 65px; display: block;">
        <div class="panel" style="width:100%; display: block;">
            <div class="contents" style="display:inline-block; position:relative; left: 0px; right: 200px;">
                <div class="buttonTextAndCounterContainer" style="width:100%; display:block">
                    <div class="button" style="float:left; display:none;"></div>
                    <div class="textAndCounterContainer" style="display:block;">
                        <div class="counter" style="float:right; display:block;"></div>
                        <div class="text" style="width:100%; position:relative; left:0px; vertical-align:top; display:inline-block;"></div>
                    </div>
                </div>
            </div>
            <div class="fixedWidthButtons" style="display:inline-block; float:right;"></div>
        </div>
    </div>

【问题讨论】:

  • 如果我理解正确,您正试图通过赋予其左右属性来声明 div 的宽度。据我所知,您必须通过声明来声明 div 的宽度。 Left 和 Right 是定位声明。 (w3schools.com/CSSref/pr_pos_right.asp)
  • 但我可以使用 right:0;左:0;将宽度设置为 100% 对吗?
  • @GlenPierce 仅当您使用 position: absolute; 时。 position: relative; 的用途完全不同。

标签: html css


【解决方案1】:

您需要阅读您正在处理的一些属性。位置是原点而不是宽度。

您还表达了块与内联块的一些问题,这些问题很容易在谷歌上搜索到。也就是说,您的问题的解决方案是更改 css:

.content {
    display: inline-block;
    width: calc(100% - 200px);
}

【讨论】:

  • 是的,这可以解决问题,但我正在使用 gwt 并且无法让 calc() 工作,所以我希望我可以通过左右实现类似的效果.
  • 正如我所说的左右参数位置更关心的是窗口在哪里不是它的大小。至于 calc 的问题,您是否在算术符号周围留下空格?有关详细信息,请参阅此主题:stackoverflow.com/questions/16290873/…
  • 如果您无法让calc() 工作,您可能会省略参数和运算符之间的空格。
  • @connexo,你的意思是我需要 100%、- 和 200px 之间的空白?
  • .getElement().getStyle().setProperty("width", "calc(100% - 200px)") 在 gwt 中工作 :) 太棒了,谢谢 :)
【解决方案2】:

解决方案:

要仅使用leftright 设置div 的宽度,您必须设置div 的position: absolute;

来源:http://alistapart.com/article/conflictingabsolutepositions

【讨论】:

    【解决方案3】:

    几点建议:

    • position: absolute 相对于其相对位置(位于relative 的父级)起作用。
    • position: relative 通知元素未定位(完全不改变布局),如果设置为absolute,则使其成为子元素的位置相对于它的父元素
    • 设置inline-block 也为我们提供了设置宽度和高度,它将调整到;如果不需要,最好使用inline
    • inline-styles去掉就好了 - 下面的示例sn-p

    .container {
      margin-bottom: 10px;
      height: 65px;
      border: 1px solid black;
      position: relative;
    }
    .contents {
      border: 1px solid grey;
      position: absolute;
      display: inline-block;
      width: 300px;
      left: 0;
    }
    .fixedWidthButtons {
      display: inline-block;
      width: 200px;
      position: absolute;
      right: 0;
      border: 1px solid red;
    }
    <div class="container">
      <div class="panel">
        <div class="contents">
          <div class="buttonTextAndCounterContainer">
            <div class="button">button</div>
            <div class="textAndCounterContainer">
              <div class="counter">counter</div>
              <div class="text">text</div>
            </div>
          </div>
        </div>
        <div class="fixedWidthButtons">For my buttons</div>
      </div>
    </div>

    【讨论】:

    • 你的代码 sn-p 比我的更清晰,干得好。
    • 感谢您的建议,“位置:绝对相对于其相对位置(相对定位的父级)起作用。”这非常有帮助,因为很明显,我真的很想利用 position: relative。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多