如何仅通过CSS实现多行文本超长自动省略号

简介:

在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 Ellipsis: How to Manage Multi-Lice Ellipsis in Pure CSS


本文转自Jaxu博客园博客,原文链接:http://www.cnblogs.com/jaxu/archive/2013/04/03/2997157.html,如需转载请自行联系原作者

相关文章
|
1月前
|
前端开发
CSS文本单行溢出显示省略号与多行溢出显示省略号(简单易懂)
CSS文本单行溢出显示省略号与多行溢出显示省略号(简单易懂)
|
1月前
编程笔记 html5&css&js 007 HTML文本:段落和格式
编程笔记 html5&css&js 007 HTML文本:段落和格式
|
1月前
编程笔记 html5&css&js 006 HTML文本:标题
编程笔记 html5&css&js 006 HTML文本:标题
|
2月前
|
移动开发 前端开发 JavaScript
H5+CSS3+JS逆向前置——HTML1、H5文本元素
H5+CSS3+JS逆向前置——HTML1、H5文本元素
27 0
|
3月前
|
Web App开发 前端开发
CSS:字体和文本样式
CSS:字体和文本样式
39 0
|
3月前
|
前端开发
css实现单行文本溢出以及多行文本溢出
css实现单行文本溢出以及多行文本溢出
|
前端开发
CSS文本加省略号
版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82766836 ...
939 0
|
前端开发 弹性计算
css文本超出2行就隐藏并且显示省略号
首先,要知道css的三条属性。 overflow:hidden; //超出的文本隐藏 text-overflow:ellipsis; //溢出用省略号显示 white-space:nowrap; //溢出不换行 这三个是css的基础属性,需要记得。
4013 0
|
前端开发
当内容超出最大的长度的时候,使用CSS使文本显示省略号
.description{  height:17px;  overflow:hidden;  text-overflow:ellipsis;  -webkit-text-overflow:ellipsis;  -o-text-overflow:ellipsis;  white-space:nowrap; }
1093 0