CSS3 added support for new types of <image> , gradients, in the CSS background property. Using CSS gradients in a background lets you display smooth transitions between two or more specified colors; this lets you avoid using images for these effects, thereby reducing download time and bandwidth usage. In addition, because the gradient is generated by the browser, objects with gradients look better when zoomed, and you can adjust your layout much more flexibly.

Browsers support two types of gradients: linear (linear-gradient ) and radial (radial-gradient ).

Linear gradients

To create a linear gradient, you set a starting point and a direction (specified as an angle) along which the gradient effect is applied. You also define color stops. Color stops are the colors you want Gecko to render smooth transitions among, and you must specify at least two of them, but can specify more to create more complex gradient effects.

Simple linear gradients

Here's a linear gradient that starts at the center (horizontally) and top (vertically), and starts blue, transitioning to white.

Screen Shot Live Demonstration
t

 

/* The old syntax, deprecated, but still needed with the -o, -ms and -webkit prefixes*/
background: linear-gradient(top, blue, white); 

/* The new syntax needed for Firefox 10 onwards, with the -moz prefix */
background: linear-gradient(to bottom, blue, white);

Changing the same gradient to run from left to right:

Screen Shot Live Demonstration
basic_linear_blueleft.png
t

 

/* The old syntax, deprecated, but still needed with the -o, -ms and -webkit prefixes*/
background: linear-gradient(left, blue, white); 

/* The new syntax needed for Firefox 10 onwards, with the -moz prefix */
background: linear-gradient(to right, blue, white);

You can make the gradient run diagonally by specifying both the horizontal and vertical starting positions. For example:

Screen Shot Live Demonstration
basic_linear_bluetopleft.png
t
/* The old syntax, deprecated, but still needed with the -o, -ms and -webkit prefixes*/
background: linear-gradient(left top, blue, white); 

/* The new syntax needed for Firefox 10 onwards, with the -moz prefix */
background: linear-gradient(to bottom right, blue, white);

Using angles

If you don't specify an angle, one is determined automatically based on the start position. If you'd like more control over the direction of the gradient, you can set the angle specifically.

For example, here are two gradients that have the same starting point of "left center", but the second one also has an angle of 20 degrees.

linear_gradient_angle.png

The one on the right uses CSS like this:

/* Must be prefixed */
background: linear-gradient(left 20deg, black, white);

The angle is specified as an angle between a horizontal line and the gradient line, going counter-clockwise. In other words, 0deg generates a left to right horizontal gradient, while 90deg creates a vertical gradient from the bottom to the top:

linear_redangles.png

/* Must be prefixed */
background: linear-gradient(<angle>, red, white);

Color stops

Color stops are points along the gradient line that will have a specific color at that location. The location can be specified as either a percentage of the length of the line, or as an absolute length. You may specify as many color stops as you like in order to achieve the desired effect.

If you specify the location as a percentage, 0% represents the starting point, while 100% represents the ending point; however, you can use values outside that range if necessary to get the effect you want.

Example: Three color stops

This example specifies three color stops:

Screen Shot Live Demonstration
linear_colorstops1.png
t
/* The old syntax, deprecated, but still needed with the -o, -ms and -webkit prefixes*/
background: linear-gradient(top, blue, white 80%, orange);

/* The new syntax needed for Firefox 10 onwards, with the -moz prefix */
background: linear-gradient(to bottom, blue, white 80%, orange);

Note that the first and last color stops don't specify a location; because of that, values of 0% and 100% are assigned automatically. The middle color stop specifies a location of 80%, putting it most of the way toward the bottom.

Example: Evenly spaced color stops

Here's an example using a wide variety of colors, all evenly spaced:

Screen Shot Live Demonstration
linear_rainbow.png
t
/* The old syntax, deprecated, but still needed with the -o, -ms and -webkit prefixes*/
background: linear-gradient(left, red, orange, yellow, green, blue);

/* The new syntax needed for Firefox 10 onwards, with the -moz prefix */
background: linear-gradient(to right, red, orange, yellow, green, blue);

Notice that the color stops are automatically spaced evenly when no locations are specified.

Transparency and gradients

Gradients support transparency. You can use this, for example, when stacking multiple backgrounds, to create fading effects on background images. For example:

linear_multibg_transparent.png

/* The old syntax, deprecated, but still needed with the -o, -ms and -webkit prefixes*/ 
background: linear-gradient(left, rgba(255,255,255,0), rgba(255,255,255,1)), url(http://foo.com/image.jpg);

/* The new syntax needed for Firefox 10 onwards, with the -moz prefix */  
background: linear-gradient(to right, rgba(255,255,255,0), rgba(255,255,255,1)), url(http://foo.com/image.jpg);

The backgrounds are stacked with the first specified background on top, and each successive background farther away. By stacking backgrounds this way, you can create very creative effects as seen above.

Radial gradients

Radial gradients are specified using the -moz-radial-gradient property. The syntax is similar to that for linear gradients, except you can specify the gradient's shape (whether it should be a circle or ellipse) as well as its size.

Color stops

You specify color stops the same way as for linear gradients. The gradient line extends out from the starting position in all directions.

Example: Evenly spaced color stops

By default, as with linear gradients, the color stops are evenly spaced:

Screen Shot Live Demonstration
radial_gradient_even.png  
background: -moz-radial-gradient(red, yellow, rgb(30, 144, 255));

Example: Explicitly spaced color stops

Here we specify specific locations for the color stops:

Screen Shot Live Demonstration
radial_gradient_varied.png  
background: -moz-radial-gradient(red 5%, yellow 25%, #1E90FF 50%);

Size

This is one of the areas in which radial gradients differ from linear gradients. You can provide a size value that specifies the point that defines the size of the circle or ellipse. See this description of the size constants for specifics.

Example: closest-side for ellipses

This ellipse uses the closest-side size value, which means the size is set by the distance from the starting point (the center) to the closest side of the enclosing box.

Screen Shot Live Demonstration
radial_ellipse_size1.png  
background: -moz-radial-gradient(ellipse closest-side, red, yellow 10%, #1E90FF 50%, white);

Example: farthest-corner for ellipses

This example is similar to the previous one, except that its size is specified as farthest-corner, which sets the size of the gradient by the distance from the starting point to the farthest corner of the enclosing box from the starting point.

Screen Shot Live Demonstration
radial_ellipse_size2.png  
background: -moz-radial-gradient(ellipse farthest-corner, red, yellow 10%, #1E90FF 50%, white);

Example: closest-side for circles

This example uses closest-side, which determines the circle's size as the distance between the start point (the center) and the closest side.

Screen Shot Live Demonstration
radial_circle_size1.png  
background: -moz-radial-gradient(circle closest-side, red, yellow 10%, #1E90FF 50%, white);

Here, the circle's radius is half the height of the box, since the top and bottom edges are equidistant from the start point and are closer than the left and right edges.

Repeating gradients

The linear-gradient  and radial-gradient properties don't support automatically repeating the color stops. However, the repeating-linear-gradient and repeating-radial-gradient  properties are available to offer this functionality.

Examples: Repeating linear gradient

This example uses repeating-linear-gradient to create a gradient:

Screen Shot Live Demonstration
repeating_linear_gradient.png  
background: repeating-linear-gradient(top left -45deg, red, red 5px, white 5px, white 10px);

Another example using the repeating-linear-gradient property.

repeat_background_gradient_checked.png

background-color: #000;
background-image: repeating-linear-gradient(90deg, transparent, transparent 50px,
      rgba(255, 127, 0, 0.25) 50px, rgba(255, 127, 0, 0.25) 56px, transparent 56px, transparent 63px,
      rgba(255, 127, 0, 0.25) 63px, rgba(255, 127, 0, 0.25) 69px, transparent 69px, transparent 116px,
      rgba(255, 206, 0, 0.25) 116px, rgba(255, 206, 0, 0.25) 166px),
repeating-linear-gradient(0deg, transparent, transparent 50px, rgba(255, 127, 0, 0.25) 50px,
      rgba(255, 127, 0, 0.25) 56px, transparent 56px, transparent 63px, rgba(255, 127, 0, 0.25) 63px,
      rgba(255, 127, 0, 0.25) 69px, transparent 69px, transparent 116px, rgba(255, 206, 0, 0.25) 116px,
      rgba(255, 206, 0, 0.25) 166px),
repeating-linear-gradient(-45deg, transparent, transparent 5px, rgba(143, 77, 63, 0.25) 5px,
      rgba(143, 77, 63, 0.25) 10px),
repeating-linear-gradient(45deg, transparent, transparent 5px, rgba(143, 77, 63, 0.25) 5px,
      rgba(143, 77, 63, 0.25) 10px);

Example: Repeating radial gradient

This example uses -moz-repeating-radial-gradient  to create a gradient:

Screen Shot Live Demonstration
repeating_radial_gradient.png  
background: -moz-repeating-radial-gradient(black, black 5px, white 5px, white 10px);

Tags (2)

Edit tags

Attachments (17)

FileSizeDateAttached by 
 basic_linear_blueleft.png
No description
7.23 kB22:27, 3 Dec 2009SheppyActions
basic_linear_bluetop.png
No description
4.34 kB22:22, 3 Dec 2009SheppyActions
 basic_linear_bluetopleft.png
No description
9.63 kB22:30, 3 Dec 2009SheppyActions
 linear_colorstops1.png
No description
4.07 kB18:23, 4 Dec 2009SheppyActions
 linear_gradient_angle.png
No description
12.8 kB18:13, 4 Dec 2009SheppyActions
 linear_multibg_transparent.png
No description
78.23 kB18:31, 4 Dec 2009SheppyActions
 linear_rainbow.png
No description
8.14 kB18:29, 4 Dec 2009SheppyActions
 linear_redangles.png
No description
24.74 kB18:16, 4 Dec 2009SheppyActions
 radial_circle_size1.png
No description
17.22 kB22:44, 4 Dec 2009SheppyActions
 radial_circle_size2.png
No description
30.74 kB22:44, 4 Dec 2009SheppyActions
 radial_ellipse_size1.png
No description
27.3 kB20:27, 4 Dec 2009SheppyActions
 radial_ellipse_size2.png
No description
31.18 kB20:27, 4 Dec 2009SheppyActions
 radial_gradient_even.png
No description
13.25 kB18:44, 4 Dec 2009SheppyActions
 radial_gradient_varied.png
No description
8.97 kB18:44, 4 Dec 2009SheppyActions
 repeat_background_gradient_checked.png
Repating Background Gradient - Checked
12.12 kB19:25, 22 Mar 2012Aniket PantActions
 repeating_linear_gradient.png
No description
3.32 kB22:59, 4 Dec 2009SheppyActions
 repeating_radial_gradient.png
No description
3.7 kB22:59, 4 Dec 2009SheppyActions

Attach file