使用CSS3建立文字色彩坡形(CSS3 Text Gradient)

來源:互聯網
上載者:User

標籤:http   color   java   使用   os   io   ar   art   

考慮一下,如何在網頁中達到類似以下文字漸層的效果?

 

 

         傳統的實現中,是用一副透明漸層的圖片覆蓋在文字上。具體實現方式可參考 http://www.qianduan.net/css-gradient-text-effect.html 。這種方式優點是圖片可控,所以可實現很複雜的漸層效果,但是缺點是圖片漸層色必須與背景色一致,同時損失了滑鼠點擊、文字選擇等事件。

         改進的方法可以使用 CSS3 的背景漸層 -webkit-gradient ,用一個背景漸層的 DIV 代替圖片。下面是實現效果樣本,相比以上方案優點是不使用圖片,減小請求量和流量,但是對於以上缺點,仍然無法解決。

 

 

         有沒有完美的解決方案呢?

         以下介紹使用 -webkit-mask 遮罩的方案來實現文字漸層,完全避免了以上方案的不足。下面是實現的完美:

 

 

         現在讓我們開始 CSS3 Text Gradient 之旅。

         1、  構建 HTML 內容和基本樣式:

         我們使用一個 H1 標籤包裹一個 A 標籤:

view plaincopy to clipboardprint?
  1. <h1><a href= "#"  mce_href= "#" >Jiangyujie</a></h1>  

 

Javascript代碼 
  1. <h1><a href="#" mce_href="#">Jiangyujie</a></h1>  

 

 

         樣式定義如下,我們使用 text-shadow 為文字添加陰影:

view plaincopy to clipboardprint?
  1. h1 {  
  2.     font-family: Segoe UI, Verdana, sans-serif;  
  3.     font-size: 100px;  
  4.     line-height: 100px;  
  5.     text-shadow: -3px 0 4px #006;  
  6. }  
  7. h1 a:link,  
  8. h1 a:visited,  
  9. h1 a:hover,  
  10. h1 a:active {  
  11.     color: #d12;  
  12.     text-decoration: none;  
  13. }  

 

Javascript代碼 
  1. h1 {  
  2.     font-family: Segoe UI, Verdana, sans-serif;  
  3.     font-size: 100px;  
  4.     line-height: 100px;  
  5.     text-shadow: -3px 0 4px #006;  
  6. }  
  7. h1 a:link,  
  8. h1 a:visited,  
  9. h1 a:hover,  
  10. h1 a:active {  
  11.     color: #d12;  
  12.     text-decoration: none;  
  13. }  

 

 

         基本效果如下:

 

 

         2、  添加漸層效果:

         我們通過 CSS3 的 mask 屬性為文字添加線性漸層。和 background 的漸層相比,可以理解為 background是在文字後面,而 mask 是疊加在文字上面的。 Mask 可以設定為普通顏色、線性漸層、放射狀漸層或者圖片。

         代碼如下:

view plaincopy to clipboardprint?
  1. h1 a:link,  
  2. h1 a:visited,  
  3. h1 a:hover,  
  4. h1 a:active {  
  5.     color: #d12;  
  6.     text-decoration: none;  
  7.     -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));  
  8. }  

 

Javascript代碼 
  1. h1 a:link,  
  2. h1 a:visited,  
  3. h1 a:hover,  
  4. h1 a:active {  
  5.     color: #d12;  
  6.     text-decoration: none;  
  7.     -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));  
  8. }  

 

 

         效果如下所示:

 

 

         3、  漸層為另外一種顏色:

         因為 -webkit-gradient 實際上是按照圖片的方式進行處理的,所以我們不能通過設定文字顏色為漸層來實現文字色彩坡形為另外一種顏色的效果(不信的話你可以試試)。

         所以我們要構造一個虛擬元素,內容和我們的文本一樣,使用虛擬元素的原因是避免再添加一個同樣內容的標籤造成代碼冗餘:

view plaincopy to clipboardprint?
  1. h1:after {  
  2.          content: "Jiangyujie" ;  
  3.          color: #000;  
  4.          text-shadow: 3px 3px 1px #600;  
  5. }  

 

Javascript代碼 
  1. h1:after {  
  2.          content: "Jiangyujie";  
  3.          color: #000;  
  4.          text-shadow: 3px 3px 1px #600;  
  5. }  

 

 

         然後我們通過position屬性將兩個文本重疊在一起:

view plaincopy to clipboardprint?
  1. h1 {  
  2.          position: relative;  
  3.          font-family: Segoe, Verdana, sans-serif;  
  4.          font-size: 100px;  
  5.          line-height: 100px;  
  6.          text-shadow: -3px 0 4px #006;  
  7.          }  
  8. h1 a:link,  
  9. h1 a:visited,  
  10. h1 a:hover,  
  11. h1 a:active {  
  12.          position: absolute;  
  13.          text-decoration: none;  
  14.          top: 0;  
  15.          z-index: 2;  
  16.          color: #d12;  
  17.          -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));  
  18. }  

 

Javascript代碼 
  1. h1 {  
  2.          position: relative;  
  3.          font-family: Segoe, Verdana, sans-serif;  
  4.          font-size: 100px;  
  5.          line-height: 100px;  
  6.          text-shadow: -3px 0 4px #006;  
  7.          }  
  8. h1 a:link,  
  9. h1 a:visited,  
  10. h1 a:hover,  
  11. h1 a:active {  
  12.          position: absolute;  
  13.          text-decoration: none;  
  14.          top: 0;  
  15.          z-index: 2;  
  16.          color: #d12;  
  17.          -webkit-mask-image: -webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));  
  18. }  

 

 

         效果如所示:

 

 

         4、  添加背景:

         這種實現方式的優勢是我們可以自訂背景,完全不受漸層顏色的影響。例如我們可以為文字添加一副背景,效果如下:

 

 

         很棒的效果,不是嗎:)

         5、  其他:

         CSS3 mask 的詳細資料可以參考 Webkit.org 的 文章 。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.