with
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
如果溢出,“…”将显示在行末。
但是,这将只在一行中显示。
但我希望它能多行显示。
它可能看起来像:
+--------------------+
|abcde feg hij dkjd|
|dsji jdia js ajid s|
|jdis ajid dheu d ...|/*Here it's overflowed, so "..." is shown. */
+--------------------+
谢谢@balpha和@Kevin,我把两种方法结合在一起。
这个方法不需要js。
你可以使用背景图像和不需要梯度隐藏点。
.ellipsis-placeholder的innerHTML不是必需的,我使用.ellipsis-placeholder来保持与.ellipsis-more相同的宽度和高度。
你可以使用display: inline-block代替。
.ellipsis {
overflow: hidden;
position: relative;
}
.ellipsis-more-top {/*push down .ellipsis-more*/
content: "";
float: left;
width: 5px;
}
.ellipsis-text-container {
float: right;
width: 100%;
margin-left: -5px;
}
.ellipsis-more-container {
float: right;
position: relative;
left: 100%;
width: 5px;
margin-left: -5px;
border-right: solid 5px transparent;
white-space: nowrap;
}
.ellipsis-placeholder {/*keep text around ,keep it transparent ,keep same width and height as .ellipsis-more*/
float: right;
clear: right;
color: transparent;
}
.ellipsis-placeholder-top {/*push down .ellipsis-placeholder*/
float: right;
width: 0;
}
.ellipsis-more {/*ellipsis things here*/
float: right;
}
.ellipsis-height {/*the total height*/
height: 3.6em;
}
.ellipsis-line-height {/*the line-height*/
line-height: 1.2;
}
.ellipsis-margin-top {/*one line height*/
margin-top: -1.2em;
}
.ellipsis-text {
word-break: break-all;
}
<div class="ellipsis ellipsis-height ellipsis-line-height">
<div class="ellipsis-more-top ellipsis-height"></div>
<div class="ellipsis-text-container">
<div class="ellipsis-placeholder-top ellipsis-height ellipsis-margin-top"></div>
<div class="ellipsis-placeholder">
<span>...</span><span>more</span>
</div>
<span class="ellipsis-text">text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text </span>
</div>
<div class="ellipsis-more-container ellipsis-margin-top">
<div class="ellipsis-more">
<span>...</span><span>more</span>
</div>
</div>
</div>
斯菲德勒
这里有很多答案,但我需要的是:
CSS只
面向未来(与时间更加兼容)
不会把单词分开(只在空格上分开)
需要注意的是,它不为不支持-webkit-线夹规则的浏览器(目前是IE, Edge, Firefox)提供省略号,但它确实使用渐变淡出文本。
.clampMe {
position: relative;
height: 2.4em;
overflow: hidden;
}
.clampMe:after {
content: "";
text-align: right;
position: absolute;
bottom: 0;
right: 0;
width: 50%;
height: 1.2em; /* Just use multiples of the line-height */
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1) 80%);
}
/* Now add in code for the browsers that support -webkit-line-clamp and overwrite the non-supportive stuff */
@supports (-webkit-line-clamp: 2) {
.clampMe {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.clampMe:after {
display: none;
}
}
<p class="clampMe">There's a lot more text in here than what you'll ever see. Pellentesque habitant testalotish morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>
你可以在这个CodePen中看到它的运行,你也可以在这里看到Javascript版本(没有jQuery)。