How to add gradients to your project using CSS ? - GeeksforGeeks

25 Oct, 2021
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
CSS Gradient
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.
Syntax:
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.
HTML
<!DOCTYPE html><html>  <head>    <style>        #grad1 {            line-height: 5rem;            font-size: 20px;            text-align: center;            height: 100px;            background-color: red;            background-image:                 linear-gradient(0deg, green, white);        }          #grad2 {            line-height: 5rem;            font-size: 20px;            height: 100px;            background-color: red;            background-image:                 linear-gradient(90deg, pink, blue);        }    </style></head>  <body>    <h1>Linear Gradients - Using Different Angles</h1>    <div id="grad1" style="text-align: center">        0deg    </div>    <br />          <div id="grad2" style="text-align: center">        90deg    </div>    <br /></body>  </html>
Output:
Example 2: This example illustrates the use of the linear-gradient() function by using multi-color from right to left Linear Gradient.
HTML
<!DOCTYPE html><html>  <head>    <style>        #grad {            width: 100%;            height: 200px;            text-align: center;            font-size: 50px;            color: white;            line-height: 170px;            font-weight: bold;            background-image: linear-gradient(                to right, orange, #ec38bc,                #7303c0, cyan);        }    </style></head>  <body>    <div id="grad">        geeksforgeeks    </div></body>  </html>
Output:
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.
HTML
<!DOCTYPE html><html>  <head>    <style>        #grad {            width: 100%;            height: 200px;            text-align: center;            font-size: 50px;            color: black;            line-height: 170px;            font-weight: bold;            background-image: linear-gradient(to right,                    rgba(255, 0, 0, 0),                    rgba(206, 16, 225, 0.58));        }    </style></head>  <body>    <div id="grad">GeeksforGeeks</div></body>  </html>
Output:
transparency 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.
HTML
<!DOCTYPE html><html>  <head>    <style>        body {            background: #f2f2f2;        }          #grad {            height: 200px;            width: 100%;            text-align: center;            font-size: 50px;            line-height: 170px;            font-weight: bold;            background-image: linear-gradient(to right,                    #307e2b,                    #20e3b2,                    #29ffc6);            -webkit-background-clip: text;            background-clip: text;            color: transparent;        }    </style></head>  <body>    <div id="grad">GeeksforGeeks</div></body>  </html>
Output:
Gradient on 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.
Syntax:
background-image: radial-gradient(
    shape size at position, start-color, ..., last-color);
Example: This example illustrates the use of the radial-gradient() function.
HTML
<!DOCTYPE html><html>  <head>    <style>        #grad {            height: 200px;            width: 100%;            text-align: center;            font-size: 50px;            color: black;            line-height: 170px;            font-weight: bold;            background-image: radial-gradient(                circle at top left,                #fdbb2d,                #22c1c3);        }    </style></head>  <body>    <div id="grad">GeeksforGeeks</div></body>  </html>
Output:
Radial Gradient
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.
Syntax:
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.
HTML
<!DOCTYPE html><html>  <head>    <title>conic gradient</title>    <style>        .box {            background-color: yellow;            height: 200px;            width: 200px;            float: left;            margin: 20px;            border-radius: 50%;        }          .c {            background-image: conic-gradient(                red, yellow, green, red);        }          .a {            background-image: conic-gradient(                red, yellow, green);        }    </style></head>  <body>    <h1>GeeksforGeeks</h1>    <h3>Conic-gradient</h3>    <div class="box c"></div></body>  </html>
Output:
Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.
CSS-Properties
CSS-Questions
HTML-Questions
CSS
HTML
Web Technologies
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to set space between the flexbox ?
Design a web page using HTML and CSS
Form validation using jQuery
Search Bar using HTML, CSS and JavaScript
How to style a checkbox using CSS?
How to set the default value for an HTML <select> element ?
Hide or show elements in HTML using display property
How to set input type date in dd-mm-yyyy format using HTML ?
REST API (Introduction)
How to Insert Form Data into Database using PHP ?