winchendonsprings
winchendonsprings

Reputation: 491

Can I grow first item of an odd number of items in a flexbox rather than last

When I have an odd number of items the last item grows to fill the space.

Can I have the first item be the one that grows, rather than the last?

Example: codepen

Code below:

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  flex-flow: row wrap;
  -webkit-flex-flow: row wrap;
  align-content: stretch;
}
.flex-item {
  background: tomato;
  padding: 5px;
  width: 200px;
  height: 150px;
  margin-top: 10px;
  flex: 1 0 200px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="flex-container">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
  <li class="flex-item">7</li>
</ul>

Upvotes: 0

Views: 1109

Answers (3)

grinmax
grinmax

Reputation: 1855

Try this media

@media screen and(max-width: 800px){
  .flex-container .flex-item:first-child {
    min-width: 100%;
  }
}

Upvotes: 0

griz
griz

Reputation: 21

This does what you need:

.flex-container {
  flex-wrap: wrap-reverse;
  flex-direction: row-reverse;
}

This reverses the flex-wrap direction, which puts the stretched item first. Then flex-direction re-orders the items so that they are in their original order.

Upvotes: 0

kukkuz
kukkuz

Reputation: 42370

I don't think that is possible when you are wrapping the flexbox items. But here is a workaround of sorts if that will do- use wrap-reverse instead :

.flex-container {
  padding: 0;
  margin: 0;
  list-style: none;
  display: -webkit-box;
  display: -moz-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  flex-direction: row;
  flex-flow: row wrap-reverse;
  -webkit-flex-flow: row wrap-reverse;
  align-content: stretch;
}

.flex-item {
  background: tomato;
  padding: 5px;
  width: 200px;
  height: 150px;
  margin-top: 10px;
  flex: 1 0 200px;
  line-height: 150px;
  color: white;
  font-weight: bold;
  font-size: 3em;
  text-align: center;
}
<ul class="flex-container">
  <li class="flex-item">1</li>
  <li class="flex-item">2</li>
  <li class="flex-item">3</li>
  <li class="flex-item">4</li>
  <li class="flex-item">5</li>
  <li class="flex-item">6</li>
  <li class="flex-item">7</li>
</ul>

Upvotes: 1

Related Questions