溢出 X 最多两行

Overflow X with max two rows

我有问题。我如何仅在两个最大行中显示此蓝色元素。如果我将 flex-wrap 设置为 wrap,我将无法控制行数。我最多只需要两行,这些蓝色项将沿 x 轴滚动。 https://stackblitz.com/edit/react-btx8jz?file=src/style.css

框的宽度发生变化。

将宽度设置为 750px 或更大

.container {
  width: 750px;  // Change
  margin-left: auto;
  margin-right: auto;
  border: 2px solid;
}
.container {
  width: 300px;
  margin-left: auto;
  margin-right: auto;
  border: 2px solid;

  overflow-x: scroll;  /* Add */
}

.parent {
  display: flex;

  flex-wrap: wrap;     /* Add */
  width: max-content;  /* Add */
}

.child {
  margin: 5px;
  background-color: blue;

  flex-basis: 6%;  /* Add */
}

您也可以使用 grid 。但是如果你只想有两行,子元素就会溢出

.parent {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: 1fr 1fr;
  overflow-x: auto;
}

如果 .parent 有一定的 height,您可以用 flex 来解决您的问题。

.container {
  width: 600px;
  margin-left: auto;
  margin-right: auto;
  border: 2px solid;
  overflow-x: auto;
}

.parent {
  height: 100px;
  display: flex;
  flex-flow:column wrap;
  justify-content: space-between;
}

.child {
  margin: 5px;
  flex-basis: 35%;
  background-color: blue;
}
<div class="container">
  <div class="parent">
    <span class="child">1 loremnxckvxckhv</span>
    <span class="child">2 loremnxckvxckhv</span>
    <span class="child">3 loremnxckvxckhv</span>
    <span class="child">4 loremnxckvxckhv</span>
    <span class="child">5 loremnxckvxckhv</span>
    <span class="child">6 loremnxckvxckhv</span>
    <span class="child">7 loremnxckvxckhv</span>
    <span class="child">8 loremnxckvxckhv</span>
    <span class="child">9 loremnxckvxckhv</span>
    <span class="child">2 loremnxckvxckhv</span>
    <span class="child">3 loremnxckvxckhv</span>
    <span class="child">4 loremnxckvxckhv</span>
    <span class="child">5 loremnxckvxckhv</span>
    <span class="child">6 loremnxckvxckhv</span>
    <span class="child">7 loremnxckvxckhv</span>
    <span class="child">8 loremnxckvxckhv</span>
    <span class="child">9 loremnxckvxckhv</span>
  </div>
</div>