Gradient Functions: linear-gradient() and radial-gradient()
You'll learn how to create smooth, CSS-based color transitions without needing image files.
What Are Gradient Functions?
Gradients are smooth color transitions that blend from one color to another. In CSS, linear-gradient() and radial-gradient() are functions that generate gradients programmatically, giving you precise control over direction, shape, and color stops. They're treated as images in CSS, so you use them with properties like background-image or background.
Think of gradients like a sunset: colors blend seamlessly from one to another. linear-gradient() creates a straight-line transition (like the horizon), while radial-gradient() radiates outward from a center point (like ripples in water).
Linear Gradients
A linear gradient transitions colors along a straight line:
.box {
background: linear-gradient(to right, blue, green);
}
- Direction:
to right,to bottom,45deg, etc. - Color stops: Define where each color begins (e.g.,
blue 0%, green 100%)
.header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
Radial Gradients
A radial gradient radiates from a center point outward:
.circle {
background: radial-gradient(circle, yellow, orange, red);
}
- Shape:
circleorellipse - Position:
at center,at top left, etc.
.spotlight {
background: radial-gradient(circle at 30% 40%, white, transparent);
}
Color Stops for Precision
Control exactly where colors transition:
.striped {
background: linear-gradient(90deg, red 33%, white 33%, white 66%, blue 66%);
}
Key Takeaway: Gradient functions create smooth, customizable color transitions directly in CSS, giving you control over direction, shape, and exactly where colors start and stop—all without external images.