As demonstrated by the first 3 .shrink
elements in the included code snippet, I have styled an element such that:
- when its text is more narrow than its parent: the element is the same width as its text
- when its text is wider than its parent, the text wraps
- if the wrapped text fits within the parent, the element is the same width as its text, which is the same width as its parent
- if the wrapped text is still wider than the parent, the element is the same width as its text, and therefore wider than its parent
My issue is that I would also like to apply min-width: ${n}px
to such an element, so that all of the above statements apply, but the final width of the element must be at least the given minimum. This is somewhat demonstrated in the fourth and fifth (last two) .shrink
elements in the following example, but as you can observe, the fifth element does not work as intended. As with the fourth .shrink
element, the intent is that if its text were less than 90px wide, the box would still be 90px wide, but should also behave like the third .shrink
element when the text is wider than the parent. Intuitively, I want to express this as min-width: max(min-content, 90px)
, but this is not supported. Can this behavior be expressed with css without an additional wrapper element and if so, how?
.shrink {
width: max-content;
min-width: min-content;
max-width: 100%;
box-sizing: border-box;
}
<div style="display: flex; flex-direction: column; width: 100px; border: 4px solid orange;">
<div class='shrink' style="border: 4px solid lightblue;">narrow</div>
<div class='shrink' style="border: 4px solid lightblue;">this text wraps to fit</div>
<div class='shrink' style="border: 4px solid lightblue;">this text cannot_wrap_any_further</div>
<div class='shrink' style="border: 4px solid lightblue; min-width: 90px;">min 90px</div>
<div class='shrink' style="border: 4px solid lightblue; min-width: 90px;">min 90px this_box_should_be_as_wide_as_this_text!!</div>
</div>
A further point of clarification is that the parent should remain a flex column. Making it a wrapped flex row causes other problems, like negating y axis flex expressions (e.g. flex grow on the y axis) and could cause multiple children to be on the same row when they are narrow enough to fit.