CSS漸層文字效果的兩種方法比較

來源:互聯網
上載者:User


是否想不用photoshop來建立一個帶漸層的標題文字嗎? 這裡用一個簡單的css技巧來向你展示如何僅僅使用css和png圖片來製造這種效果. 經測試這種方法適合大多數主流瀏覽器.當然, IE6需要一個支援透明PNG的Hack(值得慶幸的是微軟正在極力的將使用者的IE6自動升級到IE7^.^, 延伸閱讀:Warning: An IE7 Auto-Update Is Coming Soon)

優勢

這是純粹的css技巧,沒有使用任何ja指令碼或者flash, 並且它可以在大多數瀏覽器上正常工作(IE6需要支援透明PNG的hack)
這是完美的標題設計,你不必使用photoshop,這將大量節省你的頻寬和時間.
你可以對任何網頁字型使用這種效果,而且字型大小大小也是可變的.
他是如何工作的?

這個技巧很簡單.我們只是簡單的使用了1px寬的透明png覆蓋在了文本上.

html

CSS Gradient Text

CSS

關鍵就在這裡:

h1 { position: relative }
h1 span { position: absolute } h1 {
font: bold 330%/100% "Lucida Grande";
position: relative;
color: #464646;
}
h1 span {
background: url(gradient.png) repeat-x;
position: absolute;
display: block;
width: 100%;
height: 31px;
}

就這樣, 你做到了 ^_^ 點擊這裡查看樣本.

使它能夠支援IE6

下面這個hack僅僅是讓IE6支援透明PNG-24格式的圖片.

<!--[if lt IE 7]>
<style>
h1 span {
background:none;
filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='gradient.png', sizingMethod='scale');
}
</style>
<![endif]–>

jQuery產生版本

如果你不想在代碼裡有空的<span>標記, 那麼你可以使用javascript來動態產生它. 這裡是一個簡單的jquery生產方法.

<script type="text/javascript" src="jquery.js" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//prepend span tag to H1
$("h1").prepend("<span></span>");
});
</script>
DEMO打包下載

相關文章

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.