Using flex or some alternative, how can I achieve the following?
- Each item should be displayed side-by-side
- The width of each item should be as small as possible, but large enough so that each item is of equal width
- If all the items cannot fit on one row, the items should wrap appropriately
This is what I've tried so far: https://jsfiddle.net/nqyds03m/
.container {
display: flex;
width: max-content;
}
.item {
border: 1px solid gray;
margin: 0.25rem;
flex: 1;
flex-basis: 0;
}
<div class="container">
<div class="item">
An item with a lot of text indeed!
</div>
<div class="item">
Short item
</div>
</div>
The result is close enough, but not perfect. The items should be larger in the above example so that the text on the first item fits on one line, and the second item should grow to match the first.
I would like to avoid JavaScript if possible, but if a little JavaScript is necessary that is OK, as long as it remains performant like pure CSS.