How to add gradients to your project using CSS ?
The Gradient in CSS is a special type of image that is made up of progressive & smooth transition between two or more colors. The gradient can be formed in 3 ways:
- Linear-gradient: It is an inbuilt function in CSS that is used to set the linear gradient as the background image.
- Radial-gradient: It is an inbuilt function in CSS that is used to set a radial gradient as the background image.
- Conic-gradient: It is an inbuilt function in CSS that is used to set a conic gradient as the background image.
Gradients are generally merging two or more colors in each other, although, in CSS, you can control those changes. For instance, you can change the direction, angle, position, fading intensity, etc. As gradients are simply a background image, so
we can set the background image as either a linear gradient or a radial gradient, ultimately passing it into the starting or the ending colors.
There is a progressive transition between the colors in an image along a straight line in the case of Linear-gradient function whereas progressive transition between the colors in an image along a radius(radiate from an origin) in the case of radial-gradient function. The conic gradient angle starts from 0 degrees – 360 degrees. Conic are circular and use the center of the element as the source point for color stop. In this article, we will learn all these kinds of gradients in CSS & will also understand its implementation through the examples.
Color stops: Color stops inform the browsers that what color to use, at the starting point of the gradient & where to stop. By default, they are equally spaced but we can overrule it by providing the specific color stops.
There are various ways for creating a gradient, a few of them are given below:
- Angle: You can specify a gradient CSS property using angles. Here 0deg represents upward direction, positive values represent the clockwise rotation. As we increase the value of a degree in a positive direction say moving from 0deg to 90 deg, the color stops move from upward towards the right and hence 90deg would represent the right direction.
linear-gradient(30deg, red, blue);
- Side or Corner: It consists of two words to and up to(two keywords). One specifies the horizontal side i.e. left or right and the other specifies the vertical side i.e. top and bottom. If unspecified, it takes ‘to bottom’ by default.
linear-gradient(to right, red, green);
- Percentage: You can also specify the gradient property in the percentage value. They are color hints( also called color interpolation hints), which specify how the two color stops on either side should be interpolated in the space between them.
linear-gradient ( to top, red 10%, purple 45%);
Let’s discuss & understand all the categories of the gradient sequentially.
Linear Gradient: It includes the smooth color transitions to going up, down, left, right, and diagonally. The minimum two-color required to create a linear gradient. More than two-color elements can be possible in linear gradients. The starting point and the direction are needed for the gradient effect. The default direction is top to bottom.
background-image: linear-gradient( direction, color-stop1, color-stop2, ...);
Example 1: This example illustrates the use of the linear-gradient() function for using it in different angles.
Example 2: This example illustrates the use of the linear-gradient() function by using multi-color from right to left Linear Gradient.
Example 3: This example illustrates the use of the linear-gradient() function by using transparent gradient using rgba( ) function.
The text gradient: To add a gradient to the text, we use the background-clip property. For this, first, write your selected text and then apply a gradient on its background. Background-clip property removes the background except for the region behind the text. Then we make the text color transparent so that the gradient can clearly be seen upon the text.
Example 4: This example illustrates the use of the Background-clip property to remove the background except for the region behind the text.
Radial Gradients: It is an inbuilt function in CSS that is used to set a radial gradient as the background image. It starts at a single point and emanates outward. By default, the first color starts at the center position of the element and then fades to the end color towards the edge of the element. Fade happens at an equal rate until specified. It can be used for making concentric circles that start from a single point(center) and spread outwards.
background-image: radial-gradient( shape size at position, start-color, ..., last-color);
Example: This example illustrates the use of the radial-gradient() function.
Conic-gradient: It is an inbuilt function in CSS that is used to set a conic gradient as the background image. The conic gradient angle starts from 0 degrees – 360 degrees. Conic are circular and use the center of the element as the source point for color stop.
Conic Gradients include pie charts and color wheels. The result of the conic-gradient() function is an object of the data type, which is a special kind of image.
Background image: conic-gradient(color degree, color degree, ...);
Conic gradients are similar to radial gradients, except that the color stops are on the outer edge of the circle that gets created.
Example: This example illustrates the use of the conic-gradient() function.