The CSS gradient feature was introduced by Webkit for about two years but was rarely used due to incompatibility with most browers. But now with the Firefox 3.6+, which supports gradient, we can style create gradient without having to create an image. This post will show you how to code for the CSS gradient to be supported by the major browsers: IE, Firefox 3.6+, Safari, and Chrome. Also, check out my updated dropdown menu (demo) using CSS gradient.
For Webkit Browsers
The following line of code is for webkit browsers such as Safari, Chrome, etc. It will display a linear gradient from top (#ccc) to bottom (#000).
background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000));

For Firefox 3.6+
background: -moz-linear-gradient(top, #ccc, #000);

For Internet Explorer
The following filter wlil only be read by IE:
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000');

Cross-Browser CSS Gradient (demo)
Put the three lines of code from above together and the result is a cross-browser gradient box. Note: I added a background rule at the very top in case the user is using a browser that doesn't support the feature.
background: #999; /* for non-css3 browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000)); /* for webkit browsers */
background: -moz-linear-gradient(top, #ccc, #000); /* for firefox 3.6+ */
CSS Gradient Dropdown Menu
Below is a pure CSS gradient dropdown menu using CSS3 text-shadow, radius-border, and box-shadow (no Javascript or image)
Internet Explorer Limitations
Internet Explorer gradient filter doesn't support color-stop, gradient angle, and radial gradient. That means you can only specify either horizontal or vertical linear gradient with 2 colors: StartColorStr and EndColorStr.
Final Remarks
Please note not all browsers support CSS gradient. To be safe, you shouldn't rely on CSS gradient when coding the layout. It should only be used for enhancing the layout.


You did a great job, thanks a lot.
Bro..you solve the day!
Thanks a Lot!
hmmm… The css dropdown menu doen’t work for IE 7 :-(
Hi, I loved your site.
I want to create a linear gradient for the background/viewport. I am able to successfully do the linear gradient in divs. and other such containers. Mt problem is how to make the linear gradient stretch in the background
Thanks
You can apply the background in the body tag like:
body{
…Your gradient background css code here :)
}
Thanks TechbulletsPH. Top man :)
The problem with background gradients is often that they must either be a fixed width (100%) or must be set to auto, which then ruins the relative size-ability of its child divs. My solution was to give ALL parents a 100% height, starting with
html,body { position:absolute; top:0; left:0; height:100%; width:100%; /* and gradient goes here */
}
Once this is set, the gradient starts over after 100%, so to keep everything contained
.body_double {
height:100%;
width:100%;
overflow:scroll; /* the fun begins!!! */
overflow-x:hidden; /* no thanks, sideways scroll */
}
.container { /* parent div to all percentage-sized contents. */
width:90%;
HEIGHT:98%; /* now I can safely place a 2% height footer, outside of this div, that stays at the bottom of .body_double */
margin:0 auto;
float:none;
text-align:center /* for IE centering */
}
.MainLeft { /*first child to .container*/
position:relative;
HEIGHT:100%;
width:66.666667%;
float:left;
clear:left;
}
.MainRight { /*second child to .container*/
position:relative;
HEIGHT:100%;
width:33.333%;
float:left;
}
Footer goes outside of .container and has a 2% height, but remains within .body_double, your gradient is fixed while appearing fluid, and your page scrolls at the y-axis. THE ONLY Setback I can find, is that a scroll bar is inevitable. So far, this is the best answer I can find for a background gradient at 100% while also maintaining a re-sizable child div. Anybody who knows an easier way is my hero.
I meant to say “fixed height” above, at the very beginning…
I take it back. That was a stupid solution. Against my own will, I tried a fixed position background image (1px by 1800px) repeat-x, using the background-size:contain rule. Much more success. The fixed position image in the background allows inner divs to be any length, because it stays as the full-size background no matter how scroll works.
I tried using your pure css navigation bar and the colors when you hover over something or when a button is set to current does not work in Google Chrome. Is there a remedy to this?
Hi,
I left a comment a couple of months ago regarding a program I’ve written that can generate the CSS and SVG code for all possible CSS linear and radial gradients.
The good news is, I’ve since learnt Javascript and converted the program to an online web app at visualcsstools.com.
You will need a modern browser to view the page. If you use IE, you’ll need IE9+, which can handle SVG graphics. If you have the IE10 preview, you’ll be able to see the output in both CSS and SVG.
All other browser users should be fine assuming that your browser version is pretty much up to date.
If you have any comments, then please email me at the address provided on the web page.
Cheers,
Jules
This was really helpfull for me.Thank a lot.
Thanks!:)
can you help me for making text gradient masking.
thank u for this code ,, thank u very much.
can it work with magento
Thanx a ton…
hi, i want gradient to be supported by all browser types.
Please help me.
thanks.
Thanks, very useful article and well explained!
ok,thanks,very good
is there a way to create a gradient background without the use of -moz-?
what if you what a gradient with multiple colors? How do you put that into the code?
Yay finally found a code that works in all browsers – been having a hell of a morning trying to get this figured out. Thanks :)
The good news is, I’ve since learnt Javascript and converted the program to an online web app at visualcsstools.com.
You will need a modern browser to view the page. If you use IE, you’ll need IE9+, which can handle SVG graphics. If you have the IE10 preview, you’ll be able to see the output in both CSS and SVG.
All other browser users should be fine assuming that your browser version is pretty much up to date.
If you have any comments, then please email me at the address provided on the web page.
Thank you.. Thank you.. Thank you..
Thanks so much, awesome tutorial!