In days gone by, to achieve a background gradient on an element, it was necessary to tile a thin graphical slice of the gradient. As graphics resources go, it's quite an economical trade-off. An image, only a pixel or two wide, isn't going to break the bandwidth bank and on a single site it can be used on multiple elements.
However, if we need to tweak the gradient it still requires round-trips to the graphics editor. Plus, occasionally, content might 'break out' of the gradient background, extending beyond the images' fixed size limitations. This problem is compounded with a responsive design, as sections of a page may increase at different viewports.
With a CSS background-image gradient however, things are far more flexible. As part of the CSS Image Values and Replaced Content Module Level 3, CSS enables us to create linear and radial background gradients. Let's look how we can define them.
The specification for CSS Image Values and Replaced Content Module Level 3 can be found at http://www.w3.org/TR/css3-images/.
The linear-gradient notation, in its simplest form, looks like this:
.linear-gradient {
background: linear-gradient(red, blue);
}This will create a linear gradient that starts at red (the gradient starts from the top by default) and fades to blue.
Now, if you want to specify a direction for the gradient, there are a couple of ways. The gradient will always begin in the opposite direction to where you are sending it. However, when no direction is set, a gradient will always default to a top to bottom direction. For example:
.linear-gradient {
background: linear-gradient(to top right, red, blue);
}In this instance, the gradient heads to the top right. It starts red in the bottom-left corner and fades to blue at the top right.
If you're more mathematically minded, you may believe it would be comparable to write the gradient like this:
.linear-gradient {
background: linear-gradient(45deg, red, blue);
}However, keep in mind that on a rectangular box, a gradient that heads 'to top right' (always the top right of the element it's applied to) will end in a slightly different position than 45deg (always 45 degrees from its starting point).
It's worth knowing you can also start gradients before they are visible within a box. For example:
.linear-gradient {
background: linear-gradient(red -50%, blue);
}This would render a gradient as if it had started before it is even visible inside the box.
We've actually used a color stop in that last example to define a place where a color should begin and end so let's look at those more fully.
Perhaps the handiest thing about background gradients is color stops. They provide the means to set which color is used at which point in a gradient. With color stops you can specify something as complex as you are likely to need. Consider this example:
.linear-gradient {
margin: 1rem;
width: 400px;
height: 200px;
background: linear-gradient(#f90 0, #f90 2%, #555 2%, #eee 50%, #555 98%, #f90 98%, #f90 100%);
}Here's how that linear-gradient renders:

In this example (example_06-03), a direction has not been specified so the default top to bottom direction applies.
Color stops inside a gradient are written comma separated and defined by giving first the color, and then the position of the stop. It's generally advisable not to mix units in one notation but you can. You can have as many color stops as you like and colors can be written as a keyword, HEX, RGBA, or HSLA value.
Note that there have been a number of different background gradient syntaxes over the years so this is one area that is particularly difficult to write fallbacks for by hand. At the risk of sounding like a broken record (kids, if you don't know what a 'record' is, ask mom or dad), make your life easier with a tool such as Autoprefixer. This lets you write the current W3C standard syntax (as detailed earlier) and it will automatically create the prior versions for you.
Read the W3C specification for linear background gradients at http://www.w3.org/TR/css3-images/.
As a simple fallback for older browsers that don't support background gradients, just define a solid background color first. That way older browsers will at least render a solid background if they don't understand the gradient that's defined afterwards. For example:
.thing {
background: red;
background: linear-gradient(45deg, red, blue);
}It's equally simple to create a radial gradient in CSS. These typically begin from a central point and spread out smoothly in an elliptical or circular shape.
Here's the syntax for a radial background gradient (you can play with it in example_06-04):
.radial-gradient {
margin: 1rem;
width: 400px;
height: 200px;
background: radial-gradient(12rem circle at bottom, yellow, orange, red);
}After specifying the property (background:) we begin the radial-gradient notation. To start with, before the first comma, we define the shape or size of the gradient and the position. We have used 12rem circle for the shape and size above but consider some other examples:
5em would be a circle 5em in size. It's possible to omit the 'circle' part if giving just a size.circle would be a circle the full size of the container (the size of a radial gradient defaults to 'farthest corner' if omitted—more on sizing keywords shortly)40px 30px would be a ellipse as if drawn inside a box 40px wide by 30px tallellipse would create an ellipse shape that would fit within the elementNext, after the size and/or shape, we define the position. The default position is center but let's look at some other possibilities and how they can be defined:
We end our size, shape, and position 'parameters' with a comma and then define any color stops; which work in exactly the same manner as they do with linear-gradient.
To simplify the notation: size, shape, and position before the first comma, then as many color stops as needed after it (with each stop separated with commas).
For responsive work, you may find it advantageous to size gradients proportionally rather than using fixed pixel dimensions. That way you know you are covered (both literally and figuratively) when the size of elements change. There are some handy sizing keywords that can be applied to gradients. You would write them like this, in place of any size value:
background: radial-gradient(closest-side circle at center, #333, blue);
Here is what each of them does:
closest-side: The shape meets the side of the box nearest to the center (in the case of circles), or meets both the horizontal and vertical sides that are closest to the center (in the case of ellipses)closest-corner: The shape meets exactly the closest corner of the box from its centerfarthest-side: The opposite of closest-side, in that rather than the shape meeting the nearest size, it's sized to meet the one farthest from its center (or both the furthest vertical and horizontal side in the case of an ellipse)farthest-corner: The shape expands to the farthest corner of the box from the centercover: Identical to farthest-cornercontain: Identical to closest-sideRead the W3C specification for radial background gradients at http://www.w3.org/TR/css3-images/.
The cheat's way to perfect CSS3 linear and radial gradients
If defining gradients by hand seems like hard work, there are some great online gradient generators. My personal favorite is http://www.colorzilla.com/gradient-editor/. It uses a graphics editor style GUI, allowing you to pick your colors, stops, gradient style (linear and radial gradients are supported), and even the color space (HEX, RGB(A), HSL(A)) you'd like the final gradient in. There are also loads of preset gradients to use as starting points. If that wasn't enough, it even gives you optional code for fixing up Internet Explorer 9 to show the gradient and a fallback flat color for older browsers. Still not convinced? How about the ability to generate a CSS gradient based on upon the gradient values in an existing image? Thought that might swing it for you.