This is pretty annoying, but you cannot use flexbox for this. imho they completely threw the ball when they made the specification, because what you describe is the behavior that we all would like to have in 99% of cases, while the available space and the space between positions of a position in strange ways (in particular , between spaces) that just don't apply in the real world.
"An additional property of" flex-last-row: "to adjust the behavior of the last line in justified containers of flex space or between spaces would be really welcome. But this is only in our developerโs dream.
It has javascript hacks and even some very specific CSS hacks (such as "gazillion calcs and specific nth-childs"), but again, the whole reason for using flexbox should be to avoid re-hacks, as we should do with clearfixes by floats and font size: 0 on inline-block containers ...
The best way for me is to go with a CSS grid instead and apply
.fixed-grid--around{ grid-template-columns: repeat(auto-fill, minmax(150px,1fr)); justify-items: center; } .fixed-grid--between{ grid-template-columns: repeat(auto-fit, 150px); justify-content: space-between; } .grid-element{ width: 150px; }
By setting the minimum width of the grid columns to the width of their elements and the maximum column width to 1 fr, we can make them evenly distribute the space around them. For a space between style, autofit and space-between does the trick.
Thus, it adds columns to fill the container, evenly distributes the space between them until another column is added, and wraps as needed. That we always hoped for flexbox.
The pen I made before investigating the problem:
If you intend to use this, be sure to add a backup for browsers that do not support the grid. can be float, inline-blocks, flex or whatever. CSS Grid is really good at overriding backups , so it's pretty easy to apply.
Facundo corradini
source share