In the example below I desire to get all items to be the width of largest content (e.g item 5).
The number of items in the list is dynamic and there could be hundred of them.
The width of each item content is also dynamic.
The number of items in each row would vary dependant on the width of the container.
DESIRED OUTPUT
Note: Media queries won't work for my scenario.
USING CSS FLEX
Flex
is considered to be one dimensional. This means it can achieve the desired result on a single axis (row) unless the content is smaller than the available space.
Non wrapping example (Single row)
.container {
display: flex;
}
.item {
background: #58c;
text-align: center;
white-space: nowrap;
margin: 0 10px 10px 0;
flex: 1;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5 is longer</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
</div>
Flex's flex-wrap
attribute can be used to create a two dimensional layout. Again this works nice but requires a fixed width value in order to calculate each items width.
Wrapping example (Multiple rows)
.container {
display: flex;
flex-wrap:wrap;
}
.item {
background: #58c;
text-align: center;
white-space: nowrap;
margin: 0 10px 10px 0;
flex: 0 1 100px;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5 is longer</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
USING CSS GRID
For two dimensional design, CSS Grid is recommended but the example below is using a fixed width. It seems like min-content
needs factoring in here, but how?
Failed example
grid-template-columns: repeat(auto-fit, min-content);
FIXED EXAMPLE (Multple rows)
.container {
display: grid;
grid-template-columns: repeat(auto-fit, 100px);
grid-auto-rows: 20px;
grid-gap: 5px;
}
.item {
background: #58c;
text-align: center;
white-space: nowrap;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5 is longer</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>
I do not think this is achievable with grid because of the following W3C note:
"Automatic repetitions (auto-fill or auto-fit) cannot be combined with intrinsic or flexible sizes."
Summary
Fundamentally I think there is no intrinsic way to do this with css.
To get the width of each item it needs to be:
- predefined (fixed).
- inferred horizontally using a percentage or fraction.
- inferred vertically from the current column.
I see no way to infer an items width from another row AND column simultaneously.