隨筆-199  評論-203  文章-11  trackbacks-0
          我們在瀏覽網(wǎng)頁的時候,常常會看到某些網(wǎng)站的文章標題由于過長而只顯示一部分,另一部分用省略號來表示,這是為了防止標題過長而導(dǎo)致頁面排版不好 看。那么這個文章標題省略號是如何實現(xiàn)的呢?目前常見的方法是通過動態(tài)語言程序來控制,判斷標題的長度,然后截取部分來顯示,其余的用省略號顯示,這樣往 往在英文和中文的長度上難以判斷。今天在網(wǎng)上看到可以用CSS來實現(xiàn),效果還不錯,拿來分享。

              CSS實現(xiàn)截斷過長標題文字的原理非常簡單,就是設(shè)置一個寬度,然后超過這個寬度值的內(nèi)容就隱藏,并用省略號來顯示。用到的就是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,調(diào)用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 閱讀(2322) 評論(1)  編輯  收藏 所屬分類: 11.JavaScript

          評論:
          # re: 使用CSS截斷過長文字的方法 2016-03-08 12:21 | 12312312
          測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試測試測試測試彩色測試  回復(fù)  更多評論
            
          主站蜘蛛池模板: 伽师县| 饶阳县| 台州市| 大同市| 中山市| 峨山| 平和县| 上杭县| 浠水县| 永城市| 湄潭县| 改则县| 白河县| 尉犁县| 加查县| 怀集县| 普兰店市| 宁安市| 奈曼旗| 温泉县| 临沧市| 兖州市| 万全县| 剑阁县| 松原市| 陇南市| 安远县| 顺义区| 孟州市| 九寨沟县| 古交市| 嵊州市| 莱西市| 河西区| 大安市| 霍山县| 阜阳市| 东乌珠穆沁旗| 洛扎县| 阿坝县| 珠海市|