Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
How to create gradient search button using HTML and CSS ?
Next article icon

How to create linear gradient background using CSS ?

Last Updated : 15 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
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:



Next Article
How to create gradient search button using HTML and CSS ?

V

varunvj181
Improve
Article Tags :
  • CSS
  • Web Technologies
  • CSS-Properties
  • CSS-Questions

Similar Reads

  • How to create linear gradient text using HTML and CSS ?
    The linear-gradient is used to create eye-catching text effects, particularly suitable for dark-themed websites or applications. This method involves filling text with linear-gradient color codes, making it look attractive and bold. Linear-gradient text effects are ideal for dark themes and provide
    3 min read
  • How to Create a Sliding Background Effect Using CSS ?
    A sliding background effect in CSS creates a smooth, continuous movement of a webpage’s background image, typically in a horizontal or vertical direction. It gives a dynamic, animated feel by using properties like background-position and animation, enhancing visual engagement without additional Java
    1 min read
  • How to create texture background using CSS ?
    Introduction: We can use CSS property to texture the background of the web page by using an image editor to cut out a portion of the background. Apply CSS background-repeat property to make the small image fill the area where it is used. CSS provides many styling properties of the background includi
    3 min read
  • How to Create a Gradient Shadow using CSS ?
    A gradient shadow in CSS refers to creating a shadow effect that transitions smoothly from one color to another, using gradients. While the box-shadow property doesn't support gradients directly, you can simulate this effect by layering a semi-transparent gradient as the background or using pseudo-e
    2 min read
  • How to create gradient search button using HTML and CSS ?
    In this article, we will see how to create a gradient search button using HTML & CSS, along with knowing its basic implementation through the examples. The creation of a gradient search button involves the CSS linear-gradient() function, which sets the background color of the button. Here, we ha
    4 min read
  • How to create a skewed background using CSS ?
    A skewed background design enhances a website's visual appeal with dynamic, angled elements. By using CSS transform: skew() along with ::before and ::after pseudo-elements, you can easily create a slanted effect, adding a modern and engaging look to your site's layout. ApproachHTML Structure - Secti
    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 `<svg>` Element: The `<svg>` element in HTML provide
    2 min read
  • How to Create Wave Background using CSS?
    A wave background is a design element having wavy patterns or shapes used to add movement in the web pages. The :before pseudo-element can be used to create a wavy background by applying it to an element and using CSS properties like clip-path to create wave shapes. Using :before pseudo-elementTo cr
    2 min read
  • Create a Galaxy Background using HTML/CSS
    In this article, we will see how to create the galaxy background using the HTML & CSS, along with knowing the basic CSS properties used & will understand it through the example. A galaxy background means a small bunch of circles having different sizes with different shades and a dark backgro
    3 min read
  • How to Animate Gradient Background Smoothly Using CSS ?
    Animating gradient backgrounds smoothly using CSS means setting a gradient background and using transitions or keyframe animations to create gradual changes in colors, angles, or positions. This technique adds a dynamic and visually attractive effect to enhance the webpage's design. What is Gradient
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences