I am trying to hide overflow in a label that is the child of a deeply nested flex-container. Below are two simple snippets to illustrate the problem:
Single container (works):
#parent-flexbox {
display: flex;
width: 200px;
}
label {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div id="parent-flexbox">
<label>long text that should be hidden because it is wider than the parent container</label>
</div>
Nested container (doesn't work):
#parent-flexbox {
width: 200px;
}
#parent-flexbox, #child-flexbox {
display: flex;
}
label {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<div id="parent-flexbox">
<div id="child-flexbox">
<label>long text that should be hidden because it is wider than the parent container</label>
</div>
</div>
To solve this I could add an explicit width like max-width: 100%
to every container in the hierarchy. This is a terrible solution that I'm hoping is unnecessary! My current best fitting solution, but not much better, is these two rules for the immediate parent of the label: {position: absolute; max-width: 100%}
. I want to avoid using absolute position because this comes with problems of its own. This JSFiddle shows the absolute position solution (lines outcommented default).
How can I achieve the same as this JSFiddle without A) using position: absolute
and B) targetting all containers in the hierachy to give them max-width: 100%
or min-width: 0
?