I have a wrapping 100%-wide flexbox with a dynamic number of items of dynamic size. This is an example of what it may look like:
.container {
display: flex;
flex-wrap: wrap;
width: 350px;
gap: 0.75rem;
}
.container > div {
background: teal;
color: #f8f8f8;
font-family: sans-serif;
display: inline-block;
padding: .25em .4em;
line-height: 1;
text-align: center;
white-space: nowrap;
font-weight: bold;
border-radius: .25rem;
}
<div class="container">
<div>Lorem ipsum</div>
<div>dolor sit amet</div>
<div>consectetur adipiscing elit</div>
<div>sed do eiusmod</div>
<div>tempor incididunt</div>
<div>ut labore</div>
<div>et dolore</div>
</div>
I want the last item (or "column") in each row to expand its width to fill the remaining empty space in that row. The result should look like this:
.container {
display: flex;
flex-wrap: wrap;
width: 350px;
gap: 0.75rem;
}
.container > div {
background: teal;
color: #f8f8f8;
font-family: sans-serif;
display: inline-block;
padding: .25em .4em;
line-height: 1;
text-align: center;
white-space: nowrap;
font-weight: bold;
border-radius: .25rem;
}
<div class="container">
<div>Lorem ipsum</div>
<div style="flex-grow:1">dolor sit amet</div>
<div style="flex-grow:1">consectetur adipiscing elit</div>
<div>sed do eiusmod</div>
<div style="flex-grow:1">tempor incididunt</div>
<div>ut labore</div>
<div style="flex-grow:1">et dolore</div>
</div>
Is there a way to achieve this with pure CSS? Please note that the number of items is dynamic, as well as their width and the overall width of the container. The solution does not necessarily have to involve a flexbox. A grid-based approach or something else would also be fine.
Edit for clarification in response to Johannes' answer (which now doesn't exist anymore):
I do not know in advance which item is going to be the last one in each row because that depends on the number of items, the width of each item, and the width of the container, all of which I do not know in advance. Therefore adding flex-grow:1
to particular items is not possible.