Flexbox Truncating
그러니까 as long as I understand, 문제가 뭐였냐하면은 만약에 truncate 먹일 친구들이 flex-container의 direct-child 면 문제가 되지 않는다. 그런데 문제가 되는 것은 direct-child가 아니고 한 번 더 들어갈 때 문제가 되는 것 같다. 아래의 예시를 보면… 직계자식인 애는 제대로 동작을 하는데 한 번 더 들어가있는 자식은 동작을 안한다.
이유가 무엇일까? 그리고 해결방법은?
이유
이유를 알기 위해서는 먼저 flex item의 기본동작 방식에 대해서 알아야 합니다.
Note: The auto keyword, representing an automatic minimum size, is the new initial value of the min-width and min-height properties. The keyword was previously defined in this specification, but is now defined in the CSS Sizing module.
To provide a more reasonable default minimum size for flex items, the used value of a main axis automatic minimum size on a flex item that is not a scroll container is a content-based minimum size; for scroll containers the automatic minimum size is zero, as usual.
이 말이 어떤 말이냐면, flex item의 최소 사이즈는 flex item이 scroll container가 아닌 경우에는 자식들의 컨텐츠 사이즈(content-based minimum size)가 될 것이라는 것입니다.
MDN의 min-width 를 참고하세요.
The default value. The source of the automatic value for the specified element depends on its display value. For block boxes, inline boxes, inline blocks, and all table layout boxes auto resolves to 0. For flex items and grid items, the minimum width value is either the specified suggested size, such as the value of the width property, the transferred size, calculated if the element has an aspect-ratio set and the height is a definite size, otherwise, the min-content size is used. If the flex or grid item is a scroll container, or if a grid item spans more than one flexible column track, the automatic minimum size is 0.
자 그러면 우리는 이제 이유를 알 수 있죠. truncate해야할 대상이 직계자신이 아닌 경우에 그 대상은 flex item을 부모로 가지게 되는데요. truncate해야할 대상에 대해서는 white-space: nowrap을 걸어놨으니, text의 길이만큼 width가 길어지고, flex item은 그 text의 길이보다 짧아질 수는 없는 것이죠.
해결방법
그러면 어떻게 해야할까요? flex item이 text의 길이만큼을 초기값으로 가져가게 되니까요. 저는 Flexbox And Truncated Text | CSS-Tricks에서 정답을 찾았습니다. 문제가 되는 것이 min-width가 text의 길이를 초기값으로 가지는 것이니 min-width: 0 이나 min-width: <length> 를 먹이고 flex-grow: 1 을 해주면, 일단 넓이는 가질 것이고, min-width도 text의 길이값이 되지 않으니 제대로 처리가 되는 것이죠.
참조
- Flexbox And Truncated Text | CSS-Tricks (practical 한 해결방법 제시)
- https://developer.mozilla.org/en-US/docs/Web/CSS/min-width#auto (flex item에서는 min-width가 어떻게 동작하는지 설명)
- flex item에 text-overflow: ellipsis 속성 적용하기 (정말 딥하게 설명 잘 되어 있음)