html - 绝对定位的 div 动态扩展到相对父 div 之外

标签 html css

我在相对定位的 div 中有一个绝对定位的 div。这两个都位于我无法更改的父 div 中,它显式设置了 150px 的 width。我希望我的绝对定位 div 能够超出这个 150px 宽度。我还需要它是动态的——即我不想在任何地方显式设置宽度,我想要你通常通过设置 max-width 并获得绝对定位的 div 填充可用空间。这可能吗?

.outerContainer {
  width:150px;/*can't be changed*/
}

.innerContainer {
  position: relative;
}

.absolutely {
  position: absolute;
}

.item {
  display:inline-block;
}
<div class="outerContainer">
    <div class="innerContainer">
        <div class="absolutely"> <!-- want this to extend beyond outerContainer width so items wrap less-->
          <div class="item">
              first item
          </div>
          <div class="item">
              second item
          </div>
          <div class="item">
              third item
          </div>
          <div class="item">
              fourth item
          </div>
        </div>
    </div>
</div>

jsfiddle 版本: https://jsfiddle.net/3xbpzp7b/

最佳答案

你可以做的是为 .innerContainer 元素添加一个固定宽度,作为 .absolutely 的一种 max-width > 正如你描述的那样。如果 .absolutely 中的内容较多,它会换行,如果内容较少,它会变得不那么宽。

.outerContainer {
  width:150px;/*can't be changed*/
}

.innerContainer {
  position: relative;
  width: 250px;
}

.absolutely {
  position: absolute;
  background: #fb9;
}

.item {
  display:inline-block;
}
<div class="outerContainer">
    <div class="innerContainer">
        <div class="absolutely"> <!-- want this to extend beyond outerContainer width so items wrap less-->
          <div class="item">
              first item
          </div>
          <div class="item">
              second item
          </div>
          <div class="item">
              third item
          </div>
          <div class="item">
              fourth item
          </div>
        </div>
    </div>
</div>

关于html - 绝对定位的 div 动态扩展到相对父 div 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50032312/

相关文章:

html - 我无法将 div 与网页的中心对齐

html - 将 div 置于右上角的 div 内,不使用位置 :absolute

css - 获取浮点到 "float up"以填充空间

jquery - fancybox 2.0.6 ie7 不工作

jquery - 在非表格布局中将两个圆圈居中

javascript - Div 右对齐并根据右侧的其他 Div 更改 x 位置

java - 如何在 Java 中连接 XHTML 文件以解决任何可能的 CSS 冲突?

html - CSS 三列调整大小问题

html - 如果我将 <span> 放在新行上,星星会改变位置

html - 占位符 CSS 调用问题