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.


Beautiful site, with beautiful content. I liked this article a lot.
The design guru says – this is a well crafted and well put together website
with some really fine tutorials.
This rocks thanks!
new? inventive? tutorial? or simply stolen?
http://www.khmerang.com/index.php?p=95
Nice effect, but not quite the same as the image sample that accompanies the article. The sample image shows a mirror image.
Thank you so much for this. I love it and am going to try it out now.
Brilliant idea.
why bother people? more knowledge source for everyone.. that’s what sharing means for..
Fantastic
Great idea : )
I just added this trick on my website with an orange text, and a small letter-spacing, it looks beautiful!
You can see it in the second column (the orange titles) : http://bbxdesign.com/
Awesome effect! Will try out.
Thanks for sharing.
Best wishes,
Jonny
hi its not working in IE 6.0 (png problem grey box coming over the text check it again) if you rectify this problem of png then also let me know…
all the best
regards
mukesh marwah
So clear! nice! thank u!
window.addEvent(‘domready’, function() {
var span = new Element(‘span’);
$$(‘h1′).each(function(el) {
span.clone().injectTop(el);
});
});
That’s one way of doing it for Mootools 1.1 as well, if anyone’s interested. Nice trick!!
Wow man this is pretty neat and now I’m watching the live comment preview, awesome
I am always very impressed by people who own GUI related talents/knowledge. Congratulations.
Great..
a very good design, I must say that first.
The tips are also very nicely presented, handy and good.
A very well built page, have fallen in love with the layout. Wish I knew all that it takes to create such a page.
Regards,
cipher
Nice! I love the effect!
I think that I will try it.
Thanks! :)
I love this one, I will use it for sure on my new website http://www.houstonpartybooths.com I think that will take care of both the design of a logo it will ad a new dimension to my header.
Thanks for the great tip