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

来源:互联网 发布:百度seo干货 编辑:程序博客网 时间:2024/04/28 11:44

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

http://www.cnblogs.com/jaxu/category/686860.html

  在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。

  这里是原文出处:http://www.mobify.com/blog/multiline-ellipsis-in-pure-css/

      原理:

ellipsis:before (由于float:left)在这里是至关重要的,设置了它之后,才能在文字溢出(文字容器float:right)之后,ellipsis:after也就是省略号的部分移到ellipsis:before的下面(由于float:right,而ellipsis:before固定了高度),看起来是在文字的后面,只是因为设置了left:100%,其实在文字未溢出的时候,省略号一直在文字的下面,也就是因为设置了left:100%,才看不到的。说白了设置那么多float,就是为了在右边的容器高于左边的容器时,省略号容器能改变位置。


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 天猫退货卖家收到货不退款怎么办 唯品会的账号找不回来了怎么办 从网上买的沙发物流超级慢怎么办 三星c5手机玩王者荣耀卡怎么办 买手机送话费的卡不想用了怎么办 到银行存钱被骗买保险了怎么办 去银行存钱被骗买了保险怎么办 京东申请退款后卖家又发货了怎么办 苹果6s没有4g网怎么办 花呗分期付款买手机额度不够怎么办 手机天猫不支持购买淘宝商品怎么办 天猫国际购买商品狠盾怎么办 在微信上微拍堂买东西被骗了怎么办 京东自营买到返修手机怎么办? 7p弯了怎么办能修复吗 农商银行app登录密码忘了怎么办 网址上的重庆时时彩不能提现怎么办 天天中彩票自己账户登录不了怎么办 天猫上卖王者荣耀的兑换码是怎么办 身份证借别人开淘宝永久封店怎么办 天猫店铺被扣12分怎么办 天猫法人变更之前的天猫贷款怎么办 在日本旅游遇到精日导游怎么办 银行资金交易异常卡被冻结怎么办 如果淘宝被盗了店铺乱上东西怎么办 快递不送货直接代售点签收怎么办 淘宝快递没收到却显示已签收怎么办 支付宝余额未满16受限怎么办 未满16岁支付宝余额受限怎么办 手机天猫购物买的数量大怎么办 扣扣游戏领礼包出现账号异常怎么办 美容院转让给别人客人要退钱怎么办 卖家毁约中介费担保费怎么办 天猫买东西店家不开增值税票怎么办 天猫专卖店品牌不授权了怎么办 临时京东账号被冻结买的东西怎么办 天猫强行退款给买家商家怎么办 淘宝店铺没交保证金被释放了怎么办 沭阳县地段生过了报名时间怎么办 淘宝买东西地址和收件人填错怎么办 才装修的房子马上要住怎么办