Home > Enterprise >  CSS Flexbox: grow first flex items instead of last ones
CSS Flexbox: grow first flex items instead of last ones

Time:10-07

I have a flex-boxes layout that grows the boxes in the final "row", like here:

Screenshot

The CSS:

.container {
    display: flex;
    flex-flow: row wrap;
    align-items: stretch;
}

.item {
    flex-grow: 1;
    flex-shrink: 0;
    display: inline-block;

    position: relative; width: 14em; height: 14em; min-width: 14em;
}

The question: can one indicate in such a row flow to preferably grow the "first" flex-items rather than the "last" ones (via CSS not JS)?

CodePudding user response:

You can add flex-wrap: wrap-reverse; in the container class, and in your html add the elements in reverse order

.container {
    display: flex;
    flex-flow: row wrap;
    align-items: stretch;
    flex-wrap: wrap-reverse;
}
<div >
    <div >5</div>
    <div >4</div>
    <div >3</div>
    <div >2</div>
    <div >1</div>
</div>
  • Related