Jan 17
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 <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.
Free TxEff Component Giveaways Vintage Web Design
Comments
Pages: 30 29 28 27 26 25 24 23 22 21 20 … 1 » Show All
There are 291 comments (+Add)
Pages: 30 29 28 27 26 25 24 23 22 21 20 … 1 » Show All













291 Test http://Test
June 20th, 2009 at 1:24 am
290 Nikhil Verma
June 8th, 2009 at 7:37 pm
Very good method to teach with examples and cool designs of web pages..
send me feedback.
289 flashfs http://souleaterwallpaper.co.cc
May 28th, 2009 at 7:18 pm
It’s hard to select text, but maybe a good trick if you just want some (real) text with a gradient
288 Araba Resimleri http://www.arabaresimleri.somee.com
May 21st, 2009 at 4:59 pm
thanks.
287 çorum http://www.corumhurriyet.com
May 21st, 2009 at 4:58 pm
thanks.. nice script
286 Flash Buddy http://quickcolorado.com
May 20th, 2009 at 4:01 pm
So simply clever and cleverly simple. I’m a fan!
285 Carl - Web Courses Bangkok http://www.webcoursesbangkok.com
May 14th, 2009 at 1:01 pm
This is a great method thanks. I`ve always wanted to do something like this and its such as simple method!
C
284 Simple Station Web Design http://simplestation.com
May 13th, 2009 at 10:03 pm
This was a good example of using gradients in web design but I’m not sold on the approach. As Adam mentioned it doesn’t degrade well to IE6 and without using Dean Edwards hack I’m not sure it would even display on a webpage.
283 gaziantep evden eve taşımacılık http://www.tutunculertasimacilik.com
May 13th, 2009 at 4:26 am
thanks you
282 Jeff http://www.rootsrain.com
May 11th, 2009 at 11:49 am
Interesting idea. But it appears to have an unstated limitation: the PNG transparency must be the same color as your background. So you can’t, say, have a medium grey font, a white background, and a black gradient on top. There’s nothing that “masks” the PNG to the text.
I would love to figure out how to do that, though.