Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
時(shí)候需要控制下文字?jǐn)?shù),不然就會(huì)溢出,頁(yè)面就會(huì)變樣不美觀。這時(shí)我們就可以用css控制字?jǐn)?shù),超出部分顯示省略號(hào)。可以不換行,超出部分顯示省略號(hào),也可以可以換行,多行,超出部分顯示省略號(hào)。
1.不換行,超出部分顯示省略號(hào)
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"/> <title>用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</title> <style type="text/css"> *{margin:0;padding:0;} body{width:1000px;margin:100px auto;} .box{ width:260px; /*超出部分就隱藏*/ overflow:hidden; /*不換行設(shè)定*/ white-space:nowrap; /*超出部分的文字顯示省略號(hào)*/ text-overflow:ellipsis; } </style> </head> <body> <div class="box">用css控制字?jǐn)?shù),超出部分顯示省略號(hào)用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</div> </body> </html>
效果圖如下:
2.可以換行,多行,超出部分顯示省略號(hào)
<!Doctype html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"/> <title>可以換行,多行,超出部分顯示省略號(hào)</title> <style type="text/css"> *{margin:0;padding:0;} body{width:1000px;margin:100px auto;} .box{ width:260px; display: -webkit-box; -webkit-box-orient: vertical; /*2行*/ -webkit-line-clamp: 2; overflow: hidden; } </style> </head> <body> <div class="box">1.用css控制字?jǐn)?shù),超出部分顯示省略號(hào)用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</div> <div class="box">2.用css控制字?jǐn)?shù),超出部分顯示省略號(hào)用css控制字?jǐn)?shù),超出部分顯示省略號(hào)</div> </body> </html>
效果圖如下:
注:此方法適用于WebKit瀏覽器及移動(dòng)端。
除注明外的文章,均為來(lái)源:湯久生博客,轉(zhuǎn)載請(qǐng)保留本文地址!
原文地址:http://tangjiusheng.com/divcss/169.html
標(biāo)簽是默認(rèn)是自動(dòng)換行的,因此設(shè)置好寬度之后,能夠較好的實(shí)現(xiàn)效果,但是最近的項(xiàng)目中發(fā)現(xiàn),使用ajax加載數(shù)據(jù)之后,p標(biāo)簽內(nèi)的內(nèi)容沒(méi)有換行,導(dǎo)致布局錯(cuò)亂,于是嘗試著使用換行樣式,雖然解決了問(wèn)題,但是并沒(méi)有發(fā)現(xiàn)本質(zhì)原因,本質(zhì)在于,我當(dāng)時(shí)獲取的數(shù)據(jù)是一長(zhǎng)串的數(shù)字,瀏覽器應(yīng)該是對(duì)數(shù)字和英文單詞處理方式相近,不會(huì)截?cái)唷?
先給出各種方式,再具體介紹每一個(gè)屬性。
強(qiáng)制不換行
p { white-space:nowrap; }
自動(dòng)換行
p { word-wrap:break-word; }
強(qiáng)制英文單詞斷行
p { word-break:break-all; }
*注意:設(shè)置強(qiáng)制將英文單詞斷行,需要將行內(nèi)元素設(shè)置為塊級(jí)元素。
超出顯示省略號(hào)
p{text-overflow:ellipsis;overflow:hidden;}
white-space: normal|pre|nowrap|pre-wrap|pre-line|inherit;
white-space 屬性設(shè)置如何處理元素內(nèi)的空白
normal 默認(rèn)。空白會(huì)被瀏覽器忽略。
pre 空白會(huì)被瀏覽器保留。其行為方式類(lèi)似 HTML 中的 pre 標(biāo)簽。
nowrap 文本不會(huì)換行,文本會(huì)在在同一行上繼續(xù),直到遇到 br 標(biāo)簽為止。
pre-wrap 保留空白符序列,但是正常地進(jìn)行換行。
pre-line 合并空白符序列,但是保留換行符。
inherit 規(guī)定應(yīng)該從父元素繼承 white-space 屬性的值。
word-wrap: normal|break-word;
word-wrap 屬性用來(lái)標(biāo)明是否允許瀏覽器在單詞內(nèi)進(jìn)行斷句,這是為了防止當(dāng)一個(gè)字符串太長(zhǎng)而找不到它的自然斷句點(diǎn)時(shí)產(chǎn)生溢出現(xiàn)象。
normal: 只在允許的斷字點(diǎn)換行(瀏覽器保持默認(rèn)處理)
break-word:在長(zhǎng)單詞或URL地址內(nèi)部進(jìn)行換行
word-break: normal|break-all|keep-all;
word-break 屬性用來(lái)標(biāo)明怎么樣進(jìn)行單詞內(nèi)的斷句。
normal:使用瀏覽器默認(rèn)的換行規(guī)則。
break-all:允許再單詞內(nèi)換行
keep-all:只能在半角空格或連字符處換行
舉例看起區(qū)別:
<!doctype html>
<html lang="en">
<head>
<!--網(wǎng)站編碼格式,UTF-8 國(guó)際編碼,GBK或 gb2312 中文編碼-->
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="Keywords" content="關(guān)鍵詞一,關(guān)鍵詞二">
<meta name="Description" content="網(wǎng)站描述內(nèi)容">
<meta name="Author" content="Yvette Lau">
<title>Document</title>
<!--css js 文件的引入-->
<style>
.word{background:#E4FFE9;width:250px;margin:50px auto;padding:20px;font-family:"microsoft yahei";}
/* 強(qiáng)制不換行 */
.nowrap{white-space:nowrap;}
/* 允許單詞內(nèi)斷句,首先會(huì)嘗試挪到下一行,看看下一行的寬度夠不夠,
不夠的話就進(jìn)行單詞內(nèi)的斷句 */
.breakword{word-wrap: break-word;}
/* 斷句時(shí),不會(huì)把長(zhǎng)單詞挪到下一行,而是直接進(jìn)行單詞內(nèi)的斷句 */
.breakAll{word-break:break-all;}
/* 超出部分顯示省略號(hào) */
.ellipsis{text-overflow:ellipsis;overflow:hidden;}
</style>
</head>
<body>
<div class = "word">
<p class = "nowrap">wordwrap:breakword;absavhsafhuafdfbjhfvsalguvfaihuivfs</p>
<p class = "breakword">wordwrap:break-word;absavhsafhuafdfbjhfvsalguvfaihui</p>
<p class = "breakAll">wordwrap:break-word;absavhsafhuafdfbjhfvsalguvfaihuivf</p>
<p class = "normal">wordwrap:breakword;absavhsafhuafdfbjhfvsalguvfaihuivfsa</p>
<p class = "ellipsis">wordwrap:breakword;absavhsafhuafdfbjhfvsalguvfaihuivfsab</p>
</div>
</body>
</html>
效果如下:
伙伴們,對(duì)于省略號(hào)呢,咱們前端攻城獅的實(shí)現(xiàn)方法可就是多種多樣了,那接下來(lái)呢我就給你羅列一下如果用css書(shū)寫(xiě)這些特殊效果,來(lái)一起看看吧~~~
效果圖:
實(shí)現(xiàn)代碼:
HTML部分
<h3>使用css實(shí)現(xiàn)單行省略號(hào)</h3>
<div class="box1">
<a href="#" class="a1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam ipsaexplicabo quos sapiente ea error, mollitia necessitatibus animi facere rem non sed velit aperiam laboriosamdebitis. Quae deleniti doloremque nisi.
</a>
</div>
CSS部分
h3 {
padding-left: 10px;
}
.a1 {
text-decoration: none;
color: #000;
padding-left: 20px;
}
.box1 {
height: 40px;
line-height: 40px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
box-sizing: border-box;
/* 單行顯示省略號(hào) */
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
注:此案例為京東首頁(yè)的部分切圖,此時(shí)的省略號(hào)可以用上述代碼實(shí)現(xiàn)
效果圖:
HTML部分
<h3>使用css實(shí)現(xiàn)三行之后顯示省略號(hào)</h3>
<div class="box2">
<a href="#" class="a1">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Fugit dicta laudantium aspernatur illo id, beatae mollitia magnam, laboriosam cupiditate harum veritatis ullam delectus adipisci quasi, laborum ipsum quis est molestiae.
</a>
</div>
CSS部分
h3 {
padding-left: 10px;
}
.a1 {
text-decoration: none;
color: #000;
padding-left: 20px;
}
.box2 {
height: 60px;
line-height: 30px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
overflow: hidden;
/* 三行顯示省略號(hào) */
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
}
實(shí)際案例應(yīng)用場(chǎng)景說(shuō)明:
注:此時(shí)明顯是折行后在第二行多余的部分顯示省略號(hào),那由于內(nèi)容不固定字?jǐn)?shù)不固定,所以需要?jiǎng)討B(tài)設(shè)置,那么就應(yīng)用到了上述的多行顯示省略號(hào)的方法
效果圖:
HTML部分
<h3>使用css實(shí)現(xiàn)中間顯示省略號(hào)方法一</h3>
<div class="box3">Lorem, ipsum dolor sit amet consectetur adipisicing elit. Commodi perferendis iste sit! Et quos aspernatur suscipit ab qui? Cumque debitis fugiat ab fugit repudiandae, vel eius error nisi minus<span></span><a href="#">全文</a>
</div>
css部分
.box3 {
/* height: 120px; */
line-height: 30px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
}
.box3 span::after {
content: '......';
}
實(shí)際案例應(yīng)用場(chǎng)景說(shuō)明:
效果圖:
HTML部分
<h3>使用css實(shí)現(xiàn)中間顯示省略號(hào)方法二</h3>
<div class="box4">
<a href="#">
<span class="span1" title="我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容">我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容我是左側(cè)內(nèi)容</span>
<span class="span2" title="我是右側(cè)內(nèi)容我是右側(cè)內(nèi)容"></span>
</a>
</div>
css部分
.box4 {
height: 30px;
line-height: 30px;
background-image: linear-gradient(white, gray);
box-shadow: 0 0 2px 2px rgb(148, 145, 145);
}
.box4 .span1 {
float: left;
width: 62%;
height: 30px;
line-height: 30px;
overflow: hidden;
}
.box4 a {
color: #000;
}
.box4 .span2::before {
content: attr(title);
width: 38%;
float: right;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
direction: rtl;
}
/* 優(yōu)化兩個(gè)span中間的間距 */
.box4 {
text-align: justify;
}
實(shí)際案例應(yīng)用場(chǎng)景說(shuō)明:
綜上所述:以上四種方案是目前頁(yè)面中應(yīng)用較多的實(shí)現(xiàn)省略號(hào)的方法,僅代表個(gè)人觀點(diǎn),如您有更優(yōu)的方法歡迎聯(lián)系我們。
*請(qǐng)認(rèn)真填寫(xiě)需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。