CSS文字截取功能實現代碼

來源:互聯網
上載者:User

好處是:
相容IE,firefox,Opera;
有利於內容完整性;有利於SEO;
無需背景程式處理;
可以在前台隨時調節要截取的長度。

不好的地方:
不能自動判斷截取長度,當字元很短的時候在Firefox中也會產生後面的省略符號。

另外在設定截取寬度的時候,要注意,盡量讓文字截取完整。

Quote:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>css文字截取</title>
<style type="text/css">
body{font-size:13px;color:#8c966b;}
div{clear:both;width:340px;border:1px solid #333;margin:3px;padding:3px;}
div a{color:#8c966b;text-decoration:none;}
div a:hover{text-decoration:underline;}

div a{display:block;width:310px;white-space:nowrap;overflow:hidden;float:left;
-o-text-overflow: ellipsis; /* for Opera */
text-overflow:ellipsis; /* for IE */
}
div:after{content:"...";padding-left:3px;font-size:12px;}/* for Firefox */
</style>
</head>

<body>

用css來實現自動截取文字,不需要背景程式和JS的使用

好處是:有利於內容完整性,有利於SEO,無需背景程式處理,可以在前台隨時調節要截取的長度。

<div><a href="#">不好的地方:不能自動判斷截取長度,當字元很短的時候在Firefox中也會產生後面的省略符號。</a></div>
<div><a href="#">另外在設定截取寬度的時候,要注意,盡量讓文字截取完整</a></div>

</body>
</html>

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.