COM271, Week 6

CSS for Background Gradient Fills

Syllabus | Assignments | References and Useful Links

As an alternative to images for gradient backgrounds, CSS3 has the following properties that are now recognized in all contemporary browsers:

Source: CSS3 Gradient Backgrounds, http://css-tricks.com/examples/CSS3Gradient/ (includes other examples).

For latest compatibility, see http://caniuse.com/#feat=css-gradients

Linear Gradient, Top to Bottom: CSS coding.

#linearBg2 { /* fallback */ background-color: #1a82f7; background: url(images/linear_bg_2.png /*you supply this image, as a default for older browsers*/); background-repeat: repeat-x;
/* Safari 4-5, Chrome 1-9 */ background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#1a82f7), to(#2F2727));
/* Safari 5.1, Chrome 10+ */ background: -webkit-linear-gradient(top, #2F2727, #1a82f7);
/* Firefox 3.6+ */ background: -moz-linear-gradient(top, #2F2727, #1a82f7);
/* IE 10 */ background: -ms-linear-gradient(top, #2F2727, #1a82f7);
/* Opera 11.10+ */ background: -o-linear-gradient(top, #2F2727, #1a82f7); }

Linear Gradient, Left to Right: CSS coding.

#linearBg1 { /* fallback */ background-color: #1a82f7; background-image: url(images/linear_bg_1.png /*you supply....*/); background-repeat: repeat-y;
/* Safari 4-5, Chrome 1-9 */ background: -webkit-gradient(linear, left top, right top, from(#1a82f7), to(#2F2727));
/* Safari 5.1, Chrome 10+ */ background: -webkit-linear-gradient(left, #2F2727, #1a82f7);
/* Firefox 3.6+ */ background: -moz-linear-gradient(left, #2F2727, #1a82f7);
/* IE 10 */ background: -ms-linear-gradient(left, #2F2727, #1a82f7);
/* Opera 11.10+ */ background: -o-linear-gradient(left, #2F2727, #1a82f7); }

Radial Gradient (Centered, Full Size): CSS coding.

#radial-center { /* fallback */ background-color: #2F2727; background-image: url(images/radial_bg.png /*you supply....*/); background-position: center center; background-repeat: no-repeat;
/* Safari 4-5, Chrome 1-9 */ /* Can't specify a percentage size? Laaaaaame. */ background: -webkit-gradient(radial, center center, 0, center center, 460, from(#1a82f7), to(#2F2727));
/* Safari 5.1+, Chrome 10+ */ background: -webkit-radial-gradient(circle, #1a82f7, #2F2727);
/* Firefox 3.6+ */ background: -moz-radial-gradient(circle, #1a82f7, #2F2727);
/* IE 10 */ background: -ms-radial-gradient(circle, #1a82f7, #2F2727);
/* Opera couldn't do radial gradients, then at some point they started supporting the -webkit- syntax, how it kinda does but it's kinda broken (doesn't do sizing) */ }

Caniuse.com compatibility chart, 10/3/2013: