Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Bring the Bling with CSS Gradients

Gradients are one of the most hotly anticipated features to become native to CSS. Gradients are vital for design in general to reproduce the effects of light falling on curved/shiny surfaces and create interesting patterns. The number of developers who use them in web design is staggering, if not unsurprising. What is a surprise is that until CSS3 came along, web developers never had the ability to create gradients programmatically in any sane way that would work across browsers. SVG had gradients for a long time before that, but IE never supported SVG until IE9.

All this time you’ve been stuck with either faking SVG in IE using a Polyfill solution like SVGWeb or using repeated background images for those gradients or repeating patterns you desired. This last technique works OK-ish but is an inflexible pain and can become cumbersome very quickly, especially if your boss keeps changing his mind about the gradient colors (more playing with Photoshop; oh goody) or if you are trying to create any kind of complicated layered effect.

Again, CSS3 comes to the rescue with linear and radial gradients, which are defined in the CSS Image Values and Replaced Content module (http://dev.w3.org/csswg/css3-images). To see how flexible CSS gradients are, just have a good play with the examples in this section.

Let’s review the two different gradient types separately.

Linear Gradients

Linear gradients are the simpler of the two types; these are smooth color progressions that start at one side or corner of an area and cycle smoothly between two or more color stops, ending at the other side or corner.

In CSS they work the same. CSS gradients are basically a special kind of background image. You can set them in place of an image in most places that it would make sense to do so; for example, background-image and border-image (see the “Box Clever: border-image” section later in this chapter for more on border images).

The most common place you’ll want to use them is on standard, commonplace backgrounds. Here is a simple syntax example:

background: linear-gradient(#ff0000,#007700);

Figure 4.12 shows the result, taken from an example file in the chapter4 code download folder called linear-gradient-test.html. The two colors are the start color in the gradient and the end color, and by default the gradient runs from the top to the bottom of the container.

Figure 4.12

Figure 4.12 A basic linear gradient.

Linear Gradient Direction

If you want to vary the direction of your gradient, you can add a direction value at the start of the gradient, like this:

background: linear-gradient(to bottom right, #ff0000,#007700);

This direction value makes the gradient travel from the top left to the bottom right (Figure 4.13).

Figure 4.13

Figure 4.13 A linear gradient direction can be varied via the use of keywords or degree values.

As you’d expect, you can use a whole range of logical keywords for gradient direction: to top, to top right, to right, to bottom right, to bottom, to bottom left, to left, to top left.

You can also specify the direction you want the gradient to travel in using an angle. Zero degrees (0deg) is the equivalent of to right; as you increase the angle, it travels around counterclockwise. So the subsequent equivalents would be 90deg = to top, 180deg = to left, 270deg = to bottom. Bear in mind that 135deg will not be the equivalent of to top left (as you might expect) unless the container is a perfect square: The diagonal keywords will change the angle so the gradient will always run from one corner to the other. As a result, you can choose keywords or angles, depending on the effect you want to create.

Note: The spec states that 0deg is the equivalent of the keywords to top, but browsers don’t follow this currently. This could change in the future.

Linear Gradient Color Stops

You can also add multiple color stops between the start and the end point by putting them between the start and end color stops, like this:

background: linear-gradient(to right,#ff0000,#0000ff 40%, #000000 70%,#007700);

The result is shown in Figure 4.15.

Figure 4.15

Figure 4.15 A gradient with multiple color stops.

The unit values specify the distance away from the start of the gradient. Note that the percentage values are optional: If you don’t specify them, the color stops will be evenly spaced along the gradient.

Instead of percentages, you can use any units you like that would make sense in the circumstances. By default, the first and last values are at 0% and 100%, but you can alter their positions too. For example:

background: linear-gradient(#ff0000 66px, #ffffff 67px, #ffffff 133px, #00ff00 134px);

This effect creates three solid color bands from left to right (Figure 4.16). The green color stop is set at 66px down from the top, and everything before it adopts the same color. The red color stop is set at 134px, and everything after it adopts the same color. I also inserted two white color stops in the middle to force the middle band to be completely white. This technique is very useful, especially if you want to start creating more intricate and interesting repeating background patterns, as you’ll read about later in the “Multiple Backgrounds” section.

Figure 4.16

Figure 4.16 Italiano, pasta, meatballs, Roma Roma (well, not quite).

You can even use negative unit values if for some reason you want the linear gradient to start or end outside the container. (You might want to change the gradient on hover. Unfortunately, you can’t smoothly animate a gradient, at least not at the time of this writing. Believe me, I’ve tried.)

Again, I’ll extol the awesomeness of transparent colors by providing a very simple gradient with a vital difference (Figure 4.17):

background: linear-gradient(to top right, rgba(0,0,0,0.6),rgba(0,0,0,0));
background-color: #ff0000;
Figure 4.17

Figure 4.17 RGBA colors provide great control over gradients while blending them into their surroundings.

Here the gradient is a transparency gradient overlaid onto a solid background color to create the different gradient colors. This is a very powerful technique because it means you can control the look of an entire site section just by varying the background color. It’s perfect if you want to vary the look of different pages on a site with minimum effort. Try it!

Repeating Linear Gradients

Repeating linear gradients have a similar syntax to linear gradients. Look at the following example and the result in Figure 4.18:

background: repeating-linear-gradient(to top right, rgba(0,0,0,0.4) 10px ,rgba(0,0,0,0) 20px, rgba(0,0,0,0.4) 30px);
background-color: #ff0000;
Figure 4.18

Figure 4.18 A simple repeating gradient.

Only 30 pixels’ worth of gradient has been specified, but it is repeated over and over again until the end of the container is reached.

Radial Gradients

Radial gradients work a bit differently than linear gradients. Instead of traveling across a container from one side to another, they radiate outwards from a single point. Here is a simple example:

background: radial-gradient(50% 50%, 60% 60%, rgb(75, 75, 255), rgb(0, 0, 0));

This produces the result shown in Figure 4.19 (if you want to experiment with this code, download the radial-gradient-test.html file in the chapter4 folder).

Figure 4.19

Figure 4.19 A simple radial gradient.

The syntax is a little different than that of linear gradients, so let’s go through the radial gradient syntax step by step.

Radial Gradient Position

The first two values in the syntax (50% 50% in the preceding code) dictate the location of the origin of the radial gradient: The first value is the horizontal position inside the container, and the second value is the vertical. In the preceding example, the radial gradient equates to 50% across from the left side and 50% down from the top, which places it slap bang in the middle of the container. As with linear gradients, you can use any unit values that make sense, even negative unit values.

You can also use keywords in place of unit values in the same manner as you learned earlier but with the addition of center if you want the horizontal or vertical position to be centered in the container (this doesn’t make sense for linear gradients, but it does for radial gradients). Figure 4.20 shows a few examples.

Figure 4.20

Figure 4.20 From left to right:top left, bottom center, and right positioning of a gradient. When only one keyword is supplied, it is assumed to be the horizontal keyword, and the vertical one is given a value of center.

Radial Gradient Size and Shape

The second set of values in the radial gradient syntax (60% 60% in the example) dictates the size of the gradient—the horizontal and vertical radius size. Because you are working with the radius rather than the diameter, 50% or 60% will produce a nice spread across a container. 100% would be double the width/height of the container, swamping it entirely, which may or may not be the effect you want (Figure 4.21).

Figure 4.21

Figure 4.21background: radial-gradient(50% 50%, 100% 100%, rgb(75, 75, 255), rgb(0, 0, 0)); swamps the container.

Again, to set these values, you can use any units that make sense. You can also use different values for the horizontal and vertical radii, for example:

background: radial-gradient(50% 50%, 70% 40%, rgb(75, 75, 255), rgb(0, 0, 0));

This effect is shown in Figure 4.22.

Figure 4.22

Figure 4.22 Creating an ellipse using different vertical and horizontal radius values. Neo’s amphetamine lunch?

But as usual, there are more ways to set the radii: CSS3 supplies several keywords for setting the radii, which need explaining because they are a bit confusing. Consider the following examples (Figure 4.23):

background: -o-radial-gradient(30% 50%, circle closest-side, rgb(75, 75, 255), rgb(0, 0, 0));
background: -o-radial-gradient(30% 50%, ellipse closest-side, rgb(75, 75, 255), rgb(0, 0, 0));
Figure 4.23

Figure 4.23 The effects of circle closest-side and ellipse closest-side.

So, what’s going on here? By using circle and ellipse, you specify that you want your gradient to be a circle or an ellipse, respectively. closest-side means that the shape will expand so that it just touches the container side closest to the point of origin of the radius in the case of a circle and the horizontal and vertical container sides closest to the point of origin of the radius in the case of an ellipse.

Other keyword combinations available to you include:

  • closest-corner positions the gradient so that its edge just touches the corner of the element closest to the origin (Figure 4.24).
    Figure 4.24

    Figure 4.24 The effects of circle closest-corner andellipse closest-corner.

  • farthest-side positions the gradient so that its edge touches the side of the element farthest from its centre in the case of a circle or the farthest horizontal and vertical sides in the case of an ellipse (Figure 4.25).
    Figure 4.25

    Figure 4.25 The effects of circle farthest-side and ellipse farthest-side.

  • farthest-corner positions the gradient so that its edge just touches the corner of the element farthest from the origin. You can use the keyword cover in place of farthest-corner (Figure 4.26).
    Figure 4.26

    Figure 4.26 The effects of circle farthest-corner and ellipse farthest-corner.

I’ve not used these implicit shape values very much, preferring instead to control the shape using explicit unit values. But this doesn’t mean you won’t.

Radial Gradient Color Stops

Color stops work in the same way as the color stops in linear gradients except that the units you specify denote distance from the center of the gradient, not distance from the starting corner/edge.

I encourage you to experiment with different color values with the examples I’ve provided and otherwise. Try creating a sun’s rays or a shadow or flashlight moving across the top of your site. Again, use RGBA colors for the win! You’ll see more exciting examples throughout the book.

Repeating-Radial-Gradient

As with linear gradients, you can also create repeating radial gradients by adding repeating values into the syntax (Figure 4.27):

background: -o-repeating-radial-gradient(50% 50%, 60% 60%, rgba(75, 75, 255,0.5) 10px, rgba(0, 0, 0,0.5) 20px);
background-color: #ff0000;
Figure 4.27

Figure 4.27 A simple repeating radial gradient.

Providing Gradient Support for Old Versions of IE

CSS3PIE also adds support for CSS gradients. But again, you need to be careful of its limited RGBA support. To use CSS3PIE, target a separate, nontransparent color gradient to IE using a special -pie- prefixed background property (bear in mind that CSS3PIE doesn’t add support for background-image, just the shorthand). Look at the following example from the Monty Python blog (I’ve removed all the less interesting and prefixed properties for brevity):

aside article {
    ...
background: repeating-linear-gradient(45deg, rgba(0,0,0,0.1) 1px, rgba(0,0,0,0.05) 2px, rgba(0,0,0,0.1) 3px, rgba(0,0,0,0) 4px, rgba(0,0,0,0) 5px);
background-color: rgba(255,255,255,0.4);
border-radius: 4px;
box-shadow: 2px 2px 10px black;
}

The result is a rather nice container with a shadow, rounded corners, and a textured repeating gradient pattern (Figure 4.28).

Figure 4.28

Figure 4.28 An attractive bling box.

To add IE support after you’ve placed the PIE.htc file, you can add the following two lines, which include a far simpler gradient without an alpha channel that is still in keeping with the color scheme (Figure 4.29):

-pie-background: linear-gradient(45deg, #6988af, #a6b9cf);
behavior: url(/cmills/arthur/script/PIE.htc);
Figure 4.29

Figure 4.29 A pleasing alternative set of styling provided for older versions of IE.

  • + Share This
  • 🔖 Save To Your Account