Home > Software design >  Overflow X with max two rows
Overflow X with max two rows

Time:02-01

I have question. How I can display this blue element only in two max rows. If I set flex-wrap to wrap, I can't control the number of rows. I want only max two rows, these blueitems will be scroll along the x axis. https://stackblitz.com/edit/react-btx8jz?file=src/style.css

CodePudding user response:

you can also use grid . but if you want to have only two rows , the child elements become overflowed

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

with flex you can do it to solve your problem , if the .parent has a certain height.

.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 >
  <div >
    <span >1 loremnxckvxckhv</span>
    <span >2 loremnxckvxckhv</span>
    <span >3 loremnxckvxckhv</span>
    <span >4 loremnxckvxckhv</span>
    <span >5 loremnxckvxckhv</span>
    <span >6 loremnxckvxckhv</span>
    <span >7 loremnxckvxckhv</span>
    <span >8 loremnxckvxckhv</span>
    <span >9 loremnxckvxckhv</span>
    <span >2 loremnxckvxckhv</span>
    <span >3 loremnxckvxckhv</span>
    <span >4 loremnxckvxckhv</span>
    <span >5 loremnxckvxckhv</span>
    <span >6 loremnxckvxckhv</span>
    <span >7 loremnxckvxckhv</span>
    <span >8 loremnxckvxckhv</span>
    <span >9 loremnxckvxckhv</span>
  </div>
</div>

CodePudding user response:

Change in width of box.

set width 750px or greater it

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

CodePudding user response:

.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 */
}
  •  Tags:  
  • Related