日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

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

發(fā)布時間:2023/12/2 CSS 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何仅通过CSS实现多行文本超长自动省略号 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

  在CSS中,我們可以通過下面的樣式實現(xiàn)DIV元素中文本超長后自動截斷并以省略號結尾:

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

  text-overflow: ellipsis是實現(xiàn)文本截斷后以省略號結尾的關鍵樣式,但問題是如果添加該樣式則DIV元素內的文本無法自動換行,也就是說該效果只被允許在單行文本上實現(xiàn)。另外,word-break: normal可以防止文字被部分截斷,這個在內容為英文的情況下顯得尤其重要。

  要實現(xiàn)多行文本自動截斷以省略號結尾的效果,通常的做法是使用JavaScript腳本。下面這些文章給出了如何通過腳本進行字符串截斷,不過僅限于英文環(huán)境。

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

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

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

  使用純CSS樣式來實現(xiàn)該效果則會稍微有些麻煩,你需要懂得如何在CSS中進行hack。這里是一個可以在多個通用瀏覽器中實現(xiàn)該效果的例子:

<!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屬性的值來指定容器的高度。該樣式的實現(xiàn)在多個不同版本的瀏覽器下測試通過,注意如果你是在IE下查看則需要確保你的文檔模型必須是在標準模式下,即Document Mode必須是Standards。

  這里是原文出處:CSS Ellipsis: How to Manage Multi-Lice Ellipsis in Pure CSS


更多專業(yè)前端知識,請上 【猿2048】www.mk2048.com

總結

以上是生活随笔為你收集整理的如何仅通过CSS实现多行文本超长自动省略号的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。