怎么通过CSS样式实现DIV元素中多行文本超长自动省略号

发布时间:2021-08-07 23:39:47 作者:chen
来源:亿速云 阅读:106

这篇文章主要讲解了“怎么通过CSS样式实现DIV元素中多行文本超长自动省略号”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么通过CSS样式实现DIV元素中多行文本超长自动省略号”吧!

在CSS中,我们可以通过下面的样式实现DIV元素中文本超长后自动截断并以省略号结尾:

代码如下:


overflow: hidden;
word-break: normal;
text-overflow: ellipsis;


text-overflow: ellipsis是实现文本截断后以省略号结尾的关键样式,但问题是如果添加该样式则DIV元素内的文本无法自动换行,也就是说该效果只被允许在单行文本上实现。另外,word-break: normal可以防止文字被部分截断,这个在内容为英文的情况下显得尤其重要。

要实现多行文本自动截断以省略号结尾的效果,通常的做法是使用JavaScript脚本。下面这些文章给出了如何通过脚本进行字符串截断,不过仅限于英文环境。

http://www.barelyfitz.com/projects/truncate/

http://www.javascriptsource.com/miscellaneous/truncate-text.html

http://www.javascriptbank.com/truncate-html-text.html/en/

使用纯CSS样式来实现该效果则会稍微有些麻烦,你需要懂得如何在CSS中进行hack。这里是一个可以在多个通用浏览器中实现该效果的例子:

代码如下:


<!DOCTYPE HTML>
<html>
<head>
<style>
html, body, p { margin: 0; padding: 0; font-family: sans-serif;}
.ellipsis {
overflow: hidden;
height: 200px;
line-height: 25px;
margin: 20px;
border: 5px solid #AAA; }
.ellipsis:before {
content:"";
float: left;
width: 5px; height: 200px; }
.ellipsis > *:first-child {
float: right;
width: 100%;
margin-left: -5px; }
.ellipsis:after {
content: "\02026";
box-sizing: content-box;
-webkit-box-sizing: content-box;
-moz-box-sizing: content-box;
float: right; position: relative;
top: -25px; left: 100%;
width: 3em; margin-left: -3em;
padding-right: 5px;
text-align: right;
background: -webkit-gradient(linear, left top, right top,
from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white));
background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white);
background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white); }
</style>
</head>
<body>
<div class="ellipsis"><div>
<p>Call me Ishmael. Some years ago &ndash; never mind how long precisely &ndash; having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouth; whenever it is a damp, drizzly November in my soul; whenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meet; and especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off &ndash; then, I account it high time to get to sea as soon as I can.</p>
</div></div>
</body>
</html>


通过修改.ellipsis和.ellipsis:before样式中height属性的值来指定容器的高度。该样式的实现在多个不同版本的浏览器下测试通过,注意如果你是在IE下查看则需要确保你的文档模型必须是在标准模式下,即Document Mode必须是Standards。

感谢各位的阅读,以上就是“怎么通过CSS样式实现DIV元素中多行文本超长自动省略号”的内容了,经过本文的学习后,相信大家对怎么通过CSS样式实现DIV元素中多行文本超长自动省略号这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. css怎么实现文本单行超出和多行超出省略号
  2. CSS实现单行和多行文本溢出显示省略号的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css

上一篇:CSS常用样式简单的总结

下一篇:如何解决某些HTML字符打不出来的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》