CSS Gradients Guide: Master Linear, Radial, and Conic Gradients
CSS gradients are one of the most powerful tools in modern web design, allowing you to create stunning visual effects without the need for images. This comprehensive guide will teach you everything you need to know about CSS gradients, from basic linear gradients to advanced conic effects.
What Are CSS Gradients?
CSS gradients are smooth transitions between two or more colors. Unlike solid colors or images, gradients are generated by the browser and can be infinitely scaled without loss of quality. They’re defined using CSS functions and can be used anywhere an image would be used.
Benefits of CSS Gradients:
- Performance: No HTTP requests needed
- Scalable: Perfect quality at any resolution
- Dynamic: Can be modified with CSS variables
- Lightweight: Smaller file sizes than images
- Accessible: Better for responsive design
Types of CSS Gradients
CSS supports three main types of gradients:
- Linear Gradients: Colors transition along a straight line
- Radial Gradients: Colors radiate from a center point
- Conic Gradients: Colors transition around a center point
Linear Gradients
Linear gradients are the most common type, creating a smooth transition along a straight line.
Basic Syntax
background: linear-gradient(direction, color1, color2, ...);
Direction Options
Using Angles:
/* 0deg = to top, 90deg = to right, 180deg = to bottom, 270deg = to left */
background: linear-gradient(90deg, #FF6B9D, #6B9DFF);
background: linear-gradient(45deg, red, blue);
background: linear-gradient(135deg, #f093fb, #f5576c);
Using Keywords:
background: linear-gradient(to right, #FF6B9D, #6B9DFF);
background: linear-gradient(to bottom right, red, blue);
background: linear-gradient(to top, orange, purple);
Multiple Color Stops
You can add as many colors as you want:
/* Rainbow gradient */
background: linear-gradient(
90deg,
red 0%,
orange 16.6%,
yellow 33.3%,
green 50%,
blue 66.6%,
indigo 83.3%,
violet 100%
);
/* Sunset gradient */
background: linear-gradient(
180deg,
#FF512F 0%,
#F09819 50%,
#DD2476 100%
);
Color Stop Positions
Control exactly where each color starts and ends:
/* Sharp transitions */
background: linear-gradient(90deg, red 50%, blue 50%);
/* Soft blending */
background: linear-gradient(90deg, red 0%, blue 100%);
/* Custom positions */
background: linear-gradient(
to right,
red 20%,
yellow 40%,
green 60%,
blue 80%
);
Practical Examples
Button with Gradient:
.button {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 12px 24px;
border: none;
border-radius: 8px;
cursor: pointer;
transition: transform 0.2s;
}
.button:hover {
transform: translateY(-2px);
}
Gradient Text:
.gradient-text {
background: linear-gradient(90deg, #FF6B9D, #C06FFF, #6B9DFF);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
font-size: 48px;
font-weight: bold;
}
Card with Gradient Border:
.card {
position: relative;
background: white;
border-radius: 12px;
padding: 20px;
}
.card::before {
content: '';
position: absolute;
inset: 0;
border-radius: 12px;
padding: 2px;
background: linear-gradient(45deg, #f093fb, #f5576c);
-webkit-mask: linear-gradient(#fff 0 0) content-box,
linear-gradient(#fff 0 0);
mask-composite: exclude;
}
Radial Gradients
Radial gradients radiate outward from a center point, creating circular or elliptical patterns.
Radial Gradient Syntax
background: radial-gradient(shape size at position, color1, color2, ...);
Shape Options
/* Circle */
background: radial-gradient(circle, #FF6B9D, #6B9DFF);
/* Ellipse (default) */
background: radial-gradient(ellipse, red, blue);
/* Custom size */
background: radial-gradient(circle 100px, yellow, orange);
Size Keywords
/* closest-side: gradient ends at closest edge */
background: radial-gradient(closest-side, red, blue);
/* farthest-side: gradient ends at farthest edge */
background: radial-gradient(farthest-side, red, blue);
/* closest-corner: gradient ends at closest corner */
background: radial-gradient(closest-corner, red, blue);
/* farthest-corner: gradient ends at farthest corner (default) */
background: radial-gradient(farthest-corner, red, blue);
Positioning
/* Center (default) */
background: radial-gradient(circle at center, red, blue);
/* Top left */
background: radial-gradient(circle at top left, yellow, orange);
/* Custom position */
background: radial-gradient(circle at 30% 40%, #f093fb, #f5576c);
Radial Gradient Examples
Glowing Button:
.glow-button {
background: radial-gradient(
circle at center,
#ffffff 0%,
#667eea 50%,
#764ba2 100%
);
box-shadow: 0 0 20px rgba(102, 126, 234, 0.6);
border: none;
color: white;
padding: 15px 30px;
border-radius: 50px;
}
Spotlight Effect:
.spotlight {
background: radial-gradient(
circle at var(--x, 50%) var(--y, 50%),
rgba(255, 255, 255, 0.3) 0%,
transparent 50%
);
pointer-events: none;
}
Loading Spinner:
.spinner {
width: 60px;
height: 60px;
border-radius: 50%;
background: radial-gradient(
farthest-side,
#667eea 94%,
transparent
);
animation: spin 1s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
Conic Gradients
Conic gradients rotate colors around a center point, perfect for pie charts, color wheels, and unique effects.
Conic Gradient Syntax
background: conic-gradient(from angle at position, color1, color2, ...);
Examples
Color Wheel:
background: conic-gradient(
red 0deg,
yellow 60deg,
green 120deg,
cyan 180deg,
blue 240deg,
magenta 300deg,
red 360deg
);
border-radius: 50%;
Pie Chart:
/* 40% blue, 30% red, 30% green */
background: conic-gradient(
#3b82f6 0deg 144deg,
#ef4444 144deg 252deg,
#10b981 252deg 360deg
);
border-radius: 50%;
Loading Indicator:
.conic-loader {
width: 100px;
height: 100px;
border-radius: 50%;
background: conic-gradient(
from 0deg,
transparent 0deg,
#667eea 360deg
);
animation: rotate 1s linear infinite;
}
@keyframes rotate {
to { transform: rotate(360deg); }
}
Repeating Gradients
CSS also supports repeating versions of all gradient types, useful for patterns and backgrounds.
Repeating Linear Gradient
/* Striped background */
background: repeating-linear-gradient(
45deg,
#667eea,
#667eea 10px,
#764ba2 10px,
#764ba2 20px
);
/* Diagonal stripes */
background: repeating-linear-gradient(
-45deg,
transparent,
transparent 10px,
rgba(255, 107, 157, 0.2) 10px,
rgba(255, 107, 157, 0.2) 20px
);
Repeating Radial Gradient
/* Concentric circles */
background: repeating-radial-gradient(
circle,
#667eea,
#667eea 10px,
#764ba2 10px,
#764ba2 20px
);
Repeating Conic Gradient
/* Sunburst pattern */
background: repeating-conic-gradient(
from 0deg,
#667eea 0deg 15deg,
#764ba2 15deg 30deg
);
border-radius: 50%;
Multiple Gradients
You can layer multiple gradients using comma separation:
background:
linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),
linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),
linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%);
Browser Compatibility
CSS gradients are supported in all modern browsers:
- Chrome: 26+ (with prefix: -webkit-)
- Firefox: 16+ (with prefix: -moz-)
- Safari: 7+ (with prefix: -webkit-)
- Edge: All versions
- Opera: 12.1+
For best compatibility:
background: -webkit-linear-gradient(90deg, #FF6B9D, #6B9DFF); /* Chrome 10-25, Safari 5.1-6 */
background: -moz-linear-gradient(90deg, #FF6B9D, #6B9DFF); /* Firefox 3.6-15 */
background: linear-gradient(90deg, #FF6B9D, #6B9DFF); /* Standard syntax */
Performance Best Practices
1. Use CSS Variables for Dynamic Gradients:
:root {
--gradient-start: #FF6B9D;
--gradient-end: #6B9DFF;
}
.element {
background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
}
2. Prefer Gradients Over Images:
Gradients are more performant than background images for simple color transitions.
3. Avoid Too Many Color Stops:
While you can add unlimited stops, 3-5 is optimal for performance.
4. Use Hardware Acceleration:
.gradient-element {
transform: translateZ(0);
will-change: background;
}
Accessibility Considerations
Ensure Sufficient Contrast:
/* Good: High contrast between text and gradient */
.good-contrast {
background: linear-gradient(90deg, #000000, #333333);
color: white;
}
/* Bad: Low contrast */
.bad-contrast {
background: linear-gradient(90deg, #f0f0f0, #ffffff);
color: white; /* Hard to read! */
}
Provide Fallback Colors:
.gradient-bg {
background: #667eea; /* Fallback */
background: linear-gradient(135deg, #667eea, #764ba2);
}
Common Use Cases
Hero Sections
.hero {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
Navigation Bars
.navbar {
background: linear-gradient(to right, #f093fb 0%, #f5576c 100%);
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
Cards and Containers
.card {
background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
border-radius: 12px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
Loading States
.skeleton {
background: linear-gradient(
90deg,
#f0f0f0 0%,
#e0e0e0 50%,
#f0f0f0 100%
);
background-size: 200% 100%;
animation: loading 1.5s ease-in-out infinite;
}
@keyframes loading {
0% { background-position: 200% 0; }
100% { background-position: -200% 0; }
}
Tools and Resources
Gradient Generators
- datafmt.com Gradient Generator - Create CSS gradients with live preview
- CSS Gradient - Browser-based gradient tool
- Gradient Hunt - Curated gradient collection
Color Inspiration
- Coolors - Color scheme generator
- Adobe Color - Professional color tools
- UIGradients - Beautiful gradient library
Conclusion
CSS gradients are a powerful feature that every web developer should master. They offer endless creative possibilities while being performant and scalable. Whether you’re creating subtle backgrounds, eye-catching buttons, or complex patterns, gradients are an essential tool in modern web design.
Key takeaways:
- Use linear gradients for directional color transitions
- Apply radial gradients for circular effects and spotlights
- Leverage conic gradients for pie charts and color wheels
- Combine multiple gradients for complex effects
- Always test contrast for accessibility
- Provide fallback colors for older browsers
Ready to create your own gradients? Try our CSS Gradient Generator with live preview, multiple gradient types, and instant CSS/SVG/PNG export!
Tags: #CSS #Gradients #WebDesign #Frontend #Styling #WebDevelopment
Found this helpful? Try our free tools!
Explore Our Tools →