background-image: linear-gradient(purple, pink);
}
Note : Internet Explorer 9 and earlier versions do not support gradients.
2. Linear Gradient - Left to Right
The following example shows a Linear Gradient starting from the left. Linear Gradient starts with purple and changes to pink:
#grad {
background-image: linear-gradient(to right, purple, pink);
}
3. Linear Gradient - Diagonal (Diagonal Direction)
You can create a diagonal gradient by defining both vertical and horizontal directions.
The following example shows a Linear Gradient starting from the top left (and going to the bottom right). Linear Gradient starts with purple and changes to pink:
#grad {
background-image: linear-gradient(to bottom right, purple, pink);
}
You can choose another way to format the gradient that is to use an angle instead of defining the direction (to bottom, to top, to right, to left, to bottom right .) using the following syntax :
background-image: linear-gradient(angle, color-stop1, color-stop2);
Parameters:
Example: Use angle in Linear Gradient
#grad {
background-image: linear-gradient(-90deg, purple, pink);
}
The following example shows a Linear Gradient (from top to bottom) with multiple color stops:
3 stops of color are equally spaced
#grad {
background-image: linear-gradient(indigo, crimson, pink);
}
7 stops color equally
#grad {
background-image: linear-gradient(crimson, lightsalmon, gold, seagreen,
midnightblue, indigo, violet);
}
3 color stops are not evenly spaced
#grad {
background-image: linear-gradient(crimson 10%, indigo 55%, violet 80%);
}
Note: If you do not specify a% value after each color stop, the gradient is automatically spaced.
Full code:
7 color stops are equally spaced from left to right
#grad {
background-image: linear-gradient(to right, crimson, lightsalmon, gold,
seagreen, midnightblue, indigo, violet);
}
In addition to the above gradients, you can use transparency - transparency in CSS to create gradient gradation effects.
To add transparency to the color, you use RGBA Color as introduced in the color-intensive lesson to identify color stops. The last parameter in RGBA () specifies the opacity / transparency of the color, with a value between 0.0 and 1.0, the smaller the value, the more transparency.
For example: A background linear gradient starts with a completely transparent color and gradually changes to purple:
#grad {
background-image: linear-gradient(to right, rgba(88,37,123,0),
rgba(88,37,123,1));
}
The repeating-linear-gradient () function is used to repeat Linear Gradient.
#grad {
background-image: repeating-linear-gradient(indigo, crimson 10%, pink 20%);
}
#grad {
background-image: repeating-linear-gradient(45deg, indigo, crimson 7%,
pink 10%);
#grad {
background-image: repeating-linear-gradient(190deg, indigo, crimson 7%,
pink 10%);
#grad {
background-image: repeating-linear-gradient(90deg, indigo, crimson 7%,
pink 10%);
Full code:
Corner 45deg:
Angle 190deg:
90deg angle:
Radial Gradient creates a transition effect from the center of the element .
To create a Radial Gradient you must also specify at least two color stops.
background-image: radial-gradient(shape-size-at position, start-color. last-color);
Parameters:
1. Stops colors equally
#grad {
background-image: radial-gradient(bisque, crimson, indigo);
}
2. Color stops are not evenly spaced
#grad {
background-image: radial-gradient(bisque 5%, crimson 20%, indigo 65%);
}
The shape parameter is used to determine the shape of the gradient , which can be either a circle value or an ellipse.
For example, a gradient originating from the center with a shape is a circle:
#grad {
background-image: radial-gradient(circle, bisque, crimson, indigo);
}
The size parameter determines the size of the gradient. Possible values:
closest-side:
#grad1 {
background-image: radial-gradient
(closest-side at 60% 55%, bisque,
crimson, indigo);
}
farthest-side:
#grad2 {
background-image: radial-gradient
(farthest-side at 60% 55%, bisque,
crimson, indigo);
}
closest-corner:
#grad3 {
background-image: radial-gradient
(closest-corner at 60% 55%, bisque,
crimson, indigo);
}
farthest-corner (default):
#grad4 {
background-image: radial-gradient
(farthest-corner at 60% 55%, bisque,
crimson,indigo);
}
Full code:
The repeating-radial-gradient () function is used to repeat the Radial Gradient.
#grad {
background-image: repeating-radial-gradient(bisque, crimson 10%, indigo 15%);
}
Previous lesson: Deep understanding of Color in CSS
Next lesson: Shadow effect in CSS