Typiquement, une troncature d'une ligne est assez simple
.truncate-text {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
La troncature de deux lignes est un peu plus délicate, mais elle peut être réalisée avec css ; cet exemple est en sass.
@mixin multiLineEllipsis($lineHeight: 1.2rem, $lineCount: 2, $bgColor: white, $padding-right: 0.3125rem, $width: 1rem, $ellipsis-right: 0) {
overflow: hidden; /* hide text if it is more than $lineCount lines */
position: relative; /* for set '...' in absolute position */
line-height: $lineHeight; /* use this value to count block height */
max-height: $lineHeight * $lineCount; /* max-height = line-height * lines max number */
padding-right: $padding-right; /* place for '...' */
white-space: normal; /* overwrite any white-space styles */
word-break: break-all; /* will break each letter in word */
text-overflow: ellipsis; /* show ellipsis if text is broken */
&::before {
content: '...'; /* create the '...'' points in the end */
position: absolute;
right: $ellipsis-right;
bottom: 0;
}
&::after {
content: ''; /* hide '...'' if we have text, which is less than or equal to max lines and add $bgColor */
position: absolute;
right: 0;
width: $width;
height: 1rem * $lineCount;
margin-top: 0.2rem;
background: $bgColor; /* because we are cutting off the diff we need to add the color back. */
}
}