I try to build a dynamic toolbar where:
- The number of tools is dynamic
- All tools should have the same width (based on the widest one)
- Tools can be separated by a separator that takes all the space available (stretched)
- The separator can be placed anywhere
- The html can't be changed
Expected output (given BBB the widest tool):
—————————————————————————————————————————————————————
| A |BBB| CC| SEPARATOR | D |
—————————————————————————————————————————————————————
Flex
I tried with the flex method, I can't combine all the rules:
- Either the separator takes all the space but tools width are not equal:
—————————————————————————————————————————————————————
|A|BBB|CC| SEPARATOR |D|
—————————————————————————————————————————————————————
nav {
display: flex;
background: #e8e8e8;
width: 100%;
}
.item {
flex: 1;
text-align: center;
}
.separator {
width: 100%;
background: #d3d3d3;
}
<nav>
<div class="item">A</div>
<div class="item">BBB</div>
<div class="item">CC</div>
<div class="separator"></div>
<div class="item">D</div>
</nav>
- Either all tools (including the separator) have the same width:
—————————————————————————————————————————————————————
| A | BBB | CC | SEPARATOR | D |
—————————————————————————————————————————————————————
nav {
display: flex;
background: #e8e8e8;
width: 100%;
}
.item {
flex: 1;
text-align: center;
}
.separator {
flex: 1;
background: #d3d3d3;
}
<nav>
<div class="item">A</div>
<div class="item">BBB</div>
<div class="item">CC</div>
<div class="separator"></div>
<div class="item">D</div>
</nav>
Grid
With the grid system, I can't get the separator without specifying a grid-template-columns, which I want to avoid. I need something dynamic.
nav {
display: grid;
grid-auto-columns: minmax(0, 1fr);
grid-auto-flow: column;
background: #e8e8e8;
width: 100%;
}
.item {
text-align: center;
}
.separator {
justify-self: stretch;
background: #d3d3d3;
}
<nav>
<div class="item">A</div>
<div class="item">BBB</div>
<div class="item">CC</div>
<div class="separator"></div>
<div class="item">D</div>
</nav>
I'm open to JavaScript solutions if there is no CSS solution. Thank you for your help.