The Gradient Syntax
The gradient in CSS3 is declared using the
background-image
property. This is for better compatibility when we also need to add background-color
in a single rule set, so that they do not collide with one another. Then, to create the radial gradient we simply call it with this radial-gradient()
function. There are four values to be included in the function to set the gradient properly.The first value defines the gradient position. We can use a descriptive keyword such as top, bottom, center and left, or we can also be more specific like,
50% 50%
to set the gradient at the center or 0% 0%
to set the gradient to start at top left
.The second value defines the shape and the gradient size, there are two arguments to shape the gradients, first the
ellipse
which is the default, and the second is circle
.And for the gradient size, we can select one of the following six arguments.
Values | Description |
---|---|
closest-side |
The gradient’s 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 shape is sized so it exactly meets the closest corner of the box from its center.
|
farthest-side |
Similar to closest-side, except the shape is sized to meet the side of the box farthest from its center (or vertical and horizontal sides).
|
farthest-corner |
The gradient’s shape is sized so it exactly meets the farthest corner of the box from its center.
|
contain |
A synonym for
closest-side . |
cover |
A synonym for
farthest-corner . |
Lastly, the third and the fourth define the color combination. So, here is how we write the syntax to create the Elliptical gradients, and this time we will use
cover
for the gradient size, so it will spread widely, covering the container;view plaincopy to clipboardprint?
- body {
- background-image: radial-gradient(center center, ellipse cover, #ffeda3, #ffc800);
- }
To create the Circular gradient we can simply do it this way:
view plaincopy to clipboardprint?
- body {
- background-image: radial-gradient(center center, circle cover, #ffeda3, #ffc800);
- }
As the name implies, the gradient shape will be a circle.
Browser Support
Just take note though, all the browsers are still in the process of providing full support for this feature, so they still need the vendor prefix. Thus, the whole complete syntax that will work in all modern browsers – IE10+, Firefox 3.6+, Chrome 4.0+, Safari 4.0+ and Opera 11+ – will look something like this;
view plaincopy to clipboardprint?
- body {
- background-image: radial-gradient(center bottombottom, ellipse cover, #ffeda3, #ffc800);
- background-image: -o-radial-gradient(center bottombottom, ellipse cover, #ffeda3, #ffc800);
- background-image: -ms-radial-gradient(center bottombottom, ellipse cover, #ffeda3, #ffc800);
- background-image: -moz-radial-gradient(center bottombottom, ellipse cover, #ffeda3, #ffc800);
- background-image: -webkit-radial-gradient(center bottombottom, ellipse cover, #ffeda3, #ffc800);
- }