我們在瀏覽網頁的時候,常常會看到某些網站的文章標題由于過長而只顯示一部分,另一部分用省略號來表示,這是為了防止標題過長而導致頁面排版不好
看。那么這個文章標題省略號是如何實現的呢?目前常見的方法是通過動態語言程序來控制,判斷標題的長度,然后截取部分來顯示,其余的用省略號顯示,這樣往
往在英文和中文的長度上難以判斷。今天在網上看到可以用CSS來實現,效果還不錯,拿來分享。
CSS實現截斷過長標題文字的原理非常簡單,就是設置一個寬度,然后超過這個寬度值的內容就隱藏,并用省略號來顯示。用到的就是text-
overflow:ellipsis,在IE下顯示是正確的,超出部分為省略號...,而在Firefox中超出部分卻是裁切掉了,有的文字就顯示一半,
很不好看,這是因為Firefox不支持text-
overflow:ellipsis屬性。為了讓Firefox中也能顯示省略號,所以要外加一個xml文件。下面直接給出兼容IE和Firefox的代
碼:
<!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=gb2312" />
<title>使用CSS截斷過長標題文字的方法-HTMer</title>
<style type="text/css">
<!--
.htmer{
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow:ellipsis; /*兼容IE*/
-moz-binding: url('ellipsis.xml#ellipsis'); /*兼容Firefox,調用ellipsis.xml文件,注意ellipsis.xml文件路徑*/
}
-->
</style>
</head>
<body>
<div class="htmer">使用CSS截斷過長標題文字的方法-HTMer</div>
</body>
</html>
ellipsis.xml文件源代碼:
<?xml version="1.0"?>
<bindings
xmlns="http://www.mozilla.org/xbl"
xmlns:xbl="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
>
<binding id="ellipsis">
<content>
<xul:description crop="end" xbl:inherits="value=xbl:text"><children/></xul:description>
</content>
</binding>
</bindings>
posted on 2011-01-17 11:13
Werther 閱讀(2316)
評論(1) 編輯 收藏 所屬分類:
11.JavaScript