Position wrapped flex items prefer to group/bunch at the end of the container

Agendum

I have a simple flex container. For example:

.container {
  display: flex;
  flex-wrap: wrap;
  outline: dashed red;
  width: 300px;
}

.container > div {
  width: 100px;
  display: flex;
}

.container > div:nth-child(even) { 
  background-color: yellow; 
} 

.container > div:nth-child(odd) { 
  background-color: cyan; 
} 
<div class="container">
  <div>Item1</div>
  <div>Item2</div>
  <div>Item3</div>
  <div>Item4</div>
  <div>Item5</div>
  <div>Item6</div>
  <div>Item7</div>
  <div>Item8</div>
  <div>Item9</div>
  <div>Item10</div>
</div>

Using the row widths of this specific example, flex by default causes the first row to contain items 1, 2, and 3, and the last row to contain just 10. But I want the opposite of this, something like this:

1
2  3  4
5  6  7
8  9  10

If I use wrap-reverse, the behavior looks close to something I want:

10
7  8  9
4  5  6
1  2  3

The problem is that it just shifts the rows, and not the items in it also. Notice 1 is no longer the last one, so reversing is not an option. For example, if I couple wrap-reverse and use the order style to reverse the items of my order, it ends up being:

1
4  3  2
7  6  5
10 9  8

If I don't use wrap-reverse, the items are always grouped/bunched to the front.

Ideally I don't need to manually decorate the items with a specific order. The list of items is dynamic and comes from a CMS and I don't really have visibility into the items getting added from my component.

Is there any way to accomplish the layout behavior I am looking for?

---- edit -----

Sorry I left details out. I only used widths here to illustrate how flex is arranging items. In reality the size of both the container and the items may change, so I cannot take a dependency on fixed widths or column counts. Additionally I don't know the total number of items in advance as they are sourced from a CMS.

Michael Benjamin

To have a dynamic list of elements flow in their source order but start filling rows from the last column of the last row is a task I don't believe CSS is advanced enough yet to accomplish. At least not without serious hackery of both the CSS and HTML.

Your best bet in my view would be to leave the HTML and CSS as is (it looks clean and good) and add a script to finish up the layout.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Position last flex item at the end of container

From Dev

Flex: wrapped items with same width as the rest

From Dev

How to position –THIS– in a Flex-container?

From Dev

stack flex items to bottom of flex container

From Dev

How to overlay a flex container with fixed position over another flex container?

From Dev

How can I make a flexbox container that stretches to fit wrapped items?

From Dev

Align flex items with different heights in the same container

From Dev

Prevent flex items from overflowing a container

From Dev

How to align the items to the start and center the flex container?

From Dev

scrolling flex container does not fit centered items

From Dev

CSS flex - Align items to left, and container to center

From Dev

Right-align items in a flex container

From Dev

Using position: relative inside a flex container

From Dev

Center flex items with one at the end of the row

From Dev

Move single element to the end of a flex container

From Dev

How to center one of two flex items in a flex container?

From Java

How to center a flex container but left-align flex items

From Dev

Center flex item in container, when surrounded by other flex items

From Dev

Is it possible to animate position of flex items when items are removed/added?

From Dev

Adding bullet points spaced evenly between flexbox items (but only between items, not at beginning or end of wrapped rows)

From Dev

How to keep wrapped flex-items the same width as the elements on the previous row?

From Dev

How can I make a display:flex container expand horizontally with its wrapped contents?

From Dev

How can I make a display:flex container expand horizontally with its wrapped contents?

From Java

Make flex items take content width, not width of parent container

From Dev

How to create "collapsed" borders around flex items and their container?

From Dev

Make flex items on second row take remaining height of container

From Dev

Change justify-content value when flex items overflow container

From Dev

Allow flex items to keep fixed width and scroll horizontally inside container

From Dev

Position site title between two columns of flex items

Related Related

  1. 1

    Position last flex item at the end of container

  2. 2

    Flex: wrapped items with same width as the rest

  3. 3

    How to position –THIS– in a Flex-container?

  4. 4

    stack flex items to bottom of flex container

  5. 5

    How to overlay a flex container with fixed position over another flex container?

  6. 6

    How can I make a flexbox container that stretches to fit wrapped items?

  7. 7

    Align flex items with different heights in the same container

  8. 8

    Prevent flex items from overflowing a container

  9. 9

    How to align the items to the start and center the flex container?

  10. 10

    scrolling flex container does not fit centered items

  11. 11

    CSS flex - Align items to left, and container to center

  12. 12

    Right-align items in a flex container

  13. 13

    Using position: relative inside a flex container

  14. 14

    Center flex items with one at the end of the row

  15. 15

    Move single element to the end of a flex container

  16. 16

    How to center one of two flex items in a flex container?

  17. 17

    How to center a flex container but left-align flex items

  18. 18

    Center flex item in container, when surrounded by other flex items

  19. 19

    Is it possible to animate position of flex items when items are removed/added?

  20. 20

    Adding bullet points spaced evenly between flexbox items (but only between items, not at beginning or end of wrapped rows)

  21. 21

    How to keep wrapped flex-items the same width as the elements on the previous row?

  22. 22

    How can I make a display:flex container expand horizontally with its wrapped contents?

  23. 23

    How can I make a display:flex container expand horizontally with its wrapped contents?

  24. 24

    Make flex items take content width, not width of parent container

  25. 25

    How to create "collapsed" borders around flex items and their container?

  26. 26

    Make flex items on second row take remaining height of container

  27. 27

    Change justify-content value when flex items overflow container

  28. 28

    Allow flex items to keep fixed width and scroll horizontally inside container

  29. 29

    Position site title between two columns of flex items

HotTag

Archive