I have several items laid out horizontally that should:
- Always stay in a single row (never break to new lines).
- Not be a larger width than their content or their max-width, whichever is smaller.
- Truncate with ellipses when max-width limits their width.
- Not be a smaller width than their min-width.
These items share the same parent. If the parent is too small in width to contain the items using the above rules, then the items should shrink width-wise but never get thinner than their min-width.
EDIT: In other words, if the parent is larger than the total width of the children, that's fine. The children should not expand to fill the parent. If the parent becomes smaller than the total width of the children as numbers 1, 2, 3, and 4 dictate, the children should shrink to stay within the new parent width. An example of this (and actually what I'm trying to reproduce) can be seen in Chrome's browser tabs. The tabs have a max-width and a min-width. If you start shrinking the browser width-wise then it will get to a point where the tabs will no longer be able to fit so they start shrinking until they hit their min-width.
I've nailed down numbers 1, 2, 3, and 4 but I don't know how to get them to shrink to stay within the parent width. Any help would be much appreciated.
Example HTML:
<div class="container">
<div class="tab"><span>Bookmark</span></div><div class="tab"><span>Finish</span></div><div class="tab"><span>A</span></div><div class="tab"><span>Kala kazoo A Roosky Fantastic</span></div>
</div>
Example CSS:
.container {
border: 1px solid blue;
white-space: nowrap;
}
.tab {
display: inline-block;
border: 1px solid red;
}
.tab span {
display: inline-block;
width: 100%;
min-width: 20px;
max-width: 100px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
JSFiddle: http://jsfiddle.net/ug3en/
Target browsers: IE9+, modern Chrome, modern Firefox, and modern Safari.