返回正常中文阅读
想对这篇译文“指手画脚”吗?
大错
小错
不顺
建议 CSS Gradient Text Effect
Do you want to create fancy headings without rendering each heading with Photoshop? Here is a simple CSS trick to show you how to create gradient text effect with a PNG image (pure CSS, no Javascript or Flash). All you need is an empty tag in the heading and apply the background image overlay using the CSS position:absolute property. This trick has been tested on most browsers: Firefox, Safari, Opera, and even Internet Explorer 6. Continue to read this article to find out how.
Benefits
- This is pure CSS trick, no Javascript or Flash. It works on most browsers including IE6 (PNG hack required).
- It is perfect for designing headings. You don’t have to render each heading with Photoshop. This will save you time and bandwidth.
- You can use on any web fonts and the font size remains scalable.
How does this work?
The trick is very simple. Basically we are just adding a 1px gradient PNG (with alpha transparency) over the text.
The HTML markups
<h1><span>span>CSS Gradient Texth1>
The CSS
The key point here is: h1 { position: relative } and 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;}
That’s it! You are done. Click here to view my demo page.
Make it work on IE6
Since IE6 doesn’t render PNG-24 properly, the following hack is required in order to display the transparent PNG (add anywhere in between the tag):
This is why we hate IE 6!
jQuery prepend version (for semantic lovers)
If you don’t want to have the empty tag in the heading, you can use Javascript to prepend the tag. Here is a sample using jQuery prepend method:
<script type="text/javascript" src="jquery.js">script><script type="text/javascript">$(document).ready(function(){ //prepend span tag to H1 $("h1").prepend("<span>span>");});script>
More samples
Want to make Web 2.0 glossy text?

Literally, you can apply this trick on any solid background color (as long your gradient color is the same as your background color).





Pattern / Texture
You can also apply this trick with a tile background image. Look, here is an example of zebra pattern. So, be creative!

Limitations and more…
- This trick is only suitable for solid background color elements. Your gradient color (PNG image) must be the same color as your background color.
- IE PNG hack is required if you want it to work on IE 6.
- If your gradient image is taller than the heading, the text will not be selectable.
CSS渐变文本效果
你是否想不用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
关键就在这里:
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" mce_src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//prepend span tag to H1
$("h1").prepend("<span></span>");
});
</script>


