I'm trying to lay out a tab bar in HTML / CSS but am having trouble implementing my desired layout. It is:
- no tab element should ever be larger than needed
- if there is not enough space, larger elements should be shrunk before smaller elements
Essentially, I'd like it to be similar to "5 tabs? max-width: 20%" except where if one tab is very short, another could claim its space.
The closest I've gotten is with flex: 0 1 auto and setting min-width to 0. When there's more space than needed, this works fine:
However, when there is insufficient space, the tabs are shrunk according to their initial width ratios:
This is very close to what I'm trying to accomplish. However, each tab loses width according to its initial size ratio, meaning the first tab (whose contents is short) gets undesirably shortened.
How can I implement a row of elements such that width is taken from the larger elements evenly before width is taken from the smaller elements? I am hoping to accomplish this without javascript if at all possible.
For completeness, the code which drives the almost correct screenshots above is:
.nav-tabs {
display: flex;
}
.nav-tabs li {
flex: 0 1 auto;
min-width: 0;
max-width: auto;
}
.nav-tabs li a {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.nav-tabs .manage-tabs {
min-width: auto;
width: auto;
flex-basis: 0;
margin-left: auto;
}
with the following HTML, using Twitter Bootstrap 2.3.2 for basic styles:
<ul class="nav nav-tabs">
<li>
<a href="...">short1</a>
</li>
<li>
<a href="...">This is very long wwwwwww</a>
</li>
<li class="active">
<a href="...">Also very long wwwwwwwwww</a>
</li>
<li>
<a href="...">wwwwwwwwwwwwwwwwwwwwwwwww</a>
</li>
<li>
<a href="...">awwwwwwwwwwwwwwwwwwwwwwww</a>
</li>
<li>
<a href="...">bwwwwwwwwwwwwwwwwwwwwwwww</a>
</li>
<li class="manage-tabs">
<a href="...">Manage Pockets</a>
</li>
</ul>

