Introduction
CSS linear gradients allow you to create smooth transitions between two or more colors. They’re essential for modern web design, from subtle backgrounds to eye-catching hero sections.
Basic Syntax
Simple Two-Color Gradient
.gradient {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}
Direction: to right (can be to left, to top, to bottom, or angles)
With Angle
.gradient {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
Angles:
0deg= to top90deg= to right180deg= to bottom270deg= to left45deg= diagonal top-right
Color Stops
Multiple Colors
.rainbow {
background: linear-gradient(
to right,
red,
orange,
yellow,
green,
blue,
indigo,
violet
);
}
Positioned Color Stops
Control where colors transition:
.gradient {
background: linear-gradient(to right, #ff6b6b 0%, #4ecdc4 50%, #45b7d1 100%);
}
Even distribution:
.gradient {
background: linear-gradient(
to right,
red 0%,
red 33%,
blue 33%,
blue 66%,
green 66%,
green 100%
);
}
Hard Stops
Create sharp transitions:
.stripes {
background: linear-gradient(to right, red 0%, red 50%, blue 50%, blue 100%);
}
Common Patterns
Vertical Gradient
.hero {
background: linear-gradient(to bottom, #667eea 0%, #764ba2 100%);
}
Horizontal Gradient
.header {
background: linear-gradient(to right, #f093fb 0%, #f5576c 100%);
}
Diagonal Gradient
.banner {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
Radial-like with Linear
.sunset {
background: linear-gradient(to bottom, #ffecd2 0%, #fcb69f 100%);
}
Advanced Techniques
Repeating Gradients
.stripes {
background: repeating-linear-gradient(
45deg,
#ff6b6b,
#ff6b6b 10px,
#4ecdc4 10px,
#4ecdc4 20px
);
}
Multiple Gradients
Layer gradients for complex effects:
.complex {
background: linear-gradient(
to right,
rgba(255, 0, 0, 0.3),
rgba(0, 0, 255, 0.3)
), linear-gradient(to bottom, rgba(0, 255, 0, 0.3), rgba(255, 255, 0, 0.3));
}
Gradient Over Images
.hero-image {
background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
url("hero.jpg");
}
Text Gradient
.gradient-text {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
Color Theory for Gradients
Analogous Colors
Colors next to each other on color wheel:
.analogous {
background: linear-gradient(
to right,
#ff6b6b,
/* Red */ #ff8e53,
/* Orange-red */ #ffa726 /* Orange */
);
}
Complementary Colors
Opposite colors for high contrast:
.complementary {
background: linear-gradient(to right, #ff6b6b, /* Red */ #4ecdc4 /* Teal */);
}
Triadic Colors
Three evenly spaced colors:
.triadic {
background: linear-gradient(
to right,
#ff6b6b,
/* Red */ #4ecdc4,
/* Teal */ #ffe66d /* Yellow */
);
}
Real-World Examples
Hero Section
.hero {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
Card Hover Effect
.card {
background: linear-gradient(to bottom, #ffffff, #f5f5f5);
transition: background 0.3s;
}
.card:hover {
background: linear-gradient(to bottom, #667eea, #764ba2);
color: white;
}
Button Gradient
.btn-gradient {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: none;
color: white;
padding: 12px 24px;
border-radius: 8px;
}
.btn-gradient:hover {
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}
Loading Bar
.loading-bar {
background: linear-gradient(to right, #4facfe 0%, #00f2fe 100%);
height: 4px;
animation: loading 2s infinite;
}
@keyframes loading {
0% {
width: 0%;
}
100% {
width: 100%;
}
}
Best Practices
✅ Do This
-
Use semantic color names
/* Good */ background: linear-gradient(to right, var(--primary), var(--secondary)); -
Keep gradients subtle
/* Good - subtle */ background: linear-gradient(to bottom, #ffffff, #f8f9fa); -
Consider performance
- Gradients are GPU-accelerated
- Better than images for simple effects
- Use for backgrounds, not large areas
-
Test contrast
- Ensure text is readable
- Use gradient overlays for text contrast
❌ Don’t Do This
-
Don’t overuse
- Too many gradients = busy design
- Use sparingly for impact
-
Don’t ignore accessibility
- Check color contrast
- Provide fallback colors
-
Don’t use for critical UI
- Buttons, links should have solid colors
- Gradients can make states unclear
Browser Support
Linear gradients are well-supported:
- ✅ Chrome 10+
- ✅ Firefox 3.6+
- ✅ Safari 5.1+
- ✅ Edge 12+
- ✅ All modern browsers
Fallbacks
.button {
/* Fallback */
background: #667eea;
/* Gradient */
background: linear-gradient(135deg, #667eea, #764ba2);
}
Tools and Resources
Use our tools:
- Linear Gradient Generator - Create gradients visually
- Color Converter - Convert between color formats
Gradient inspiration:
- uiGradients.com
- WebGradients.com
- Coolors.co
Conclusion
CSS linear gradients are powerful tools for modern web design:
When to use:
- ✅ Hero sections
- ✅ Backgrounds
- ✅ Hover effects
- ✅ Subtle depth
Keep in mind:
- Performance is excellent
- Use subtle gradients for professionalism
- Test contrast for accessibility
- Combine with other CSS features
Next Steps
- Try Linear Gradient Generator
- Learn Color Theory
- Explore CSS Grid Layout