radial-gradient

This article is in need of a technical review.

Summary

The CSS radial-gradient() function creates an <image> which represents a gradient of colors radiating from an origin, the center of the gradient. The result of this function is an object of the CSS <gradient> data type.

Radial gradients are defined by their center, the ending shape contour and position, and color stops. The radial gradient consists, going from its center to its ending shape and potentially beyond, of successive uniformly-scaled concentric shapes, identical to the ending shape. Color stops are positioned on a virtual gradient ray going out horizontally of the center and going to the right. Percentages positioning of color stops are relative to the intersection between the ending shape and this gradient ray representing 100%. Each shape is monocolor and defined by the color on the gradient ray it intersects.

Ending shapes can only be circle or ellipse.

Like any other gradient, a CSS radial gradient is not a CSS <color> but an image with no intrinsic dimensions, i. e. it has no natural or preferred size, nor ratio. Its concrete size will match the one of the element it applies to.

The radial-gradient function does not allow repeating gradients. For such a functionality, use the CSS repeating-radial-gradient function.

Syntax

Formal grammar: 
  radial-gradient( [[ circle               || <length> ]                     [ at <position> ]? , | 
                    [ ellipse              || [<length> | <percentage> ]{2}] [ at <position> ]? , |
                    [ [ circle | ellipse ] || <extent-keyword> ]             [ at <position> ]? , |
                                                                               at <position> ,     <color-stop> [ , <color-stop> ]+ )
                   \------------------------------------------------------------------------------/\--------------------------------/
                                 Definition of the contour, size and position of the ending shape         List of color stops  
    
          where <extent-keyword> = closest-corner | closest-side | farthest-corner | farthest-side
            and <color-stop> = <color> [ <percentage> | <length> ]?
Definition of the ending shape
radial-gradient( circle, … )                /* Synonym of radial-gradient( circle farthest-corner, …) */
radial-gradient( ellipse, … )               /* Synonym of radial-gradient( ellipse farthest-corner, …) */
radial-gradient( <extent-keyword>, … )      /* It draws a circle */
radial-gradient( circle radius, … )         /* A centered circle of the given length. It can't be a percentage */
radial-gradient( ellipse x-axis y-axis, … ) /* The two semi-major axis are given, horizontal, then vertical */

Definition of the position of the shape
radial-gradient (… at <position>, … )

Definition of the color stops
radial-gradient (…, <color-stop>)
radial-gradient (…, <color-stop>, <color-stop>)

Values

<position>
A <position>, interpreted in the same way as background-position or transform-origin. If omitted, the default is center.
<shape>
The gradient's shape. This is one of circle (meaning that the gradient's shape is a circle with constant radius) or ellipse (meaning that the shape is an axis-aligned ellipse). The default value is ellipse.
<size>
The size of the gradient. This is one of the Size constants listed below.
<color-stop>
Representing a fixed color at a precise position, this value is composed by a <color> value, followed by an optional stop position (either a <percentage> or a <length> along the virtual gradient ray). A percentage of 0%, or a length of 0, represents the center of the gradient, the value 100% the intersection of the ending shape with the virtual gradient ray. Percentage values in-between. are linearily positioned on the gradient ray.
<extent-keyword>
Keywords describing how big the ending shape must be. The possible keywords are:
Constant Description
closest-side The gradient's ending shape meets the side of the box closest to its center (for circles) or meets both the vertical and horizontal sides closest to the center (for ellipses).
closest-corner The gradient's ending shape is sized so it exactly meets the closest corner of the box from its center.
farthest-side Similar to closest-side, except the ending shape is sized to meet the side of the box farthest from its center (or vertical and horizontal sides).
farthest-corner The gradient's ending shape is sized so it exactly meets the farthest corner of the box from its center.
Early drafts included other keywords (cover and contain) as synonyms of the standard farthest-corner and closest-side respectively. Use the standard keywords only, as some implementations have already dropped those older variants.

Examples

ellipse farthest-corner
background-image: radial-gradient(ellipse farthest-corner at 45px 45px , #00FFFF 0%, rgba(0, 0, 255, 0) 50%, #0000FF 95%);
470px 47px
background-image: radial-gradient(ellipse farthest-corner at 470px 47px , #FFFF80 20%, rgba(204, 153, 153, 0.4) 30%, #E6E6FF 60%);
farthest-corner
background-image: radial-gradient(farthest-corner at 45px 45px , #FF0000 0%, #0000FF 100%);
16px radius fuzzy circle
background-image: radial-gradient(16px at 20px 50% , #000000 0%, #000000 14px, rgba(0, 0, 0, 0.3) 18px, rgba(0, 0, 0, 0) 19px);

Specifications

Specification Status Comment
CSS Image Values and Replaced Content Module Level 3 Candidate Recommendation  

Browser compatibility

Feature Firefox (Gecko) Chrome Internet Explorer Opera Safari
Basic support (support for the radial-gradient() function, but not necessary with the final syntax) 3.6 (1.9.2)-moz[4]
16 (16)
10.0 (534.16)-webkit [2][4] 10.0 [1] 11.60-o 5.1-webkit[2]
Legacy webkit syntax Not supported 3-webkit [2] Not supported Not supported 4.0-webkit[2]
at syntax (final standard syntax) 10 (10)-moz[4]
16 (16)
Not supported 10.0 11.60-o[4]
Presto 2.12 will remove the prefix.
Not supported

[1] Internet Explorer 5.5 through 9.0 supports proprietary filter: progid:DXImageTransform.Microsoft.Gradient() filter.

[2] WebKit since 528 supports the legacy -webkit-gradient(radial,…) function. See also their current support for radial gradients.

[4] Firefox 3.6 and Webkit implemented, prefixed, an early syntax.

See also

Attachments

File Size Date Attached by
Radial gradient principles
Describes how radial gradients work.
78242 bytes 2012-07-29 22:09:57 teoli
Hide Sidebar