CSS Gradients
1. Introduction to CSS Gradients
A gradient is a smooth transition between two or more colors. CSS provides different types of gradients that can be used directly as background images.
Why Use CSS Gradients?
- Create attractive backgrounds.
- Reduce the need for image files.
- Improve website performance.
- Create modern UI designs.
- Easy to customize using CSS.
.box{
background:linear-gradient(red, blue);
}
2. Linear Gradient
A linear gradient creates a gradual color transition in a straight line.
.box{
background:linear-gradient(red, yellow);
}
.box{
background:linear-gradient(to right,
blue, cyan);
}
| Direction | Example |
|---|---|
| Top to Bottom | linear-gradient(red, blue) |
| Left to Right | linear-gradient(to right, red, blue) |
| Diagonal | linear-gradient(to bottom right, red, blue) |
3. Radial Gradient
A radial gradient starts from the center and spreads outward in a circular or elliptical shape.
.box{
background:radial-gradient(red,
yellow, green);
}
.circle{
background:radial-gradient(circle,
white, blue);
}
| Shape | Description |
|---|---|
| Circle | Creates a circular gradient. |
| Ellipse | Creates an elliptical gradient. |
4. Conic Gradient
A conic gradient creates color transitions around a center point in a circular pattern.
.box{
background:conic-gradient(red,
yellow, green, blue);
}
.chart{
background:conic-gradient(
red 25%,
yellow 25% 50%,
green 50% 75%,
blue 75%
);
}
| Use | Example |
|---|---|
| Pie Charts | ✔ Yes |
| Color Wheels | ✔ Yes |
| Decorative Backgrounds | ✔ Yes |
5. Gradient Direction & Angle
You can control the direction of a gradient using keywords
such as to right, to left, or by
specifying an angle in degrees.
.box{
background:linear-gradient(
to right,
red,
orange
);
}
.box{
background:linear-gradient(
45deg,
purple,
pink
);
}
| Direction | Example |
|---|---|
| to right | Left → Right |
| to left | Right → Left |
| to bottom | Top → Bottom |
| 45deg | Diagonal Gradient |
| 90deg | Horizontal Gradient |
6. Multiple Color Stops
CSS gradients allow you to use multiple color stops to create smooth and colorful transitions between several colors.
.box{
background:linear-gradient(
red,
yellow,
green,
blue
);
}
.box{
background:linear-gradient(
red 10%,
yellow 40%,
green 70%,
blue 100%
);
}
| Feature | Description |
|---|---|
| Multiple Colors | Uses two or more colors. |
| Color Stops | Controls where each color appears. |
| Smooth Transition | Creates attractive color blending. |
7. Repeating Gradients
CSS provides repeating gradients that automatically repeat the gradient pattern across the element.
.box{
background:repeating-linear-gradient(
red 0px,
yellow 20px,
blue 40px
);
}
.circle{
background:repeating-radial-gradient(
red,
yellow 15%,
green 30%
);
}
| Gradient | Purpose |
|---|---|
| repeating-linear-gradient() | Repeats linear gradients. |
| repeating-radial-gradient() | Repeats radial gradients. |
8. Practical Gradient Examples
Example 1 : Button Background
button{
background:linear-gradient(
#2196F3,
#0D47A1
);
}
Example 2 : Hero Section
.hero{
background:linear-gradient(
to right,
purple,
pink
);
}
Example 3 : Circular Background
.circle{
background:radial-gradient(
white,
blue
);
}
Example 4 : Pie Chart
.chart{
background:conic-gradient(
red,
yellow,
green,
blue
);
}
9. Advantages of CSS Gradients
- No image files are required.
- Improves website loading speed.
- Creates attractive backgrounds.
- Fully responsive on all screen sizes.
- Easy to customize using CSS.
- Supports multiple colors and directions.
- Works well with animations and transitions.
| Feature | Benefit |
|---|---|
| Performance | ✔ Better than images |
| Responsive | ✔ Yes |
| Customization | ✔ Excellent |
10. CSS Gradients Comparison Table
| Gradient Type | Direction | Common Use |
|---|---|---|
| Linear Gradient | Straight Line | Buttons, Headers |
| Radial Gradient | Center Outward | Spotlight Effects |
| Conic Gradient | Around Center | Pie Charts, Color Wheels |
| Repeating Gradient | Repeats Pattern | Background Patterns |
.box{
background:linear-gradient(
45deg,
#ff6b6b,
#4ecdc4,
#1a73e8
);
}
11. CSS Gradient Best Practices
Following best practices helps create attractive, readable, and professional gradient designs.
- Use gradients with complementary colors.
- Avoid using too many bright colors together.
- Maintain sufficient contrast for text readability.
- Use gradients to enhance the design, not distract users.
- Keep gradient directions consistent throughout the website.
- Test gradients on different screen sizes.
- Combine gradients with shadows and animations carefully.
- Use subtle gradients for professional websites.
12. Common CSS Gradient Mistakes
| Mistake | Correct Practice |
|---|---|
| Using too many colors. | Use 2–4 matching colors. |
| Poor text readability. | Ensure good color contrast. |
| Choosing random directions. | Use consistent directions. |
| Overusing repeating gradients. | Use repeating gradients only when needed. |
| Ignoring responsive testing. | Test gradients on different devices. |
❌ Poor Design
background:linear-gradient(
red,
yellow,
green,
blue,
purple,
orange
);
✔ Better Design
background:linear-gradient(
#4facfe,
#00f2fe
);
13. Browser Support
CSS gradients are supported by all major modern browsers.
| Browser | Support |
|---|---|
| Google Chrome | ✔ Supported |
| Mozilla Firefox | ✔ Supported |
| Microsoft Edge | ✔ Supported |
| Safari | ✔ Supported |
| Opera | ✔ Supported |
14. CSS Gradient Interview Questions
- What is a CSS gradient?
- What are the different types of CSS gradients?
- What is the syntax of
linear-gradient()? - How is a radial gradient different from a linear gradient?
- What is a conic gradient used for?
- What are color stops in gradients?
- What are repeating gradients?
- Can gradients replace background images?
- How do you specify the direction of a gradient?
- Are CSS gradients supported in modern browsers?
15. Lesson Summary
In this lesson, you learned:
- ✔ Introduction to CSS Gradients.
- ✔ Linear Gradients.
- ✔ Radial Gradients.
- ✔ Conic Gradients.
- ✔ Gradient Directions and Angles.
- ✔ Multiple Color Stops.
- ✔ Repeating Gradients.
- ✔ Practical Examples.
- ✔ Best Practices and Common Mistakes.
- ✔ Browser Support and Interview Questions.
Quick Quiz
Which CSS function creates a linear gradient?