It's possible that there are already solutions or examples for this, but I have been unable to find one.
I want to create a menu that has a varying number of menu items, each with different widths (depending on their content). If there are too many menu items I would want them to take up more than one rows.
My first solution was a simple float. It was fine, but at the end of the rows there is an uneven unoccupied space:
Disregard that the "Text" on the items are the same width. I just suck at drawing. Imagine that the texts have varying length but the same padding around them.
With a tricky border (border only on the left side of items) I'm able to hide this a little, but the clickable area is still the same as the white parts of the picture above. A hover effect also unmasks the trick.
Then I have done a little research about text-align justify and found solutions like this: "text-align: justify;" inline-block elements properly?
For first glimpse this is just what I want. The elements are aligning properly, they fill the available space at first sight. But there is a fundamental flaw: they have gaps in between them:
This is expected since this treats my elements like words in a text. I have found no way to fix this.
Later I have started looking around in flexbox land. This looks promising flexbox seems to be the holy grail of css, but I haven't been able to wrap my head around it yet.
Basically what I want is this:
The buttons fill up the available space, the menu is multiline (when there is too much elements for one line), the entire menu is clickable (with visible hover effect).
Is there a way to do this?
I'm hesitant to use javascript powered solutions (like calculating and adjusting widths of elements). But if there is a very clean solution for this, I might end up using it.
EDIT:
The menu here looks nice, but only if the menu is single line. This hacks the last element by adding it to a different list and by adding a lot of space with an invisible :after { content ". . ." }
element.