I want to create this layout:
When an item doesn't fit in the container, we can move to the next line:
When the container is tiny than the wider item, we can wrap the content in multilines
It's very easy with Javascript, here is the example https://jsfiddle.net/oucxsep4/.
var choices = document.querySelectorAll('li');
var maxWidth = 0;
// read
for (i = 0; i < choices.length; ++i) {
maxWidth = Math.max(maxWidth, choices[i].offsetWidth)
};
// write
for (i = 0; i < choices.length; ++i) {
choices[i].style.width = maxWidth + "px";
};
ul{
margin: 0;
padding: 0;
list-style: none;
}
li{
background: red;
float: left;
margin: 5px;
}
<ul>
<li>first choice</li>
<li>choice</li>
<li>This is the wider choice</li>
<li>other choice</li>
</ul>
Is it possible to do it without using Javascript, only CSS? I have tried with flexbox
without success.