Fun with CSS gradients
The fact that we can provide color gradients with the help of CSS is already a well-known fact. In terms of performance, it is also a lot better to use CSS gradients instead of (background) images. But we can also use CSS gradients for more complex things.
Usually it is one color that transitions to another color, but CSS allows us to control every aspect of how this happens. This way we can also indicate the direction and the shape. There are three types we can use for this: linear, radial and conic.
Linear gradients
Linear gradients may be the most commonly used. They are simple transitions that go from one side to the other, for example from left to right or from top to bottom.
Note that we did not provide the direction in the example above. By default CSS will display a linear transition from top to bottom, thus to bottom
, or 180deg
. So we could also have written this as follows, to explicitly indicate it.
We can easily change the direction by specifying the direction itself, in plain text. We have the choice between bottom
, top
, left
, right
and combinations of two directions, for example to top left
to go diagonally. If you still want to deviate from these eight directions, you can always enter the number of degrees. Some examples:
You want more than two colors? No problem, you can use multiple colors in the same gradient!
Radial gradients
Radial gradients differ from linear gradients because the starting point is a certain point on the element and not a full edge. You indicate the simplest radial gradient as follows:
Of course, some properties can also be changed here, such as shape
and position
. The first can be useful to use a circle as a gradient in non-square elements. The second one indicates where the gradient should focus. Combined you get, for example, the following:
Conic gradients
Finally, we have conic gradients. These are similar to the radial gradients, but the starting point seems to be elevated, making it look like a cone. Without specifying specific properties, it looks like this:
As you will see below, this is not the result we expect from a cone. This is because the transition starts at 0° by default and ends at 360°. This creates a hard stop. Fortunately we can add a third parameter, an extra color, to counteract this hard stop.
You don't have to stop with three colors, of course. For example, if we add some more colors of the rainbow, then we easily get a full color palette.
Repetitions
We can also repeat linear and radial transitions using CSS. For this we use repeating-linear-gradient
and repeating-radial-gradient
. Some examples:
Nice tricks
Now that we've mastered the basics – and even a little more than that – we can apply some neat tricks. This blog post was entitled "Fun with CSS gradients" for a good reason.
A first trick is to make a chessboard pattern or flannel shirt through repetitions, for example.
A second trick is to also give borders a gradient.
Animations
You could even add animations and create a sunrise. Below, move your cursor over the sea and the sky to see the result.