Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
CSS3 Linear gradients
CSS3 linear gradients allow you to create smooth transitions between two or more colors in a linear direction. Instead of using solid background colors, you can create beautiful gradients that transition from one color to another along a straight line.
Syntax
selector {
background: linear-gradient(direction, color1, color2, ...);
}
Possible Values
| Parameter | Description |
|---|---|
direction |
Optional. Defines the direction (to top, to right, 45deg, etc.) |
color1, color2, ... |
Two or more colors for the gradient transition |
Example: Basic Linear Gradient
The following example creates a linear gradient from pink to green −
<!DOCTYPE html>
<html>
<head>
<style>
.gradient-box {
width: 300px;
height: 150px;
background: linear-gradient(pink, green);
border: 2px solid #ccc;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
text-shadow: 1px 1px 2px black;
}
</style>
</head>
<body>
<div class="gradient-box">Linear Gradient</div>
</body>
</html>
A rectangular box with a smooth gradient transition from pink at the top to green at the bottom, with white text "Linear Gradient" in the center.
Example: Directional Gradient
You can control the direction of the gradient using keywords or angles −
<!DOCTYPE html>
<html>
<head>
<style>
.gradient-container {
display: flex;
gap: 20px;
flex-wrap: wrap;
}
.gradient-item {
width: 150px;
height: 100px;
border: 2px solid #333;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
text-shadow: 1px 1px 2px black;
font-size: 12px;
}
.horizontal {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}
.diagonal {
background: linear-gradient(45deg, #667eea, #764ba2);
}
.angle {
background: linear-gradient(135deg, #f093fb, #f5576c);
}
</style>
</head>
<body>
<div class="gradient-container">
<div class="gradient-item horizontal">to right</div>
<div class="gradient-item diagonal">45deg</div>
<div class="gradient-item angle">135deg</div>
</div>
</body>
</html>
Three gradient boxes displayed side by side: first with horizontal left-to-right gradient, second with 45-degree diagonal gradient, and third with 135-degree diagonal gradient, each labeled with their respective direction.
Example: Multiple Color Gradient
Linear gradients can include more than two colors for complex transitions −
<!DOCTYPE html>
<html>
<head>
<style>
.rainbow-gradient {
width: 400px;
height: 100px;
background: linear-gradient(to right,
red, orange, yellow, green, blue, indigo, violet);
border-radius: 10px;
border: 3px solid #333;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
text-shadow: 2px 2px 4px black;
}
</style>
</head>
<body>
<div class="rainbow-gradient">Rainbow Gradient</div>
</body>
</html>
A wide rectangular box with rounded corners displaying a rainbow gradient from left to right, transitioning through red, orange, yellow, green, blue, indigo, and violet, with "Rainbow Gradient" text centered.
Conclusion
CSS3 linear gradients provide a powerful way to create smooth color transitions without images. You can control the direction, use multiple colors, and create stunning visual effects for modern web design.
Advertisements
