Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set dot color of `text-overflow: ellipsis`

Tags:

css

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

like image 742
Demnogonis Avatar asked Feb 18 '14 17:02

Demnogonis


People also ask

How do you change text overflow on ellipsis?

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.

How do you style an ellipsis?

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.

What does text overflow ellipsis mean?

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.


1 Answers

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.

like image 142
badAdviceGuy Avatar answered Sep 19 '22 21:09

badAdviceGuy