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.

View Demos

Download Demo ZIP

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.

screenshot

The HTML markups

<code class="html"><span class="tag"><<span class="keyword">h1</span>></span><span class="tag"><<span class="keyword">span</span>></span><span class="tag"></<span class="keyword">span</span>></span>CSS Gradient Text<span class="tag"></<span class="keyword">h1</span>></span></code></pre> <h3>The CSS</h3> <p>The key point here is: <code>h1 { position: relative } </code>and <code>h1 span { </code><code>position: absolute }</code></p> <pre><code class="css"><span class="keyword">h1</span> <span class="rules">{<br />  <span class="keyword">font</span>:<span class="value"> bold 330%/100% "Lucida Grande"</span>;<br />  <span class="keyword">position</span>:<span class="value"> relative</span>;<br />  <span class="keyword">color</span>:<span class="value"> #464646</span>;<br />}</span><br /><span class="keyword">h1</span> <span class="keyword">span</span> <span class="rules">{<br />  <span class="keyword">background</span>:<span class="value"> url(gradient.png) repeat-x</span>;<br />  <span class="keyword">position</span>:<span class="value"> absolute</span>;<br />  <span class="keyword">display</span>:<span class="value"> block</span>;<br />  <span class="keyword">width</span>:<span class="value"> 100%</span>;<br />  <span class="keyword">height</span>:<span class="value"> 31px</span>;<br />}</span></code></pre> <p><em>That’s it!</em> You are done. <a href="http://www.webdesignerwall.com/demo/css-gradient-text/">Click here</a> to view my demo page.</p> <h3>Make it work on IE6</h3> <p>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 <code><head></code> tag):</p> <pre><code class="css"><!--<span class="attr_selector">[if lt IE 7]</span>><br /><br /><<span class="keyword">style</span>><br /><span class="keyword">h1</span> <span class="keyword">span</span> <span class="rules">{<br />  <span class="keyword">background</span>:<span class="value"> none</span>;<br />  filter:<span class="value"> progid:DXImageTransform.Microsoft.AlphaImageLoader(src='gradient.png', sizingMethod='scale')</span>;<br />}</span><br /></<span class="keyword">style</span>><br /><br /><!<span class="attr_selector">[endif]</span>--></code></pre> <p><em>This is why <a href="http://www.webdesignerwall.com/general/trash-all-ie-hacks/">we hate IE 6</a>!</em></p> <h3>jQuery prepend version (for semantic lovers)</h3> <p>If you don’t want to have the empty <code><span></code> tag in the heading, you can use Javascript to prepend the <code><span></code> tag. Here is a sample using <a target="_top" href="http://jquery.com/">jQuery</a> prepend method:</p> <pre><code class="html"><span class="tag"><<span class="keyword">script</span><span class="attribute"> type=<span class="value">"text/javascript"</span></span><span class="attribute"> src=<span class="value">"jquery.js"</span></span>></span><span class="tag"></<span class="keyword">script</span>></span><br /><br /><span class="tag"><<span class="keyword">script</span><span class="attribute"> type=<span class="value">"text/javascript"</span></span>></span><br />$(document).ready(function(){<br /><br />  //prepend span tag to H1<br />  $("h1").prepend("<span class="tag"><<span class="keyword">span</span>></span><span class="tag"></<span class="keyword">span</span>></span>");<br /><br />});<br /><span class="tag"></<span class="keyword">script</span>></span></code>

More samples

Want to make Web 2.0 glossy text?

screenshot

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

screenshot

screenshot

screenshot

screenshot

screenshot

screenshot

Pattern / Texture

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

screenshot

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.
Technorati Tags: