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