Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why flex elements are not going to next line when flex-basis is provided? [duplicate]

Tags:

html

css

flexbox

I am trying to add width to the flex elements. I have provided the flex-basis:20%. How can I get the 6th and 7th element in next row/line?

Adding the scratch pad link below: scratchpad.io

#wrapper {
  display: flex;
}
.children {
  flex-basis: 20%;
  border: 1px solid;
}
<div id="wrapper">
  <div class="children">
    1
  </div>
  <div class="children">
    2
  </div>
  <div class="children">
    3
  </div>
  <div class="children">
    4
  </div>
  <div class="children">
    5
  </div>
  <br>
  <div class="children">
    6
  </div>
  <div class="children">
    7
  </div>


</div>

Thanks in advance.

like image 917
freshtoUI Avatar asked Dec 15 '16 09:12

freshtoUI


People also ask

Why is my flex not wrapping?

If you are using flexbox and want the content to wrap, you must specify flex-wrap: wrap . By default flex items don't wrap. To have the images be three-across, you should specify flex-basis: 33.33333% .

How do you have content break to next line with flex when content reaches edge?

To have content break to next line with flex when content reaches edge with Flex React Native, we can set flexWrap to 'wrap' . to set flexDirection to 'row' to set the flexbox direction to horizontal. Then we set flexWrap to 'wrap' to make the child items wrap when it reaches the right edge of the screen.


1 Answers

Use the wrapping property - flex-wrap: wrap on your flexbox.

Even the horizontal margins affect wrapping - so set margin: 0 to body to reset the default browser margins and apply box-sizing: border-box to take care of the borders too.

See demo below:

* {
  box-sizing: border-box;
}
body {
  margin: 0;
}
#wrapper {
  display: flex;
  flex-wrap: wrap;
}
.children {
  flex-basis: 20%;
  border: 1px solid;
}
<div id="wrapper">
  <div class="children">1</div>
  <div class="children">2</div>
  <div class="children">3</div>
  <div class="children">4</div>
  <div class="children">5</div>
  <div class="children">6</div>
  <div class="children">7</div>
</div>
like image 136
kukkuz Avatar answered Oct 16 '22 14:10

kukkuz