02. CSS Gradients (Linear, Radial & Conic Gradients)
🟢 Beginner
1. Definition
CSS Gradients are image values generated by the browser rendering engine: linear-gradient() (transitions along a straight line), radial-gradient() (radiates from a central point), and conic-gradient() (rotates around a focal point).
2. Why It Is Used
Gradients replace heavy image assets with crisp, resolution-independent CSS backgrounds that scale smoothly to any screen resolution without extra HTTP requests.
3. How It Works
Gradients interpolate smoothly between two or more specified color stops across an angle (linear), circle/ellipse shape (radial), or rotational degrees (conic).
4. Syntax
.gradient-box {
background: linear-gradient(135deg, #1e3a8a, #3b82f6);
}
5. Gradient Function Types Reference
| Gradient Function | Direction / Center Syntax | Best Practice Usage |
|---|---|---|
linear-gradient() | 135deg, to right bottom | Hero banners & button backgrounds |
radial-gradient() | circle at center | Vignette effects & spotlight glows |
conic-gradient() | from 90deg at 50% 50% | Pie charts, color pickers, conic borders |
6. Beginner Example
.hero-banner {
background: linear-gradient(to right, #0f172a, #1e293b);
color: white;
}
7. Intermediate Example
.spotlight-card {
background: radial-gradient(circle at top right, #3b82f6, #0f172a 70%);
color: white;
}
8. Advanced Conic Chart & Border Example
/* Pie chart rendered entirely with CSS conic-gradient */
.pie-chart {
width: 150px;
height: 150px;
border-radius: 50%;
background: conic-gradient(#2563eb 0% 60%, #10b981 60% 85%, #f59e0b 85% 100%);
}
9. Line-by-Line Code Explanation
conic-gradient(#2563eb 0% 60%, ...): Creates hard color stop boundaries at 60% and 85% to render a crisp pie chart.
10. Expected Terminal / Browser Output
A circular pie chart element with blue (60%), green (25%), and yellow (15%) slices rendered purely using CSS.
11. Common Mistakes & Pitfalls
- Using background-color instead of background: Writing
background-color: linear-gradient(...)is invalid because gradients are background images.
12. Important Rules & Limitations
- CSS gradients cannot be animated directly with CSS transitions unless using
@propertyregistered variables.
13. Multilingual Explanation: English
CSS gradients generate crisp background images. Linear gradients follow a direction line, radial gradients emanate from a center, and conic gradients rotate around a point.
14. Multilingual Explanation: Hindi (Roman Script)
Gradients CSS backgrounds hote hain. Linear gradient ek line ke along, radial gradient center se bahar, aur conic gradient round angle par ghoomta hai.
15. Multilingual Explanation: Marathi (Roman Script)
Gradients mule smooth color transitions miltat. Linear, Radial ani Conic gradients cha waparn karun charts kiva background effects banvta yetat.
16. Practice Questions
- Why is
background-color: linear-gradient(...)invalid syntax? - How does a
conic-gradient()differ from aradial-gradient()?
17. Practical Coding Challenge
Write a CSS rule for a .conic-avatar with a rotating conic-gradient border frame.