Update: I wrote this article in 2009. In early 2011 WebKit decided to change their syntax to match that used in Firefox (and the W3C specification). The syntax contained in these articles will be maintained for reasons of backwards-compatibility, but you should use the new syntax for the future.
CSS Gradients were originally proposed by the WebKit team in April 2008, modified from the syntax proposed for the Canvas element of HTML5. In August of this year, Mozilla announced that an implementation slightly modified from that of WebKit would be in the next version of Firefox (3.6).
Since then, however, the CSS WG have discussed a different syntax, and a resolution was passed to add this to the Image Values module. Mozilla have decided to implement the new syntax, which is simpler than WebKit’s but less flexible.
In this article, which will be split into at least two parts, I’m going to compare the two syntaxes.
Throughout both parts, I will be referring to the following demo page; you’ll need to be using Safari 4, Chrome, or Firefox 3.6beta3+ in order to see it working:
The first and most noticeable difference is that the Mozilla implementation uses two properties –
-moz-radial-gradient – whereas WebKit use only a single property –
-webkit-gradient – with two type values:
radial. That should become clearer as I progress.
Please note that I’ve tried to use the simplest syntax possible for all examples, but there may be occasions where I’ve slipped up. Feel free to correct me.
The Mozilla syntax accepts three different properties: start point, angle of direction, and colour stops. WebKit’s also takes three (excluding type: linear, as mentioned previously): start point, end point, and colour stops.
So a simple left-right gradient change (Linear 1) between two colours in Mozilla:
-moz-linear-gradient (left, green, yellow);
The left value sets the default position to the middle of the left-hand side (0deg, 0 50% or left center does the same); the line then goes horizontally out passing from green to yellow in equal proportion.
To get the same pattern in WebKit:
-webkit-gradient (linear, left center, right center, from(green), to(yellow));
The start point is at left center (I could use unit values or percentages as Mozilla does, but not an angle), the end point at right center, and it also passes from green to yellow in equal proportion.
We can tweak those values slightly to get a diagonal gradient from the bottom left corner (Linear 2); for Mozilla I specify an angle, for WebKit I set new start and end points:
-moz-linear-gradient(45deg,green,yellow); -webkit-gradient(linear,left bottom,right top,from(green),to(yellow));
I’ll tweak those values again to make a bottom-top gradient with an extra colour (Linear 3):
-moz-linear-gradient(90deg,green,yellow,blue); -webkit-gradient(linear,center bottom,center top,from(green),color-stop(0.5, yellow),to(blue));
The big difference here is that Mozilla distributes the three colours proportionally, whereas WebKit’s
color-stop function requires that I set a stop point — I’ve used 0.5 here, although I could have used 50%. The
to functions are shorthand for
color-stop values 0 and 1 (or 100%), respectively.
I don’t have to distribute the colours evenly, however; by changing the value in the colour stop I can choose my own ratio. In the final example (Linear 4) I’ve set the yellow to begin 25% along the length of the gradient:
-moz-linear-gradient(90deg,green,yellow 25%,blue); -webkit-gradient(linear,center bottom,center top,from(green),color-stop(25%, yellow),to(blue));
For Webkit I only have to update the
color-stop value; for Mozilla, I have to specify it.
There are a few differences in the syntaxes for linear gradients, but they’re not major; Mozilla use an angle value, where WebKit use a point-to-point system. I’d say Mozilla’s is the easier to grasp purely because of its brevity, but it’s not that important. In the second part of this article I’ll talk about radial gradients, and it’s there that the differences in implementation become more pronounced.