Prev Page Next Page
Top

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/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.

Direction - Top to Bottom (this is default)


EXAMPLE:

#grad {
     background-image: linear-gradient(green, lightgreen);
     height: 200px;
     background-color: red;

}

RESULT:

Linear Gradient - Top to Bottom

Run


Direction - Left to Right


EXAMPLE:

#grad {
     background-image: linear-gradient(to right, green, lightgreen);
     height: 200px;
     background-color: red;

}

RESULT:

Linear Gradient - Top to Bottom

Run


Direction - Diagonal


EXAMPLE:

#grad {
     background-image: linear-gradient(to bottom right, green, lightgreen);
     height: 200px;
     background-color: red;

}

RESULT:

Linear Gradient - Top to Bottom

Run


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".

EXAMPLE:

#grad {
     background-image: linear-gradient(0deg, green, lightgreen);
     height: 100px;
     background-color: green;

}

#grad {
     background-image: linear-gradient(90deg, green, lightgreen);
     height: 100px;
     background-color: green;

}

#grad {
     background-image: linear-gradient(180deg, green, lightgreen);
     height: 100px;
     background-color: green;

}

#grad {
     background-image: linear-gradient(-90deg, green, lightgreen);
     height: 100px;
     background-color: green;

}

RESULT:

0deg


90deg


180deg


-90deg

Run


Using Multiple Color Stops

➢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".

EXAMPLE:

#grad {
     background-image: linear-gradient(0deg, green, lightgreen, orange, red);
     height: 100px;
     background-color: green;

}

RESULT:

Multiple colors


Run


Using Transparency

➢CSS gradients also support transparency, which can be used to create fading effects.
➢To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).

EXAMPLE:

#grad {
     background-image: linear-gradient(to right, rgba(255,0,0,0), green);
     height: 200px;

}

RESULT:

Run


CSS Radial Gradients

➢A radial gradient is defined by its center.
➢To create a radial gradient you must also define at least two color stops.

Radial Gradient - Evenly Spaced Color Stops (this is default)


EXAMPLE:

#grad {
     background-image: radial-gradient(black, red, yellow, green);
     height: 200px;

}

RESULT:

Run


Radial Gradient - Differently Spaced Color Stops


EXAMPLE:

#grad {
     background-image: radial-gradient(black 10%, red 20%, yellow 30%, green 40%);
     height: 200px;

}

RESULT:

Run


Conic Gradient

➢A conic gradient is a gradient with color transitions rotated around a center point.
➢To create a conic gradient you must define at least two colors.

EXAMPLE:

#grad {
     background-image: conic-gradient(black, red, yellow, green);
     height: 200px;

}

RESULT:

Run


Create Pie Charts


EXAMPLE:

#grad {
     background-image: conic-gradient(black, red, yellow, green);
     height: 200px;
     border-radius: 50%;

}

RESULT:

Run


Conic Gradient With Specified From Angles


EXAMPLE:

#grad {
     background-image: conic-gradient(from 90deg, black, red, yellow, green);
     height: 200px;

}

RESULT:

Run


Conic Gradient With Specified Center Position


EXAMPLE:

#grad {
     background-image: conic-gradient(at 60% 45%g, black, red, yellow, green);
     height: 200px;

}

RESULT:

Run


Prev Page Next Page





FEEDBACK