In previous articles I’ve shown how to make gradient backgrounds with CSS3, no images required. I’ve also demonstrated how to make more complex visual effects by using CSS3’s multiple backgrounds feature to layer gradients on top of each other.
Today, I’m going to take things a step further: if we add images to a background, and if the gradients we add have areas of transparency or opacity, we’ll see portions of the image through the gradient, producing a "pop-art" effect.
(Note that I’m using the most up-to-date version of the CSS3 gradient syntax for the declarations below; the most significant change is one of simplicity when specifying gradients in Safari and Chrome, so the code as shown will only work in the most recent version of browsers, although it would be easy enough to use extra vendor prefixes and specialized values to target older browsers.)
The most important point in writing the style rules is that the image must be added last: remember, in CSS3 background layers are added in reserve order, so when we specify our image at the end of the background effects, it appears to be on the bottom, with the gradients layered on top.
Checkerboard
For example, to create a checkerboard gradient effect, the CSS for Webkit would be:
- html, body { min-height: 100%; margin: 0; }
- body {
- background-image:
- -webkit-linear-gradient(45deg, #000 25%, transparent 25%),
- -webkit-linear-gradient(-45deg, #000 25%, transparent 25%),
- -webkit-linear-gradient(45deg, transparent 75%, #000 75%),
- -webkit-linear-gradient(-45deg, transparent 75%, #000 75%);
- background-size:200px 200px;
- background-position:0 0, 100px 0, 100px -100px, 0px 100px;
- }
Adding opacity, Firefox support and an image would bring the CSS to:
- html, body { min-height: 100%; margin: 0; }
- body {
- background:
- -moz-linear-gradient(45deg, rgba(0,0,0,0.2) 25%, rgba(0,0,0,0) 25%),
- -moz-linear-gradient(-45deg, rgba(0,0,0,0.2) 25%, rgba(0,0,0,0) 25%),
- -moz-linear-gradient(45deg, rgba(0,0,0,0) 75%, rgba(0,0,0,0.2) 75%),
- -moz-linear-gradient(-45deg, rgba(0,0,0,0) 75%, rgba(0,0,0,0.2) 75%),
- url(grace-kelly.jpg);
- background:
- -webkit-linear-gradient(45deg, rgba(0,0,0,0.2) 25%, rgba(0,0,0,0) 25%),
- -webkit-linear-gradient(-45deg, rgba(0,0,0,0.2) 25%, rgba(0,0,0,0) 25%),
- -webkit-linear-gradient(45deg, rgba(0,0,0,0) 75%, rgba(0,0,0,0.2) 75%),
- -webkit-linear-gradient(-45deg, rgba(0,0,0,0) 75%, rgba(0,0,0,0.2) 75%),
- url(grace-kelly.jpg);
- background-size:200px 200px, 200px 200px, 200px 200px, 200px 200px, cover;
- background-position: 0 0, 100px 0, 99px -101px, 0 100px, 0 0;
- }
Note that if we add a feature of a different size, we must repeat background-size for all elements. Also note that there is a known rendering bug in Safari, Chrome and Firefox that leaves a very faint diagonal mark in some boxes. The slightly-off values in background-position above are an attempt to minimize that, but they may still appear.
Hypnotic Circles
We could also create a repeating radial gradient layered on top of a background image:
- html, body { min-height: 100%; margin: 0; }
- body { background: -moz-repeating-radial-gradient(rgba(0,0,0,0.3),
- rgba(0,0,0,0.3) 5%, transparent 5%, transparent 10%),
- url(ava-gardner.jpg);
- background: -webkit-repeating-radial-gradient(rgba(0,0,0,0.3),
- rgba(0,0,0,0.3) 5%, transparent 5%, transparent 10%),
- url(ava-gardner.jpg);
- background-position: -20px 20px, 0 0;
- background-repeat: no-repeat, no-repeat;
- background-size: cover;
- }
Polka Dot Pop Art
We could also create a repeating radial gradient layered on top of a background image:
- body {
- background:
- -moz-radial-gradient(rgba(0, 0, 0, 0) 45%, rgba(0, 0, 0, 0.3) 46%),
- -moz-radial-gradient(rgba(0, 0, 0, 0) 45%, rgba(0, 0, 0, 0.3) 46%),
url(gilda.jpg); - background:
- -webkit-radial-gradient(rgba(0, 0, 0, 0) 45%, rgba(0, 0, 0, 0.3) 46%),
- -webkit-radial-gradient(rgba(0, 0, 0, 0) 45%, rgba(0, 0, 0, 0.3) 46%),
- url(gilda.jpg);
- background-position: 0 0, 80px 80px;
- background-size:160px 160px, 160px 160px, cover;
- }

Haha, that is actually incredibly clever.
![Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy] Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]](http://ecx.images-amazon.com/images/I/5192I1rtYnL._SL160_.jpg)

