CSS Gradients
Learn how to create beautiful gradient effects using CSS gradient functions
What are CSS Gradients?
CSS gradients let you display smooth transitions between two or more specified colors. Unlike images, gradients are generated by the browser and can be scaled indefinitely without losing quality.
Gradient Types:
- Linear Gradients - Colors transition along a straight line
- Radial Gradients - Colors transition outward from a center point
- Conic Gradients - Colors transition around a center point
- Repeating Gradients - Repeating patterns of gradients
CSS Gradient Properties Reference
Function | Description | Syntax |
---|---|---|
linear-gradient() | Creates a linear gradient along a straight line | linear-gradient([direction], color-stop1, color-stop2, ...) |
radial-gradient() | Creates a radial gradient radiating from a center point | radial-gradient([shape] [at position], color-stop1, color-stop2, ...) |
conic-gradient() | Creates a conic gradient rotated around a center point | conic-gradient([from angle] [at position], color-stop1, color-stop2, ...) |
repeating-linear-gradient() | Repeats a linear gradient | repeating-linear-gradient([direction], color-stop1, color-stop2, ...) |
repeating-radial-gradient() | Repeats a radial gradient | repeating-radial-gradient([shape] [at position], color-stop1, color-stop2, ...) |
repeating-conic-gradient() | Repeats a conic gradient | repeating-conic-gradient([from angle] [at position], color-stop1, color-stop2, ...) |
CSS Gradients in Action
Example Code
/* CSS Gradient Properties */ /* Linear Gradient */ .linear { background: linear-gradient(to right, red, blue); background: linear-gradient(45deg, red, blue); background: linear-gradient(to bottom right, red, blue); background: linear-gradient(180deg, red, blue); } /* Radial Gradient */ .radial { background: radial-gradient(circle, red, blue); background: radial-gradient(ellipse, red, blue); background: radial-gradient(circle at top left, red, blue); background: radial-gradient(farthest-corner, red, blue); } /* Conic Gradient */ .conic { background: conic-gradient(red, blue); background: conic-gradient(from 45deg, red, blue); background: conic-gradient(red 0deg 90deg, blue 90deg 180deg, green 180deg 270deg, yellow 270deg 360deg); background: conic-gradient(at center, red, blue); } /* Repeating Gradients */ .repeating { background: repeating-linear-gradient(45deg, red, red 10px, blue 10px, blue 20px); background: repeating-radial-gradient(circle, red, red 10px, blue 10px, blue 20px); background: repeating-conic-gradient(red 0deg 10deg, blue 10deg 20deg); } /* Multiple Color Stops */ .multiple-stops { background: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet); background: radial-gradient(circle, red 0%, orange 20%, yellow 40%, green 60%, blue 80%, indigo 100%); } /* Gradient with Transparency */ .transparency { background: linear-gradient(to right, rgba(255,0,0,0.5), rgba(0,0,255,0.5)); background: radial-gradient(circle, rgba(255,0,0,0.8), rgba(0,0,255,0.2)); } /* Hard Color Stops */ .hard-stops { background: linear-gradient(to right, red 0%, red 50%, blue 50%, blue 100%); background: conic-gradient(red 0deg 90deg, blue 90deg 180deg, green 180deg 270deg, yellow 270deg 360deg); } /* Using Gradients with Background Properties */ .combined { background: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)), url('image.jpg') center/cover; } /* Gradient Text */ .gradient-text { background: linear-gradient(to right, red, blue); -webkit-background-clip: text; background-clip: text; color: transparent; } /* Gradient Borders */ .gradient-border { border: 5px solid; border-image: linear-gradient(to right, red, blue) 1; } /* Gradient Shadows */ .gradient-shadow { box-shadow: 0 10px 20px rgba(255,0,0,0.5), 0 6px 6px rgba(0,0,255,0.5); }
Gradient Types in Detail
Linear Gradients
Linear gradients transition colors along a straight line. You can specify the direction using keywords, angles, or side corners.
Radial Gradients
Radial gradients transition colors outward from a center point. You can specify the shape, size, and position of the gradient.
Conic Gradients
Conic gradients transition colors rotated around a center point. You can specify the starting angle and position.
Repeating Gradients
Repeating gradients create patterns by repeating the specified gradient. They're useful for creating striped or patterned backgrounds.
Advanced Gradient Techniques
Gradient Text
You can apply gradients to text using background-clip property. Note that this requires the -webkit- prefix for WebKit browsers.
Gradient Borders
You can create gradient borders using the border-image property with a gradient.
Gradient Overlays
You can combine gradients with background images to create overlay effects.
Gradient Shadows
While not true gradients, you can simulate gradient effects with multiple box-shadows.
Browser Support
Gradient Type | Chrome | Firefox | Safari | Edge |
---|---|---|---|---|
Linear Gradients | 26.0+ | 16.0+ | 6.1+ | 12.0+ |
Radial Gradients | 26.0+ | 16.0+ | 6.1+ | 12.0+ |
Conic Gradients | 69.0+ | 83.0+ | 12.1+ | 79.0+ |
Repeating Linear | 26.0+ | 16.0+ | 6.1+ | 12.0+ |
Repeating Radial | 26.0+ | 16.0+ | 6.1+ | 12.0+ |
Repeating Conic | 69.0+ | 83.0+ | 12.1+ | 79.0+ |
Notes:
- Most modern browsers support all gradient types
- For older browsers, consider using fallback background colors or images
- Conic gradients have relatively newer support
- Check caniuse.com for detailed browser compatibility