【问题标题】:Full width of relative positioned overflowing flex container相对定位的溢出 flex 容器的全宽
【发布时间】:2018-02-12 03:14:04
【问题描述】:

我试图通过将leftright 都设置为0absolute 定位子元素来在水平溢出的flex 容器上实现一条线,这也意味着容器必须有一个relative 位置包含绝对定位元素。这就是麻烦的开始。

因为容器有一个relative的位置,所以children的right值不在滚动视图的边缘,它在溢出开始的地方停止。

CodePen example

HTML 代码:

<div>
  <div>Content</div>
  <div>Content</div>
  <div>Content</div>
</div>

CSS 代码:

body > div {
  display: flex;
  overflow-x: scroll;
  position: relative;
}

body > div::before {
  content: '';
  position: absolute;
  top: 150px;
  left: 0;
  right: 0;
  height: 4px;
  width: 100%;
  background: #000;
}

div > div {
  align-items: stretch;
  position: relative;
  min-width: 900px;
  height: 300px;
}

是否可以将子元素扩展到整个元素,包括溢出?

【问题讨论】:

标签: html css flexbox overflow


【解决方案1】:

您可以通过使用inline-flex 而不是flex 来完成此操作

Updated codepen

堆栈sn-p

body > div {
  display: inline-flex;                /*  changed  */
  overflow-x: scroll;
  position: relative;
}

body > div::before {
  content: '';
  position: absolute;
  top: 150px;
  left: 0;
  right: 0;
  height: 4px;
  width: 100%;
  background: #000;
}

div > div {
  align-items: stretch;
  position: relative;
  min-width: 900px;
  height: 300px;
}
<div>
  <div>Content</div>
  <div>Content</div>
  <div>Content</div>
</div>

或者,您可以添加具有inline-block 的外包装

body > div {
  display: inline-block;
}

body > div > div {
  display: flex;
  overflow-x: scroll;
  position: relative;
}

body > div > div::before {
  content: '';
  position: absolute;
  top: 150px;
  left: 0;
  right: 0;
  height: 4px;
  width: 100%;
  background: #000;
}

body > div > div > div {
  align-items: stretch;
  position: relative;
  min-width: 900px;
  height: 300px;
}
<div>
  <div>
    <div>Content</div>
    <div>Content</div>
    <div>Content</div>
  </div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    • 2020-12-23
    • 2019-01-09
    • 2021-05-07
    • 1970-01-01
    相关资源
    最近更新 更多