我有一个例子,其中一些字符串太长,我希望将它们缩短。但是,我希望当我将鼠标悬停在字符串上时能够看到整个字符串。
.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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
我在网上找到的大多数方法都使用两个字符串,即您用鼠标悬停在其上的文本与您用于工具提示的文本是分开的(如此处的帖子中所示)。但就我而言,我有相同的文本。为同一文本设置两个条目似乎是多余的。
不能只使用span的title属性吗???
<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>Run Code Online (Sandbox Code Playgroud)
如果您不想要重复的文本,请使用 CSS 选择器,这样如果跨度有标题,则使用该标题作为内容。然后只需删除带有标题的 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;
}
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>Run Code Online (Sandbox Code Playgroud)