Designers tend to think of gradients as smooth distributions of color between two or more hues: black to white with shades of grey between. But by using color stops that are very close to each other, we can make these transitions far more abrupt in CSS, and provide an easier, more dynamic, smaller (in terms of file size) and faster method for creating backgrounds without images.
- body{ background: -moz-linear-gradient(top, #aebcbf 0%, #6e7774 50%,
- #0a0e0a 51%, #0a0809 100%);
- background: -webkit-linear-gradient(top, #aebcbf 0%, #6e7774 50%, #0a0e0a 51%,
- #0a0809 100%); }
The CSS above produces the effect shown to the right. There are, however, two problems. Because the background does not have a set size and we have not specified that it repeats, the gradient will default to repeating at intervals of however high the body content is. Even with the body set to min-height: 100%,you would only see the background gradient once. Also, the transition between colours on the edges of the gradient is a little smooth: we want a sharp discontinuity. The solution for the first problem is simple: set a background-size of however large you want a single “tile” of the gradient to be:
- body {
- background: -moz-linear-gradient(top, #aebcbf 0%, #6e7774 50%, #0a0e0a 51%,
- #0a0809 100%);
- background: -webkit-linear-gradient(top, #aebcbf 0%, #6e7774 50%,
- #0a0e0a 51%, #0a0809 100%);
- -moz-background-size: 100px 100px;
- background-size: 100px 100px; }
The second is to place the points at which the colours in the gradient transition directly on top of each other. Here I’ll use shades of red as contrasting colours, while reducing the size of the tiles:
- body { background: -moz-linear-gradient(top, #500 0%, #500 50%, #300 50%, #300 100%);
- background: -webkit-linear-gradient(top, #500 0%, #500 50%, #300 50%, #300 100%);
- -moz-background-size: 50px 50px; background-size: 50px 50px; }
Creating a plaid pattern is simply a case of using CSS 3’s feature of multiple backgrounds: placing one gradient horizontal and the other vertical, and using rgba values and transparency to allow parts of the first background to show through the second:
- body {
- background-color: #800;
- background-image: -moz-linear-gradient(rgba(253, 153, 0, 0.5) 0%,
- rgba(253, 153, 0, 0.5) 50%, rgba(253, 153, 0, 0.7) 50%,
- rgba(253, 153, 0, 0.7) 100%),
- -moz-linear-gradient(left, rgba(253, 153, 0, 0.5) 0%, rgba(253, 153, 0, 0.5) 50%,
- rgba(253, 153, 0, 0.7) 50%,
- rgba(253, 153, 0, 0.7) 100%);
- background-image: -webkit-linear-gradient(rgba(253, 153, 0, 0.5) 0%,
- rgba(253, 153, 0, 0.5) 50%, rgba(253, 153, 0, 0.7) 50%,
- rgba(253, 153, 0, 0.7) 100%),
- -webkit-linear-gradient(left, rgba(253, 153, 0, 0.5) 0%, rgba(253, 153, 0, 0.5) 50%,
- rgba(253, 153, 0, 0.7) 50%, rgba(253, 153, 0, 0.7) 100%);
- -moz-background-size: 100px 100px; background-size: 100px 100px; }
Unlike images, gradients can be placed on an angle in a background with CSS:
- body { background-color: #800; background-image:
- -moz-linear-gradient(45deg, rgba(0, 0, 0, 0.5) 0%,
- rgba(0, 0, 0, 0.5) 50%, rgba(0, 0, 0, 0) 50%, rgba(0, 0, 0, 0) 100%);
- }
Due to the lack of background-size, we’re seeing an enormous single “tile” of red and dark red at a 45deg angle. Try adding the background-size property to the declaration: it will not lead to the result you might expect.
To make it clearer, we’ll explicitly tell the browser that we are repeating the background gradient:
- body { background-color: #800;
- background-image: -moz-repeating-linear-gradient(-45deg, rgba(0, 0, 0, 0.5),
- rgba(0, 0, 0, 0.5) 12px, rgba(0, 0, 0, 0) 12px, rgba(0, 0, 0, 0) 15px);
- -moz-background-size: 22px 22px;
- background-image: -webkit-repeating-linear-gradient(-45deg, rgba(0, 0, 0, 0.5),
- rgba(0, 0, 0, 0.5) 12px, rgba(0, 0, 0, 0) 12px, rgba(0, 0, 0, 0) 15px);
- -webkit-background-size: 22px 22px;
- background-size: 22px 22px;
- }
Playing around with these values can produce unique and unexpected results, some of which we'll cover in the next entry.

so we don't need the jQuery minimum length anymore:) cool!


