Note: It's not just max-width:0
, it's any width less than the text content's width.
The mixture of display: table-cell
, max-width
, and width:100%
make an interesting situation that the specifications don't explicitly cover. However, by making some observations and thinking, we can understand why we have the same behavior across all major browsers.
max-width:0
tries to make the element width 0, of course, meaning that everything should be overflow. However, the combination of display: table-cell
and width:100%
, as seen in the linked demo, override that command for the div
(maybe for the same reason why max-width
does not apply to table rows) for the most part. As such, the element displays itself like it would without the max-width:0
until the width of the div
is no longer large enough to contain all of the text content.
A thing to note before we continue is that the text itself by default has a width that is set by the characters inside of it. It's kind of a child element of the parent div
, though there are no tags.
This innate width of the text content is the reason why max-width:0
is needed to create the effect. Once the width of the div
is no longer large enough to contain all of the content, the max-width:0
property enables the width to become less than the width of the text content, but forces the text that can no longer fit to become overflow of the div
itself. Thus, since the div
now has text overflow and text-overflow: ellipsis
is set, the text overflow is ellipsed (if that's a word).
This is very useful to us because otherwise we couldn't make this effect without a lot of messy JavaScript. Use case demo
Note: This answer describes the behavior and gives some insight as to why this is the case. It doesn't cover how display:table-cell
overrides part of the max-width
's effect.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…