When we talk about gradients in CSS, we talk about gradients of colors.
There are 2 types of gradients in CSS:
- linear: colors go from point to another, in a straight line
- radials: colors go from the center of a circle to its edges, in all directions
A gradient is considered a background image and must be used with the according property.
linear-gradient
The syntax for linear gradients is quite complex, but the basic idea is to define:
- which colors you want
- where these colors must appear along the axis (at the start, middle, end, etc.)
- in which direction the gradient must go
Let’s start with a simple 2 color gradient:
By default:
- the direction is vertical, from top to bottom
- the first color is at the start (top)
- the second color is at the end (bottom)
Changing the direction
If the top to bottom direction doesn’t suit you, you can alter it by either:
- defining the destination of the gradient, with keywords like
to left top
- defining a specific angle in degrees like
45 deg
This direction must be set before the colors:
If you want a more specific angle, you can use a value in degrees:
0deg
is the default value, from top to bottom20deg
is slightly diagonal, going clockwise90deg
is like 3pm, from right to left180deg
is from bottom to top
Adding more colors
You can insert as many colors as you want. They will be equally distributed along the axis:
- 2 colors: 0% and 100%
- 3 colors: 0%, 50% and 100%
- 4 colors: 0%, 33%, 67% and 100%
Setting specific color stops
If you don’t want colors to equally distributed, you can set specific color stop positions, using either percentages %
or pixels px
:
In this setup:
orange
has no stop position, so it defaults to zero0%
grey
is closer to the top, at10%
instead of50%
yellow
takes half of the gradient, from50%
to the end100%
radial-gradient
While linear gradients follow a single-line axis, radial gradients spread out in all directions. Their syntax is fairly similar to linear ones, as they both have color stops. But instead of specifying a direction you need to specify:
- a shape: either a circle or an ellipse
- a starting point: which will be the center of the circle/ellipse
- an end point: where the edge of the circle/ellipse will be
By default:
- the gradient is an ellipse
- the first color starts at the center
- the last color ends at the farthest corner
start position
The start position works like background positions. You set it with the at
keyword.
end position
By default, the shape will end at the farthest corner. You can either choose:
closest-side
closest-corner
farthest-side
farthest-corner
The difference is both hard to grasp and to visualize, so I won’t go into details. Mozilla has a good description of the different values.
fixed size
Instead of setting both start and end positions, you can just set specific dimensions:
CSS gradients are powerful, considering how endless the options are.
The examples of this page are voluntarily “ugly”, with pronounced color differences, to better explain what how each property works.
But it’s quite easy to write more subtle gradients, especially for buttons: