3

I wanted my flex items to have an equal size without setting its width.
I know it can be done by setting the flex item to flex: 1;.
However, I also wanted to break the last item into the next line but keep its width equal to the other item, now I'm stuck.

How can I do that?

ul{
  padding: 0;
  margin: 0;
  list-style-type: none;
  display: flex; 
  flex-flow: row wrap;
}
li{
  flex: 1;
  border: solid 1px;
  text-align: center;
  flex-basis: calc(100% / 7);
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li class="keep-next-line">7</li> <!--Kepp this next line, but size should be equal to other item-->
</ul>
Polar
  • 3,327
  • 4
  • 42
  • 77

1 Answers1

2

You should set flex-grow:0 and should be calc(100% / 6) instead of calc(100% / 7). Also I added box-sizing:border-box; for borders to be included in width.

ul {
  padding: 0;
  margin: 0;
  list-style-type: none;
  display: flex;
  flex-flow: row wrap;
}

li {
  flex: 1;
  border: solid 1px;
  text-align: center;
  flex-basis: calc(100% / 6);
  box-sizing: border-box;
}

.keep-next-line {
  flex-grow: 0;
}
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li class="keep-next-line">7</li>
  <!--Kepp this next line, but size should be equal to other item-->
</ul>
doğukan
  • 23,073
  • 13
  • 57
  • 69