【问题标题】:Is it possible for absolute div to have width: auto, and also ignore it's parent's width?绝对 div 是否有可能具有宽度:自动,并且还忽略它的父宽度?
【发布时间】:2020-04-13 08:43:19
【问题描述】:

我正在尝试制作一个div,它可以拉伸以适应其内容,达到某个最大宽度,然后内容应该换行。

使用绝对定位的div 可以正常工作——除非其父级的宽度受到限制。当其父级的宽度有限时,绝对定位的div 就像其max-width 是其父级的width

我基本上希望绝对定位的div“假装”它的父级具有 100% 的宽度,并给我一个很好的“拉伸以适应”行为,同时尊重我设置的 max-width .

在下面的示例中,我希望第一个 .abs 工作,即使它是“瘦” div 的子级。

.parent {
  position: relative;
  background: #EEE;
  width: 100px;
}
.abs {
  position: absolute;
  max-width: 200px;
  /* width:  auto, but ignore parent! */
}

.parent2 {
  margin-top: 150px;
  background: rgba(0,128,0,.1);
  width: 100px;
}
  <div class="parent">
    <div>
      Doesn't work.
    </div>
    <div class="abs">
      This wraps after 100px, because it's parent has 100px width.
    </div>
  </div>
  
  <div class="parent2">
    <div>
      Does work.
    </div>
    <div class="abs">
      This wraps at 200px, because it's parent is as wide as the viewport, so it honors the max-width of 200px.
    </div>
  </div>

https://jsfiddle.net/nqws2p09/1/

【问题讨论】:

    标签: css width css-position


    【解决方案1】:

    使用width: max-content; 强制绝对定位的子元素根据其内容而不是父元素的尺寸调整自身大小。

    这也适用于最大宽度。

    .parent {
      position: relative;
      background: #EEE;
      width: 100px;
    }
    .abs {
      position: absolute;
      max-width: 200px;
      width: max-content; /* this forces width: auto */
    }
      <div class="parent">
        <div>
          Div inside parent element. This will wrap at 100px.
        </div>
        <div class="abs">
          This absolutely positioned child element wraps after 200px, even though the parent's max-width is set to 100px
        </div>
      </div>

    【讨论】:

      【解决方案2】:

      由于在绝对元素的宽度计算中考虑了父元素的填充,因此您可以向父元素添加更多的填充:

      .parent {
        position: relative;
        background: #EEE content-box; /* Color only the content */
        width: 100px;
        padding-right:200px;
        margin-right:-200px; /*to consume the padding added*/
      }
      .abs {
        position: absolute;
        max-width: 200px;
      }
      
      .parent2 {
        margin-top: 150px;
      }
      <div class="parent">
          <div>
            Does work.
          </div>
          <div class="abs">
            This wraps after 100px, because it's parent has 100px width.
          </div>
        </div>
        
        <div class="parent parent2">
          <div>
            Does work.
          </div>
          <div class="abs">
            This wraps at 200px, because it's parent is as wide as the viewport, so it honors the max-width of 200px.
          </div>
        </div>

      或者考虑定位元素的大边距:

      .parent {
        position: relative;
        background: #EEE;
        width: 100px;
      }
      .abs {
        position: absolute;
        max-width: 200px;
        margin-right:-200px;
      }
      
      .parent2 {
        margin-top: 150px;
      }
      <div class="parent">
          <div>
            Does work.
          </div>
          <div class="abs">
            This wraps after 100px, because it's parent has 100px width.
          </div>
        </div>
        
        <div class="parent parent2">
          <div>
            Does work.
          </div>
          <div class="abs">
            This wraps at 200px, because it's parent is as wide as the viewport, so it honors the max-width of 200px.
          </div>
        </div>

      【讨论】:

      • 这会导致“父”占用 300px 的宽度,而它应该只占用 100px。
      • @JS_Riddler 这就是为什么我添加了负边距来消耗额外的空间,因为它是填充,里面的内容仍然需要 100px
      • 我通过添加边框进行了检查,发现它是 300 像素宽——但你是对的,flow 是正确的。然而,现在添加像backgroundborder 这样的样式就成了一个问题。我发布了一个在我看来效果更好的解决方案,请告诉我您的想法。
      • @JS_Riddler 对此添加了另一个想法,如果有兴趣的话
      【解决方案3】:

      通过将abs 包装在一个宽度比relative 父级宽得多的绝对div 中在某种程度上 解决了这个问题。现在问题变成了调整wrapper 的大小以延伸到视口的右侧,这是另一个问题的主题。

      .parent {
        position: relative;
        background: #EEE content-box; /* Color only the content */
        width: 100px;
      }
      .abs-wrapper {
        position: absolute;
        width: 800px;
      }
      .abs {
        position: absolute;
        max-width: 200px;
        border: 1px solid gray;
      }
        <div class="parent">
          <div>
            Doesn't work.
          </div>
          <div class="abs-wrapper">
            <div class="abs">
              This correctly wraps after 200px, because it's parent is a wrapper with a very large width. Thus, this div will stretch-to-fit, and also honor max-width.
            </div>
          </div>
        </div>
        
        
          <div class="parent" style="margin-top: 150px">
          <div>
            Doesn't work.
          </div>
          <div class="abs-wrapper">
            <div class="abs">
              &lt; 200px shrinks to fit.
            </div>
          </div>
        </div>

      https://jsfiddle.net/rxwqtpsz/

      【讨论】:

      • 在这种情况下,只需将新包装器设置为 200 像素,您可以删除最大宽度,不再需要它。并从内部 div 中删除 absolute 并使用 inline-block
      猜你喜欢
      • 2011-08-04
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      相关资源
      最近更新 更多