The Wayback Machine - https://web.archive.org/web/20240618034042/https://www.geeksforgeeks.org/how-to-create-linear-gradient-background-using-css/
Open In App

How to create linear gradient background using CSS ?

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In CSS, we can use the background-color property to set the background color of an element to a specific color. Sometimes we want to add more styling to the element when setting the background color by using the linear-gradient property. CSS linear-gradient property lets you display smooth transitions between two or more colors.

Syntax:

background-image: linear-gradient(direction, color1, color2, ...);

Parameters:

  • direction: Specify the direction of the transition. The default value is 180deg (if not specified).
  • color1: Specify the first color.
  • color2: Specify the second color.

Note: You can specify as many colors as you want.

Example 1: In this example, we are using the above method. 

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        /* Remove the default padding and
           margin of all HTML tags */
        * {
            margin: 0;
            padding: 0;
        }
 
        /* Implementation of linear-gradient property */
        #lin_grad {
 
            /* Set the height of the div to
               the entire screen */
            height: 100vh;
 
            /* linear-gradient syntax */
            background-image: linear-gradient(lightblue, darkblue);
        }
    </style>
</head>
 
<body>
    <div id="lin_grad"></div>
</body>
</html>


Output:

Example 2: This demonstrates the setting of direction. In the above HTML code, just change the “background-image” property value in the CSS part of the head section as given below.

Syntax: 

background-image: linear-gradient(to right, lightblue, darkblue)

Example: Here we are following above explained approach.

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        /* Remove the default padding and
        margin of all HTML tags */
        * {
            margin: 0;
            padding: 0;
        }
 
        /* Implementation of linear-gradient property */
        #lin_grad {
 
            /* Set the height of the div to
            the entire screen */
            height: 100vh;
 
            /* linear-gradient syntax */
            background-image: linear-gradient(to right, lightblue, darkblue)
        }
    </style>
</head>
 
<body>
    <div id="lin_grad"></div>
</body>
</html>


Output:

 

Example 3: This demonstrates the setting of direction in degree. In the above HTML first code, just change the “background-image” property value in the CSS part of the head section as given below.

Syntax:

background-image: linear-gradient(135deg, white, lightgreen, darkblue);

Example: Here we are using the above syntax.

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        /* Remove the default padding and
        margin of all HTML tags */
        * {
            margin: 0;
            padding: 0;
        }
 
        /* Implementation of linear-gradient property */
        #lin_grad {
 
            /* Set the height of the div to
            the entire screen */
            height: 100vh;
 
            /* linear-gradient syntax */
            background-image: linear-gradient(135deg, white, lightgreen, darkblue);
        }
    </style>
</head>
 
<body>
    <div id="lin_grad"></div>
</body>
</html>


Output:

Example 4: This demonstrates the setting of the position for each color. If you don’t specify the location of a color, it is placed halfway between its preceding color and the color that follows it. The two gradients given below are equivalent. In the above HTML first code, just change the “background-image” in the CSS part of the head section as given below.

Syntax:

background-image: linear-gradient(90deg, white, lightgreen, darkblue)
background-image: linear-gradient(90deg, white 0%, 
        lightgreen 50%, darkblue 100%)

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        /* Remove the default padding and
        margin of all HTML tags */
        * {
            margin: 0;
            padding: 0;
        }
 
        /* Implementation of linear-gradient property */
        #lin_grad {
 
            /* Set the height of the div to
            the entire screen */
            height: 100vh;
 
            /* linear-gradient syntax */
            background-image:
                  linear-gradient(90deg, white, lightgreen, darkblue)
                  background-image: linear-gradient(90deg, white 0%,
                    lightgreen 50%, darkblue 100%)
        }
    </style>
</head>
 
<body>
    <div id="lin_grad"></div>
</body>
</html>


Output:

Let us change the % value of the light green color and see what happens.

Syntax:

background-image: linear-gradient(90deg, white 0%, 
    lightgreen 80%, darkblue 100%)

Example:

HTML




<!DOCTYPE html>
<html>
<head>
    <style>
        /* Remove the default padding and
        margin of all HTML tags */
        * {
            margin: 0;
            padding: 0;
        }
 
        /* Implementation of linear-gradient property */
        #lin_grad {
 
            /* Set the height of the div to
            the entire screen */
            height: 100vh;
 
            /* linear-gradient syntax */
            background-image: background-image: linear-gradient(90deg, white 0%,
                    lightgreen 80%, darkblue 100%)
        }
    </style>
</head>
 
<body>
    <div id="lin_grad"></div>
</body>
</html>


Output:



Similar Reads

How to apply background image with linear gradient using Tailwind CSS ?
In this article, we will see how to apply background images with a linear gradient using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. To apply background images with linear gradients we use the background image utility of Tailwind CSS. It is the alternati
2 min read
How CSS transition work with linear gradient background button?
In CSS, you can smoothly make transitions between two or more colors. CSS has two types of gradients: Linear gradient: It goes down/up/left/right/diagonally and makes smooth transitions of colors. To make a linear transition you first have to choose two color stops. Color stops are the color among which you want to make the transitions. You can als
1 min read
How to Create Gradient Background Animation using HTML and CSS ?
Gradient animation can be added to the background of your websites by using simple HTML and CSS @keyframes rule that generate the desired animation. HTML Code: In the following example, the basic structure of the HTML page is implemented. &lt;!DOCTYPE html&gt; &lt;html lang=&quot;en&quot;&gt; &lt;head&gt; &lt;meta charset=&quot;UTF-8&quot;&gt; &lt;
2 min read
How to create linear-gradient color generator using HTML, CSS and JavaScript ?
The background generator can be made by using HTML, CSS, and JavaScript. It will generate a gradient background based on the values that you select. We will add two files namely style.css and script.js to add CSS and JS to our HTML(index.html). We have used an empty h3 tag so that we can display the linear-gradient color codes. HTML: Save the code
2 min read
How to create linear gradient text using HTML and CSS ?
The Linear-Gradient is a kind of text-styling in which the text is filled with linear-gradient color codes. These kinds of effects are generally used in dark-themed websites or apps to make the text look attractive and bold. They are almost suitable for dark themes and do not go well with lighter themes. ApproachPlease refer linear-gradient() metho
2 min read
How to animate gradient background smoothly using CSS ?
Animating gradient backgrounds smoothly using CSS involves defining a gradient background and applying CSS transitions or keyframe animations to smoothly transition between colors, angles, or positions smoothly, creating a visually appealing effect on the webpage. Understanding GradientsBefore going into animation, let's know that Gradients in CSS
2 min read
How to create linear gradient text by using HTML ?
Creating linear gradient text on a webpage can add a dynamic and visually interesting touch to the design. While it is typically created using CSS, it is also possible to create linear gradient text using only HTML. Approach: Using the `&lt;svg&gt;` Element: The `&lt;svg&gt;` element in HTML provides a way to create vector graphics on a webpage. To
2 min read
CSS Combine background image with gradient overlay
CSS gradients allow for smooth transitions between two or more colors and can be combined with background images to create visually appealing designs. By using the background-image property, you can overlay gradients on top of background images to enhance their appearance. Table of Content 1. Using linear-gradient2. Using radial-gradientSyntaxFor l
3 min read
CSS linear-gradient() Function
The linear-gradient() function is an inbuilt function in CSS that is used to set the linear gradient as the background image. Syntax: background-image: linear-gradient( direction, color1, color2, ... )Parameters: This function accepts one direction parameter and many color parameters which are listed below: direction: This parameter is used to defi
2 min read
CSS repeating-linear-gradient() Function
The repeating-linear-gradient() function is an inbuilt function in CSS that is used to repeat linear gradients. Syntax: background-image: repeating-linear-gradient( angle | to side-or-corner, color-stop1,color-stop2, ...); Parameters: This function accepts many parameters which are listed below: angle: This parameter is used to hold the angle of di
2 min read