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 <span> 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 Text</h1>
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 <head> tag):
<!--[if lt IE 7]>
<style>
h1 span {
background: none;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='gradient.png', sizingMethod='scale');
}
</style>
<![endif]-->
This is why we hate IE 6!
jQuery prepend version (for semantic lovers)
If you don't want to have the empty <span> tag in the heading, you can use Javascript to prepend the <span> 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.


Absolutely wonderful, thnx
Excellent Tutorial! definately saves on load times.
IE is definately the bane of internet browsers. I have a PNG hack script for download on my website. Definately worth a look if you use alot of png’s.
Keep up the good work!
Correction on url for below script:
Script
Hi!
Interesting idea.
For some friends, I’ve recycled your code for use it with the IEPNGFix and I’ve writted this tutorial:
http://webstandards.dimix.it/2008/01/22/testi-con-gradienti-in-css/
I hope that this post is of interest to you and to others.
Thanks for your posts!
Great technique! But.. when I copied the sample address to IE.. it wasn’t displaying anything..
Thanks for the tutorial! I’ve only tried out the png fix portion. For anyone thats running IE7 and using a stand alone version of IE6 to browser test. I’ve found that the transparent png only displays properly in a real/installed version of IE6.
this is really good. i didn’t think it was that easy…
and man, i hate IE >.<;;;
Awesome tip ! Thnk you very much (again)
Wow, another great tutorial! Keep up the great work!
wow, this is very smart trick.
I read your posts a time ago and I found it amazing! This post is very, very useful!
Thanks for your valuable information!
Regards,
Xavier
Thanks for the tutorial!
Keep up the good work!
Your site F’ing rocks! Thanks for all the great tutorials.
This site is the shiznit!!!!!
Does anyone see any reason why this wouldn’t work or would cause problems in html email?
thanks!
This is fantastic! Not only is it easy to implement, but it also has all the advantages of image replacement (being real text, not an image).
This is amazing. Not only do you get the awesome effect… you get to keep your text, which is great for the super spiders. Nice tut…
CSS Globe has a similar article: http://www.cssglobe.com/post/1227/css-text-gradient
very interesting, i like the simple idea of it without editing the images with text and just have html text controlled by css, though one problem is no png support in IE6 and javascript fixes tent to slow web site down or not work when people disable their javascript. Well, when next server pack upgrade comes out then all IE6 will force upgrade into IE7 then this would be more cross browser, but the prob with this would be that it will take quite a while until everyone upgrade.
Wow that is cool. Will use it right away. Saving it to my delicious right now!