A quick intro into CSS3 gradients

CSS3 gradients are available to use in Webkit (Safari, Chrome) and Mozilla (Firefox) browsers. They’re not exactly new but I only recently started using them consistently myself so I’ve put together this quick article.

FYI this isn’t the holy grail of CSS3 gradients, it’s more of a reminder for me or quick getting started guide.

Why use CSS3 gradients instead of images?

  1. Gives you more flexibility
  2. Less hassle
  3. Fewer http requests
  4. Scalable

The syntax

W3C
linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, <stop>]* )

radial-gradient( [<position> || <angle>,]? [<shape> || <size>,]? <stop>, <stop>[, <stop>]* )
Mozilla (Firefox)

Same as W3C (but with -moz-)

-moz-linear-gradient( [<point> || <angle>,]? <stop>, <stop> [, <stop>]* )

-moz-radial-gradient( [<position> || <angle>,]? [<shape> || <size>,]? <stop>, <stop>[, <stop>]* )
WebKit (Safari, Chrome)

Bit different.

-webkit-gradient(<type>, <point> [, <radius>]?, <point> [, <radius>]? [, <stop>]*)

Confused yet? Instead of trying to explain what everything means I’m just going to show you some very simple examples.

CSS3 gradient examples

Linear background gradient
html{
  height:100%;
}
body{
  background-color:#440951;
  background-image: -moz-linear-gradient(top, #b032cb, #440951);
  background-image: -webkit-gradient(linear, left top,left bottom, from(#b032cb), to(#440951));
  background-image: linear-gradient(top, #b032cb, #440951);
}
linear.jpg

Demo

Radial background gradient
html{
  height:100%;
}
body{
  background-color:#440951;
  background-image: -moz-radial-gradient(center 45deg,circle cover, #b032cb, #440951);
  background-image: -webkit-gradient(radial, 50% 50%, 0, 50% 50%,800, from(#b032cb), to(#440951));
  background-image: radial-gradient(center 45deg,circle cover, #b032cb, #440951);
}
radial.jpg

Demo

Body background height problem

Seems that gradients don’t stretch the whole height of the browser window unless you declare the html height. Therefore this is needed:

html{height:100%}

But it doesn’t work in Internet Explorer!

That’s why we have a fall back value in for the background-color which should be enough although you can always use an image if you like.

Have you started using CSS3 gradients yet?

Further reading on CSS3 gradients

Related articles


23 Appreciated Comments

  1. On the , Mark McCorkell said:

    Handy article there Lee for anyone looking to get into some CSS3. I haven’t used CSS3 Gradients much yet, but I have been using quite a bit of the other CSS3 properties, like RGBa etc.

    Internet Explorer may eventually catch up, but like you said there… that’s why we have the fallback properties in there. :-)

  2. On the , lipelip said:

    Nice post Lee,

    The thing I don’t like about it is the syntaxe, it’s not really intuitive…
    That’s why I recommend this tool from colorzilla, very usefull ;)

  3. On the , @JeffreyPia said:

    Thanks for the explanation! This property is definitely one of the more difficult ones to wrap my head around.

    @lipelip LOVE that Collorzilla tool!!! The only thing I’m worried about is that I’ll use it so much it becomes a crutch and keeps me from completely mastering the code itself. I guess there are worse things to worry about. :-)

  4. On the , @hyper_linda said:

    Good article, but remember to put the actual css3 property last (that is, after the vendor prefixed ones). That way, when these browsers begin to support the proper syntax they will use these instead of the experimental vendor prefix ones.

  5. On the , Derek Johnson said:

    css3pie does a pretty good job with linear gradients in IE if you don’t mind a .htc file in your site.

  6. On the , Lee said:

    @lipelip: Nice tool. Very handy when it comes to using all those stops

    @hyper_linda: Good thinking! Changed.

  7. On the , Colin @ Slush Puppy Machine said:

    Nice article and demonstrations.

    Why is IE always playing catchup?

  8. On the , Indiadesign said:

    Great read! Thanks Lee. Me also started experimenting, still some ingredients pains my head. Anyways your tips are very helpful. Yes, IE may catch up this in the coming future if they want to stay back in the race.

  9. On the , Francois Botha said:

    *fewer* HTTP requests

  10. On the , Lee said:

    Thanks Francois :-)

  11. On the , Dallas SEO said:

    I have come to accept that Microsoft and the Internet Explorer browser was sent from hell by the devil himself to slowly erode the creative spirit of web developers everywhere. I tried using a gradient for my nav, but of course I had to have an IE version, and by that point I was just adding another http request, which completely defeated the purpose of using the gradient.

  12. On the , Merritts for Hair said:

    Great technique! I’ll try to add this in my menu, it might help make the site load faster.
    Once again IE let us down! :)

  13. On the , Dean said:

    Interesting post, actually the first time ive heard of css gradients. The demos didnt work on firefox/mac but did with safari. Wouldnt it be nice if they all just agreed on one format and stuck to it…

  14. On the , David Webb said:

    This is really useful, I’ve been adding a picture.

  15. On the , Chris said:

    At work we usually add a…

    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=’#from’, endColorstr=’#to’);

    to achieve gradients in IE.

  16. On the , Alhadis said:

    I’ve been planning on writing a gradient generator myself (as close to Photoshop’s gradient editor as possible, but with a few minor additions). The fact that gradients are heavily visual in nature means you can’t simply sit down and write out the code whilst having a solid idea of what it’ll look like… you need constant feedback to get that right look. That’s why editors are needed (though of course, it never hurts to know the syntax behind it, either. ;))

  17. On the , Rick Upshaw said:

    I’m embarrassed to say that I had never heard of CSS3 before reading this blog post. The good news, Lee, is that I can credit your website for keeping me on top of the leading edge technology (even though this has apparently been around for a while). I’m particularly interested in anything I can do to make my sites load faster, so thanks for sharing these thoughts!

    Is there anywhere I can go online to read up on CSS3, or perhaps an online course you could recommend?

  18. On the , Derek Johnson said:

    @Rick you should check out Sitepoint’s CSS3 course with John Allsopp. Only costs $10 and he explains it better than anybody else. Plenty of video tutorials too.

  19. On the , Alhadis said:

    @Rick: http://www.css3.info/

  20. On the , Website laten maken said:

    Hello,

    This works on IE, search on the internet to pie.php. This can you put in your CSS file and it works!

  21. On the , Dave said:

    If it doesn’t work in IE then why not just use an image? All the time spent on browsers is sometimes unnecessary and the client inevitably will pick up the tab.

  22. On the , Roger said:

    I’ve been playing around with CSS3 lately as I’ve been feeling really behind the curve. Thanks everyone for all the great info and especially to you Lee for all the great articles. I always feel I’m getting up on the latest stuff when I come here.

  23. On the , erd said:

    Very nice, I have to test how this works in older browsers but if it works im sure to use it, thanks

Have a comment? Tweet @leemunroe and I'll be glad to respond.