I have a div
with a fixed width, which has a div
with text inside. Parts of the text are in a span
for coloring. The text div
has all necessary styles for text-overflow with dots at the end (ellipsis), but the dots are not inheriting the span
's color, because their definition is on the div
. When I put the definition on the span
, it ignores its parent's width.
Test code:
.container { width: 120px; } .text { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; } .color { color: #b02b7c; }
<div class="container"> <div class="text">Lorem <span class="color">ipsum dolor sit amet, consetetur</span> </div> <!-- works --> <div>Lorem <span class="text color">ipsum dolor sit amet, consetetur</span> </div> <!-- doesn't work --> </div>
Is there any clean CSS way to solve this problem? I'd like to stick with text-overflow: ellipsis;
, because the other solutions for text truncation are a bit messy in my opinion.
Referrent source at https://www.w3schools.com/cssref/css3_pr_text-overflow.asp
To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.
The Chicago Manual of Style calls for spaces between every ellipsis point. The AP Stylebook says to treat the ellipsis as a three-letter word, with spaces on either side of the ellipsis but no spaces between the dots. You can use either style; just be consistent throughout your document.
Definition and Usage The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.
If I understand your issue correctly, this might work for you:
.container { width:120px; background: lightgray; } .text { white-space: nowrap; text-overflow: ellipsis; overflow: hidden; color:#b02b7c; } .color { color: black; }
<div class="container"> <div class="text"><span class="color">Lorem</span> ipsum dolor sit amet, consetetur </div><!-- works --> </div>
Demo Fiddle
If the ellipsis is taking the color of the div, then make the div the color you want the ellipsis to be, and use .color
to set the initial text black.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With