For working professionals
For fresh graduates
More
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
Foreign Nationals
The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not .
1. Introduction
6. PyTorch
9. AI Tutorial
10. Airflow Tutorial
11. Android Studio
12. Android Tutorial
13. Animation CSS
16. Apex Tutorial
17. App Tutorial
18. Appium Tutorial
21. Armstrong Number
22. ASP Full Form
23. AutoCAD Tutorial
27. Belady's Anomaly
30. Bipartite Graph
35. Button CSS
39. Cobol Tutorial
46. CSS Border
47. CSS Colors
48. CSS Flexbox
49. CSS Float
51. CSS Full Form
52. CSS Gradient
53. CSS Margin
54. CSS nth Child
55. CSS Syntax
56. CSS Tables
57. CSS Tricks
58. CSS Variables
61. Dart Tutorial
63. DCL
65. DES Algorithm
83. Dot Net Tutorial
86. ES6 Tutorial
91. Flutter Basics
92. Flutter Tutorial
95. Golang Tutorial
96. Graphql Tutorial
100. Hive Tutorial
103. Install Bootstrap
107. Install SASS
109. IPv 4 address
110. JCL Programming
111. JQ Tutorial
112. JSON Tutorial
113. JSP Tutorial
114. Junit Tutorial
115. Kadanes Algorithm
116. Kafka Tutorial
117. Knapsack Problem
118. Kth Smallest Element
119. Laravel Tutorial
122. Linear Gradient CSS
129. Memory Hierarchy
133. Mockito tutorial
134. Modem vs Router
135. Mulesoft Tutorial
136. Network Devices
138. Next JS Tutorial
139. Nginx Tutorial
141. Octal to Decimal
142. OLAP Operations
143. Opacity CSS
144. OSI Model
145. CSS Overflow
146. Padding in CSS
148. Perl scripting
149. Phases of Compiler
150. Placeholder CSS
153. Powershell Tutorial
158. Pyspark Tutorial
161. Quality of Service
162. R Language Tutorial
164. RabbitMQ Tutorial
165. Redis Tutorial
166. Redux in React
167. Regex Tutorial
170. Routing Protocols
171. Ruby On Rails
172. Ruby tutorial
173. Scala Tutorial
175. Shadow CSS
178. Snowflake Tutorial
179. Socket Programming
180. Solidity Tutorial
181. SonarQube in Java
182. Spark Tutorial
189. TCP 3 Way Handshake
190. TensorFlow Tutorial
191. Threaded Binary Tree
196. Types of Queue
197. TypeScript Tutorial
198. UDP Protocol
202. Verilog Tutorial
204. Void Pointer
205. Vue JS Tutorial
206. Weak Entity Set
207. What is Bandwidth?
208. What is Big Data
209. Checksum
211. What is Ethernet
214. What is ROM?
216. WPF Tutorial
217. Wireshark Tutorial
218. XML Tutorial
Want to add depth and vibrant color to your web pages? Go beyond flat, single-color backgrounds with CSS Gradients.
This powerful CSS feature allows you to create beautiful, smooth transitions between two or more colors directly in your code, no images required. In this tutorial, we’ll explore the different types of CSS Gradients, from straight lines (linear) to circles (radial), and show you how to master them with practical, hands-on examples.
Ready to move beyond the fundamentals and build stunning, modern websites? Explore upGrad’s Software Engineering Courses to master advanced CSS, JavaScript, and the complete front-end development ecosystem with hands-on projects.
CSS gradients enable you to create smooth color transitions between two or more colors rather than using a single solid color. Gradients are excellent for giving your website design visual depth and richness since they create dynamic backgrounds, buttons, or other elements.
Backgrounds, buttons, and borders are just a few of the items on your website that can be decorated with CSS gradients. Gradients can be customized with various color stops and placements and offer visual attractiveness. They provide a strong technique to improve your site design and produce interesting user experiences and are extensively supported by contemporary browsers.
Instead of utilizing a single solid color, CSS gradients let you make seamless color transitions between two or more colors. As they produce dynamic backdrops, buttons, or other items, gradients are great for adding visual depth and richness to your website design.
Ready to move beyond development and unlock new career opportunities? We've curated these forward-looking courses specifically to enhance your professional growth:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#gradient {
background-image: linear-gradient(red, green, blue, white);
height: 200px;
width: 320px;
color: white;
}
</style>
</head>
<body>
<div id="gradient">Linear gradient for 4 colors from top to bottom</div>
</body>
</html>
Web designers frequently choose CSS gradients because of their many advantages and practical applications. The following are some justifications for employing CSS gradients:
Also Read: Top 70+ CSS Interview Questions & Answers for All Experience Levels for 2025
In this portion of the tutorial, we will discuss the different types of gradients in CSS through examples.
The most typical kind of gradient is a linear gradient. The gradient axis can be adjusted to run vertically, horizontally, diagonally, or at any other angle.
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#gradient {
background-image: linear-gradient(green, lightgreen, white);
height: 200px;
width: 320px;
color: #b2ffff;
}
</style>
</head>
<body>
<div id="gradient">Linear Gradient</div>
</body>
</html>
Straight-line color transitions are produced by linear gradients. Use the terms "to top," "to bottom," "to right," "to left," "to top right," etc. to describe the gradient's direction or enter an angle (in degrees) instead.
Also Read: CSS vs CSS3: Understand the Difference Between CSS and CSS3
An ellipse or circular-shaped radial gradient is a sort of CSS gradient that produces a seamless color transition from a center point outward. The defined shape's edges are covered with colors that radiate outward from the center. The creation of round or rounded components with multiple color variations makes use of radial gradients particularly well.
Syntax: background-image: radial-gradient(shape size at position, start-color, transitioning colors, ..., last-color);
Example:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
#radialgradient {
background-image: -webkit-radial-gradient(white,blue); /*For Safari & Chrome 10+*/
background-image: -o-radial-gradient(white, yellow); /*For Opera */
background-image: -moz-radial-gradient(white,blue); /*For Firefox */
background-image: radial-gradient( white, yellow); /*Standard Syntax*/
height: 400px;
width: 450px;
text-align: center;
}
</style>
</head>
<body>
<div id="radialgradient"><h2>Radial Gradient</h2></div>
</body>
</html>
Here, we will discuss different types of linear gradients with the help of examples.
You can use the linear-gradient() function keyword to produce a linear gradient that flows smoothly from the top to the bottom of an element. This will guarantee that the gradient runs vertically through the element, from top to bottom.
Syntax: background-image: linear-gradient(red, white);
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#linear-top-bottom-grad {
height: 400px;
width: 550px;
background: -webkit-linear-gradient(blue, lightred); /* For Safari*/
background: -o-linear-gradient(blue, lightred); /* For Opera 11.1*/
background: -moz-linear-gradient(blue, lightred); /* For Firefox */
background: linear-gradient(blue, lightgreen); /* Standard syntax */
}
</style>
</head>
<body>
<h3>Linear Gradient - Top to Bottom</h3>
<p>This linear gradient starts at the top. It starts as blue, then transitions to light green:</p>
<div id="linear-top-bottom-grad"></div>
</body>
</html>
Also Read: 15 Amazing CSS Project Ideas Every Beginner Must Try in 2025!
You can use the linear-gradient(to right,.....) function keyword to produce a linear gradient that flows smoothly from the left to right of an element. This will guarantee that the gradient runs horizontally through the element, from left to right.
Syntax: background-image: linear-gradient(to right,red, white);
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#linear-left-right-grad {
height: 400px;
width: 550px;
background: -webkit-linear-gradient(left, lightgreen, green); /* For Safari*/
background: -o-linear-gradient(right, lightgreen, green); /* For Opera*/
background: -moz-linear-gradient(right, lightgreen, green); /* For Firefox*/
background: linear-gradient(to right, lightgreen , green); /* General syntax*/
}
</style>
</head>
<body>
<h3>Linear Gradient - Left to Right</h3>
<p>This linear gradient starts at the left. It starts as lightgreen, and then transitions to green:</p>
<div id="linear-left-right-grad"></div>
</body>
</html>
You can use the linear-gradient(to bottom right,.....) function keyword to produce a linear gradient that flows smoothly from the top left to the bottom right of an element. This will guarantee that the gradient runs diagonally through the element, from left to right.
Syntax: background-image: linear-gradient(to bottom right,red, white);
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#linear-diagonal-grad {
height: 400px;
width: 550px;
background: -webkit-linear-gradient(left top, red , purple); /* For Safari */
background: -o-linear-gradient(bottom right, red, purple); /* For Opera */
background: -moz-linear-gradient(bottom right, red, purple); /* For Firefox */
background: linear-gradient(to bottom right, red , purple); /* General syntax */
}
</style>
</head>
<body>
<h3>Linear Gradient - Diagonal</h3>
<p>This linear gradient starts at top left. It starts out as red, then transitions to purple:</p>
<div id="linear-diagonal-grad"></div>
</body>
</html>
Here, we will discuss the different types of radial CSS gradients with the help of examples.
Radial gradients with evenly spaced color stops are the default CSS radial gradient. Using the necessary parameters, the radial-gradient() method can be used to produce a radial gradient in CSS. A circular or elliptical-shaped radial gradient produces a seamless color shift from a center point outward.
Syntax: background: radial-gradient(color1,.....);
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#radial-grad {
height: 400px;
width: 550px;
background: -webkit-radial-gradient(blue, lightblue, violet); /* Safari */
background: -o-radial-gradient(blue, lightblue, violet); /* For Opera */
background: -moz-radial-gradient(blue, lightblue, violet); /* For Firefox */
background: radial-gradient(blue, lightblue, violet); /* General syntax */
}
</style>
</head>
<body>
<h3>Default Radial Gradient - Evenly Spaced Color Stops with blue, lightblue ,and violet</h3>
<div id="radial-grad"></div>
</body>
</html>
Also Read: What is Responsive Web Design
Using the necessary parameters, the radial-gradient(color1%,color2%,..) method can be used to produce a radial gradient with differently spaced color stops in CSS. A circular or elliptical-shaped radial gradient produces a seamless color shift from a center point outward.
Syntax: background: radial-gradient(blue 5%, violet 15%, red 60%);
Example:
<!DOCTYPE html>
<html>
<head>
<style>
#differently-spaced-radial-grad {
height: 400px;
width: 550px;
background: -webkit-radial-gradient(blue 5%, violet 15%, red 60%); /* For Safari */
background: -o-radial-gradient(blue 5%, violet 15%, red 60%); /* For Opera */
background: -moz-radial-gradient(blue 5%, violet 15%, red 60%); /* For Firefox */
background: radial-gradient(blue 5%, violet 15%, red 60%); /* General syntax */
}
</style>
</head>
<body>
<h3>Default Radial Gradient - Differently Spaced Color Stops with blue, violet ,and red</h3>
<div id="differently-spaced-radial-grad"></div>
</body>
</html>
CSS gradients are widely supported in contemporary web browsers as of my most recent update in September 2021. CSS gradients are completely supported by all popular browsers, including Google Chrome, Mozilla Firefox, Apple Safari, Microsoft Edge, and Opera. The support features radial and linear gradients.
The supported browser versions are listed below:
Remember that CSS gradients are frequently used, and if you need to support legacy systems, it's a good idea to verify compatibility with earlier browser versions. However, you don't need to worry too much about browser support when using CSS gradients in the majority of web development cases.
Done with CSS? Check out this free course on JavaScript Basics from Scratch
In conclusion, CSS Gradients are a powerful and efficient tool for adding vibrant, dynamic color to your web designs without needing to slow down your site with image files.
You now have the skills to create beautiful linear-gradient and radial-gradient effects that can make your websites more modern and visually engaging. The best way to master CSS Gradients is to experiment, so get creative and start adding some colorful transitions to your projects!
CSS gradients are a feature of Cascading Style Sheets (CSS) that allow you to create smooth transitions between two or more specified colors. They are a type of <image> data type, meaning they can be used anywhere an image can be, such as in the background-image property. Web designers use CSS gradients to create visually appealing backgrounds, buttons, and other UI elements without needing to create and load actual image files.
The main advantage of using CSS gradients over image files is performance and scalability. Because CSS gradients are generated by the browser, they don't require an additional HTTP request to download an image file, which can speed up page load times. They also scale perfectly to any size without losing quality, unlike raster images which can become pixelated. Furthermore, CSS gradients are easier to modify and maintain directly in the code.
There are three primary functions for creating CSS gradients, each producing a different type of color transition:
Understanding these three types of CSS gradients is fundamental to using them effectively.
The basic syntax for creating linear CSS gradients involves specifying a direction and at least two colors. The function is typically used with the background-image property. If no direction is specified, the default is top to bottom. Learning the syntax is the first step to mastering CSS gradients.
CSS
/* A simple top-to-bottom linear gradient */
.element {
background-image: linear-gradient(blue, pink);
}
/* A linear gradient from left to right */
.element {
background-image: linear-gradient(to right, blue, pink);
}
You can change the direction of linear CSS gradients by using angles (like 45deg) or keywords. Keywords like to top, to bottom, to left, and to right (and their combinations, like to top right) are the most common. Using an angle gives you precise control over the gradient line. Mastering direction is key to creating custom CSS gradients.
CSS
/* Example using an angle */
.element {
background-image: linear-gradient(45deg, blue, pink);
}
/* Example using a keyword */
.element {
background-image: linear-gradient(to top right, blue, pink);
}
You can add as many colors as you like to linear CSS gradients by simply listing them in the function, separated by commas. The browser will automatically create an even transition between all the colors listed. This allows for the creation of rich, multi-colored CSS gradients like rainbows or sunset effects.
CSS
/* A linear gradient with three colors */
.element {
background-image: linear-gradient(to right, red, yellow, green);
}
Color stops are a crucial feature of CSS gradients that give you explicit control over where each color transition occurs. You can specify a position (using a percentage or a length value) for each color. This allows you to create gradients where the transitions are not evenly spaced. By understanding color stops, you can create much more complex and customized CSS gradients.
CSS
/* Blue takes up the first 10%, then transitions to pink at 90% */
.element {
background-image: linear-gradient(to right, blue 10%, pink 90%);
}
You can create hard-line stripes using linear CSS gradients by making two color stops have the same position. When the color transition has zero space to occur, it creates a sharp line instead of a smooth blend. This is a clever trick that allows you to create complex background patterns using only CSS gradients, without needing any images.
CSS
/* Creates a hard stripe pattern */
.element {
background-image: linear-gradient(to right,
blue 0%, blue 50%,
pink 50%, pink 100%
);
}
A radial CSS gradient differs from a linear one in its direction of color transition. Instead of progressing along a straight line, a radial gradient radiates outwards from a single point (the "gradient center"). By default, the shape of radial CSS gradients is an ellipse, but you can specify a circle. This type of gradient is perfect for creating effects like spotlights, sunbursts, or spherical looks.
You can explicitly control the shape of radial CSS gradients by including the shape keyword (circle or ellipse) before the color stops. If you don't specify a shape, it will default to an ellipse if the element is not a perfect square. Specifying the shape is a common way to customize radial CSS gradients.
CSS
/* A circular radial gradient */
.element {
background-image: radial-gradient(circle, blue, pink);
}
You can control the center position of radial CSS gradients using the at keyword followed by position values (e.g., at center, at top left, at 50% 50%). This allows you to place the origin of the gradient anywhere within the element's background. Manipulating the position is key to creating unique effects with radial CSS gradients.
CSS
/* A circular radial gradient positioned at the top left corner */
.element {
background-image: radial-gradient(circle at top left, blue, pink);
}
These size keywords give you precise control over the ending shape size of radial CSS gradients.
These keywords are essential for creating responsive radial CSS gradients that adapt to the element's size.
A conic CSS gradient is a type of gradient where the color transitions are rotated around a center point, rather than radiating from it. Imagine a color wheel or a pie chart—that's the effect a conic gradient creates. This type of CSS gradient is perfect for creating pie charts, color wheels, and circular progress indicators directly in CSS without needing complex JavaScript or images.
Yes, you can create repeating patterns using CSS gradients. The repeating-linear-gradient() and repeating-radial-gradient() functions are used for this purpose. You define a small gradient with color stops, and the browser will repeat that pattern indefinitely to fill the background. You can easily duplicate a gradient design using these repeating CSS gradients.
Yes, you can apply CSS gradients to text to create colorful, eye-catching headlines. This is an advanced technique that involves three steps:
CSS gradients themselves cannot be directly animated with CSS transitions or animations. However, you can create the illusion of an animated gradient by animating properties that affect it, such as the background-position or background-size. By creating a very large gradient and then animating its position, you can create smooth, dynamic, and beautiful animations using only CSS gradients.
To ensure browser compatibility for CSS gradients, it's important to use vendor prefixes for older browsers. While modern browsers have excellent support, older versions might require prefixes like -webkit- (for Chrome/Safari), -moz- (for Firefox), and -o- (for Opera). When working with CSS gradients, tools like Autoprefixer can automatically add these prefixes for you, ensuring your styles work across the widest range of browsers.
While CSS gradients are a web technology, similar concepts are used in mobile app development. For example, in React Native, you can use third-party libraries like react-native-linear-gradient to create a component that applies a native gradient effect. This allows you to use the power of linear CSS gradients to create visually appealing backgrounds and UI elements in your mobile apps, providing a consistent look and feel with web platforms.
When working with CSS gradients, a common mistake is forgetting that they are a background-image, not a background-color, so they won't override a color property. Another frequent error is using incorrect syntax, such as forgetting commas between colors. For accessibility, it's crucial to ensure there is enough contrast when placing text on top of CSS gradients.
Mastering CSS gradients is an important skill for any modern web or frontend developer. It demonstrates a strong grasp of advanced CSS and an eye for design. The ability to create complex, beautiful, and performant visuals without relying on image files is highly valued. As you advance in your web development journey, skills like creating CSS gradients become essential, which is why they are covered in comprehensive front-end development programs like those offered at upGrad.
FREE COURSES
Start Learning For Free
Author|900 articles published
Recommended Programs