【发布时间】:2019-06-27 02:29:23
【问题描述】:
是否可以为text-overflow: ellipsis 上的点设置样式?
例如将省略号加粗,例如“Lorem Ips ...”
【问题讨论】:
-
我建议使用
::after。
是否可以为text-overflow: ellipsis 上的点设置样式?
例如将省略号加粗,例如“Lorem Ips ...”
【问题讨论】:
::after。
受this answer 的启发,这里是省略号样式的一种方式。 该解决方案的缺点是
<span>(或其他)元素.text-overflow
{
color: blue;
font-weight: bold;
font-size: 20px;
overflow: hidden;
text-overflow: ellipsis;
width: 100px;
white-space: nowrap;
}
.text-overflow > span
{
font-weight: normal;
color: black;
font-size: 14px;
}
<div class="text-overflow"><span>Here is some very nice text indeed. So this is getting long</span></div>
【讨论】:
text-overflow 的重点。它类似于普通的overflow 属性,但它针对的是比其父元素更宽的文本节点(因此是white-space: nowrap)。您可以选择如何处理溢出的文本,其中一个选项是在开头或结尾显示“...”。 Here is a mdn article 谢谢你的回复 :)
text-overflow: ellipsis; 属性添加...,因此您不需要::after。
:P
我建议使用:after,但这也可以代替那些没有省略号的人。
.truncate {
width: 250px;
white-space: nowrap;
overflow: hidden;
position: relative;
}
.truncate::after {
display: block;
font-weight: bold;
content: "...";
position: absolute;
bottom: 0;
right: 0;
background: #fff;
}
<div class="truncate">You may be starting to notice a trend from my recent articles here on HTML5 Hub. I’m a JS snob by trade, but by association I have to deal with my fair share of CSS issues. So, as an “outsider” looking in, I’m taking this opportunity to highlight some of the big “pain points” that CSS causes for various basic tasks in web dev. It’s not really intended as a criticism of the technology per se, but more a rallying cry to ask for standard solutions which remove our hacks.</div>
不是防黑客的方法,而是我的几分钱。
【讨论】:
:(这就是为什么我告诉你不是一种防黑客的方式!
另一种方法是使用带有 span 元素的包装器,如下所示:
.text-overflow {
font-size: 40px;
overflow: hidden;
text-overflow: ellipsis;
width: 200px;
white-space: nowrap;
}
.style-text {
font-size: 20px;
}
<div class="text-overflow">
<span class="style-text">
text that is styled and styled and styled
</span>
</div>
【讨论】: