CSS Gradients

CSS gradients let you display smooth transitions between two or more specified colors.

CSS defines three types of gradients:

  • Linear Gradients – goes down, up, left, right, or diagonally
  • Radial Gradients – defined by their center
  • Conic Gradients – rotated around a center point

CSS Linear Gradients

To create a linear gradient, you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

Syntax

background-image: linear-gradient(direction, color-stop1, color-stop2, ...);

Direction: Top to Bottom (this is the default)

Example

The following example shows a linear gradient that starts at the top. It starts red and transitions to yellow:

background-image: linear-gradient(to bottom, red, yellow);
Output :

Direction - Left to Right

The following example shows a linear gradient that starts from the left. It starts red, transitioning to yellow:

Output :

Direction - Diagonal

You can make a gradient diagonally by specifying both the horizontal and vertical starting positions.

The following example shows a linear gradient that starts at the top left and goes to the bottom right. It starts red and transitions to yellow:

background-image: linear-gradient(to bottom right, red, yellow);
Output 3:

Using Angles

If you want more control over the direction of the gradient, you can define an angle instead of the predefined directions (to bottom, to top, to right, to left, to bottom right, etc.).

A value of 0deg is equivalent to to top.
A value of 90deg is equivalent to to right.
A value of 180deg is equivalent to to bottom.

Syntax

background-image: linear-gradient(angle, color-stop1, color-stop2);

Example

The following example shows how to use angles on linear gradients:

background-image: linear-gradient(45deg, red, yellow);
Output :

Using Multiple Color Stops

The following example shows a linear gradient (from top to bottom) with multiple color stops:

background-image: linear-gradient(to bottom, red, orange, yellow, green, blue, indigo, violet);
Output:-: