I have in some texts of a Chip
some very long texts. I have as well a button on the end of the chip
to remove the chip.
I thought I can apply text-overflow: ellipsis
for the text but somehow this doesn’t help.
Currently it looks in my sample like
but I want to have look like
2
Answers
Ellipsis works if:
width
is set inpx
andoverflow: hidden;
andwhite-space: nowrap;
.See the live demo.
HTML:
CSS:
Output:
Setting a fixed width was not an option for me as the interface needed to be fluid.
So I did some digging into the source code and found that the chip text label already has the CSS rules needed for truncating text, but this behaviour is overwritten for some reason
(the details and code references are below the solution):
Solution
As a workaround, we have to overwrite the rules, by the following css provided at:
a) at component level:
Example – Parent container that takes all the screen width: https://stackblitz.com/edit/tc5xcb-fvpbyu
Example – Parent container with fixed width: https://stackblitz.com/edit/tc5xcb-yfytxs
OR
b) in global
styles.css
file:Example: https://stackblitz.com/edit/tc5xcb-jrdkjw
Details
(the code is in https://github.com/material-components/material-components-web/blob/v14.0.0/packages/mdc-chips/_chip.scss#L215):
But the behaviour is overwritten in Angular Material source code (v16.0.2), https://github.com/angular/components/blob/16.0.2/src/material/chips/chip.scss#L131:
There is also an issue opened on Github – https://github.com/angular/components/issues/26584.
I will follow up on this and update the answer accordingly when the issue gets resolved.