Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use css to show on hover tool-tips using the same text which is shortend with ellipses?

Tags:

html

css

I have the example where some of the strings are too long and I want them shortened. However, I want to be able to see the entire string when I hover my mouse over it.

.don_single_donatori {
  width: 250px;
  min-height: 310px;
}

.overview span {
  text-overflow: ellipsis;
  width: 100px;
  white-space: nowrap;
  overflow: hidden;
  display: inline-block;
}

.overview em {
  font-style: normal;
  color: #000;
  float: right;
}
<div class="don_single_donatori">
  <div class="viewport">
    <div class="overview">
      <p><span>This is my full name which should be shortend</span><em>2.000,00 EUR</em></p>
      <p><span>Anonymous</span><em>2.000,00 EUR</em></p>
      <p><span>Anonymous</span><em>500,00 EUR</em></p>
      <p><span>This is another long name that needs shortening</span><em>2.000,00 EUR</em></p>
      <p>Anonymous<em>2.000,00 EUR</em></p>
    </div>

Most approaches I found online use two strings, i.e. the text that you hover over with a mouse is seperate from the text you use for a tooltip (like in the post here). But in my case I have the same text. Having two entries for the same text seems redundant.

like image 574
magasr Avatar asked Dec 21 '25 10:12

magasr


1 Answers

Can't you just use the title attribute of the span???

<style>
.don_single_donatori {
  width: 250px;
  min-height: 310px;
}

.overview span {
  text-overflow: ellipsis;
  width: 100px;
  white-space: nowrap;
  overflow: hidden;
  display: inline-block;
}

.overview em {
  font-style: normal;
  color: #000;
  float: right;
}
</style>
<div class="don_single_donatori">
    <div class="viewport">
        <div class="overview">
            <p><span title="This is my full name which should be shortend">
            This is my full name which should be shortend
            </span><em>2.000,00 EUR</em></p>
            <p><span>Anonymous</span><em>2.000,00 EUR</em></p>
            <p><span>Anonymous</span><em>500,00 EUR</em></p>
            <p><span title="This is another long name that needs shortening">
            This is another long name that needs shortening
            </span><em>2.000,00 EUR</em></p>
            <p>Anonymous<em>2.000,00 EUR</em></p>
        </div>
    </div>
</div>

If you don't want duplicate text, use a CSS selector so that if the span has a title, use the title for content. Then just remove the content in spans with a title as so:

<style>
.don_single_donatori {
    width: 250px;
    min-height: 310px;
}

.overview span {
    text-overflow: ellipsis;
    width: 100px;
    white-space: nowrap;
    overflow: hidden;
    display: inline-block;
}

.overview em {
    font-style: normal;
    color: #000;
    float: right;
}
span[title]::before {
    content: attr(title);
}
</style>
<!-- ... -->
<div class="don_single_donatori">
    <div class="viewport">
        <div class="overview">
            <p><span title="This is my full name which should be shortend">
            </span><em>2.000,00 EUR</em></p>
            <p><span>Anonymous</span><em>2.000,00 EUR</em></p>
            <p><span>Anonymous</span><em>500,00 EUR</em></p>
            <p><span title="This is another long name that needs shortening">
            </span><em>2.000,00 EUR</em></p>
            <p>Anonymous<em>2.000,00 EUR</em></p>
        </div>
    </div>
</div>
like image 109
Popmedic Avatar answered Dec 23 '25 22:12

Popmedic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!