I'm trying to create a horizontal navigation menu with flexible spacing between items. Flexbox seems like the appropriate choice, but I'm having trouble getting it working as I intend.
The requirements are:
- If there is enough space available, have 4rems of space between each item
- As the viewport width decreases, shrink the space between items evenly, down to a minimum of 2rem
- If the viewport width decreases further, keep 2rem space between and shrink the items themselves
I would like this to accommodate a variable number of items (there'll be a maximum).
The closest I got was using 2rem padding on the items, and then making the items themselves flex containers, with shrinking pseudo-elements, but the shrinking is quite uneven.
HTML
<div class="container">
<div class="item">
Item 1
</div>
<div class="item">
Item 2
</div>
<div class="item">
Item 3 With Long Name
</div>
<div class="item">
Item4WithLongNameNoSpaces
</div>
<div class="item">
Item 5
</div>
</div>
CSS
.container {
display: flex;
}
.item + .item {
display: flex;
padding-left: 2rem;
}
.item + .item:before {
content: "";
display: block;
width: 2rem;
flex-shrink: 1000;
}
Codepen: https://codepen.io/qubaji/pen/PoZLEbW
It seems like flexbox could do it if I could find the right combination of flex-grow/flex-shrink values, but I can't quite get there. Any help much appreciated!