CSS Linear Gradient (Summary)

CSS Linear Gradient (Summary)

CSS Gradients

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

CSS Gradient Types:

  • Linear Gradients (goes down/up/left/right/diagonally)

  • Radial Gradients (defined by their center)

Linear Gradients

Syntax

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

Code Sample:

#gradientsample {
  background-image: linear-gradient(to right, red , yellow);
}

Directions

  • to left
  • to right
  • to bottom right
  • to bottom left

Using Angles

Syntax:

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

Code Sample:

#gradientsample {
  background-image: linear-gradient(180deg, red, yellow);
}

Using Transparency

CSS gradients also support transparency, which can be used to create fading. To add transparency, introduce rgba() function.

Code Sample

#gradientsample {
  background-image: linear-gradient(to right, rgba(255,0,0,0), rgba(255,0,0,1));
}

Repeating a linear-gradient

The repeating-linear-gradient() function is used to repeat linear gradients:

Code Sample:

#gradientsample {
  background-image: repeating-linear-gradient(red, yellow 10%, green 20%);
}