Neal Grosskopf Dot Com A NG Designs Site

Neal Grosskopf Dot Com Return To Neal Grosskopf's Home Page a NG Designs Site

Toolbar

  • Skip To
    • Menu
    • Content
  • Validate
    • HTML
    • CSS
    • WAI
  • Font Size
    • Small
    • Medium
    • Large
    • Extra Large
  • Account
    • Create Account
    • Login

Interact

Navigation

  • Home
  • Interact
    • Blog
    • Message Board
    • Open Links
    • Open Videos
    • Photo Gallery
    • Polls
    • Reviews
    • Geek Speak
  • Designs
    • View All Designs
    • Professional
    • Corporate
    • Templates
    • Personal
    • Club
    • NG Designs
    • Web Guides
  • Files
    • Artwork
    • Wallpapers
    • My File Downloads
  • Extras
    • Bookmarklets
    • The Magic 8-Ball
    • Career Builder RSS Feed Generator
    • Monster.com RSS Feed Generator

How To Create CSS Text/Font Gradients With CSS

  • Actions
    • View All
    • View Article Statistics
    • View User Statistics
    • Search
    • Subscribe to RSS Feed

Recent Articles:

  • Tracking Outgoing Links in Google Analytics Using Event Tracking and jQuery
  • Tracking Internal Website Campaigns With Google Analytics and PHP
  • Everything You Need to Know About jQuery's .data() Method
  • Google vs. China: How It Will Affect Web Workers
  • Don't Like The New Facebook Home Page? Fix It With 5 Lines of CSS
View All

Subscribe To Feed
loading Subscribers

I'm On Twitter loading

Jul 01

Something that CSS is currently lacking is the ability to add gradients to our text blocks. Currently we can add drop shadows to text or change the color of it but not add gradients. I suspect that in the coming years this will be a new property but until then here is a method to do it.

HTML Setup

The HTML Setup is pretty simple. Just place an empty SPAN element inside your heading element:

<h3><span></span>Text Here</h3>

Using JQuery to Automatically Insert SPAN Elements

I think a few people might find the above code unsightly, so here's a method to leave the empty SPAN element out of your HTML and inject it via JQuery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("h3").prepend("<span></span>") }); </script>

CSS Setup

The most important part of this code is setting the H3 element's position to relative. I'd also recommend setting the line-height to the same height as the font-size used and also reseting the margins back to zero.

h3 { font-size: 25px; line-height: 25px; margin: 0px; position: relative; }

Next, we position the empty SPAN element absolute and give it a height and width. Using this SPAN we can overlay various background-images over the H3 element.

h3 span { display: block; height: 26px; position: absolute; width: 100%; }

Gradient Effect

The first effect I'll demonstrate is a simple gradient. To do this we use a simple white to transparent gradient image and set it as the SPAN element's background-image. We can then position the background-image higher or lower depending on how strong we want the effect to be.

h3 span { background: url(gradient-white.png) repeat-x left 0px; }
SPAN Element Overlay

In the example above, the black transparent area represents the SPAN while the black border represents the bounds of the H3 element.

Demo

When viewing the demo, take your cursor and select the gradient text. This will give you a good understanding of how the gradient works -

View Demo >>

Gradient Effect With Hover

We can also apply a color change on hover with our gradient. Since the color change happens on the layer behind the gradient there is no additional CSS we must do other than change the H3's color when hovered.

h3 span { background: url(gradient-white.png) repeat-x left 0px; } h3:hover { color: #990000; }

View Demo >>

Two Tone Effect

Another effect we can do is apply a two toned text color to our text. To do this we use a white rectangle with it's opacity lowered and then position it over our H3.

h3 span { background: url(dual-white.png) repeat-x left 0px; }

View Demo >>

Sliding Door Effect

Finally, my last effect uses JQuery to create an animation when the text is hovered. Using a little JQuery we can slide the SPAN's background-image up and down when hovered on giving it sort of a 'garage door' effect.

<script type="text/javascript" src="http://jqueryjs.googlecode.com/svn/trunk/plugins/backgroundPosition/jquery.backgroundPosition.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("h3 span").hover( function () { $(this).stop().animate({backgroundPosition: "(0px -27px)"}, 400); }, function () { $(this).stop().animate({backgroundPosition: "(0px 0px)"}, 400); } ); }); </script>

View Demo >>

Got any other effects that could be accomplished using a background-image? Share them below!

Tags: css ( Add Tag )

Bookmark This Page:
  • Digg loading
  • Delicious loading
  • Re-Tweet loading
  • StumbleUpon loading
  • DZone loading
  • Snipplr
  • Script & Style

Replies:

  • Richard Sgobin
    Richard Sgobin
    #1

    Wed. July 8, 9:20:59 AM

    Very interisting effect!

    I see it works with Safari and Firefox MAC. What about Windows and IE? With Safari I have some strange behavior when selecting text.

  • Adam B
    Adam B
    #2

    Thu. May 6, 3:37:48 PM

    An interesting idea, but I see an issue with this idea: If the text you are adding the gradient effect to is on top of a non solid background your gradient will show up on that background. Also, the last example demo doesn't work.

    Cool idea though!


Reply:

B U I Link Font Color Edit Preview ?
Characters: 0 Textbox Size: [+] [-]
   

  • New Comment
    #3

    Sat. July 31, 1:06:58 PM

Font Size:

  • A
  • A
  • A
  • A

Search:

Places:

  • Blog
  • Message Board
  • Chat Room
  • Geek Speak
  • Open Videos
  • Reviews
  • Tag Cloud
 

Site Links

Site

  • About
  • Contact Me
  • Site Map
  • Site Search

Interact

  • Blog
  • Geek Speak
  • Message Board
  • Open Links
  • Open Videos
  • Photo Gallery
  • Polls
  • Reviews

Random Stuff

  • Hosting by FatCow
  • © Copyright 2005 - 2010
  • Modified: Friday, July 16, 2010
  • Load Time: 0.04 Seconds

Site Coded And Designed By Neal Grosskopf Using No Crappy CMS Or Themes