CSS gradients are one of those features that most developers learn just enough of to get by — usually linear-gradient(to right, #color1, #color2) — and never go deeper. But the full gradient spec is surprisingly powerful, and understanding it opens up a lot of design possibilities without reaching for an image editor.
Here's a complete walkthrough of all three gradient types, with practical examples.
The three gradient types
CSS supports three types of gradients:
-
linear-gradient()— transitions along a straight line -
radial-gradient()— transitions outward from a central point -
conic-gradient()— transitions around a central point (like a pie chart)
All three also have repeating- variants: repeating-linear-gradient(), repeating-radial-gradient(), and repeating-conic-gradient().
linear-gradient()
The most common gradient type. Syntax:
background: linear-gradient(direction, color-stop-1, color-stop-2, ...);
Direction options:
/* Keywords */
background: linear-gradient(to right, #2f855a, #276749);
background: linear-gradient(to bottom right, #2f855a, #276749);
/* Angle (degrees) */
background: linear-gradient(90deg, #2f855a, #276749);
background: linear-gradient(135deg, #2f855a, #276749);
-
0deg= bottom to top -
90deg= left to right -
180deg= top to bottom -
270deg= right to left
Colour stops:
You can add as many colour stops as you want, and control their positions:
/* Three-colour gradient */
background: linear-gradient(to right, #ff0000, #ffff00, #00ff00);
/* Controlled positions */
background: linear-gradient(to right,
#2f855a 0%,
#2f855a 30%, /* solid green for first 30% */
#276749 70%,
#1a4a3b 100%
);
Transparency:
/* Fade to transparent */
background: linear-gradient(to right, rgba(0,0,0,0.8), transparent);
/* Image overlay — place over a background-image */
background: linear-gradient(to bottom, transparent 50%, rgba(0,0,0,0.7) 100%);
This last pattern is extremely common for hero images — it darkens the bottom half to make white text readable over a photo.
radial-gradient()
Radiates outward from a centre point. Useful for glows, spotlights, and circular effects.
background: radial-gradient(shape size at position, color-stop-1, color-stop-2);
Basic examples:
/* Circular gradient from centre */
background: radial-gradient(circle, #2f855a, #1a1a2e);
/* Ellipse (default shape) */
background: radial-gradient(ellipse, #2f855a, #1a1a2e);
/* Spotlight effect — off-centre */
background: radial-gradient(circle at 30% 40%, #ffffff 0%, #1a1a2e 80%);
Size keywords:
| Keyword | Behaviour |
|---|---|
closest-side |
Gradient ends at nearest side |
farthest-side |
Gradient ends at farthest side |
closest-corner |
Gradient ends at nearest corner |
farthest-corner |
Gradient ends at farthest corner (default) |
/* Subtle soft glow */
background: radial-gradient(circle closest-side at 50% 50%, rgba(47,133,90,0.3) 0%, transparent 100%);
conic-gradient()
Transitions colours around a point, like the hands of a clock sweeping around.
background: conic-gradient(from 0deg, red, yellow, green, blue, red);
Pie chart:
/* 40% red, 35% blue, 25% green */
background: conic-gradient(
red 0% 40%,
blue 40% 75%,
green 75% 100%
);
Colour wheel:
background: conic-gradient(
hsl(0deg, 100%, 50%),
hsl(60deg, 100%, 50%),
hsl(120deg, 100%, 50%),
hsl(180deg, 100%, 50%),
hsl(240deg, 100%, 50%),
hsl(300deg, 100%, 50%),
hsl(360deg, 100%, 50%)
);
border-radius: 50%;
Multiple gradients (layering)
You can layer multiple gradients on the same element by separating them with commas. They render front-to-back:
background:
linear-gradient(to bottom, transparent 60%, rgba(0,0,0,0.7) 100%),
linear-gradient(to right, rgba(47,133,90,0.3), transparent),
url('/hero.jpg') center/cover no-repeat;
This pattern — image with gradient overlays — is one of the most practical uses of CSS gradients.
Gradient on text
A popular visual effect: gradient-coloured text.
.gradient-text {
background: linear-gradient(135deg, #2f855a, #276749);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
Works in all modern browsers. The gradient is clipped to the text shape using background-clip: text.
repeating-linear-gradient()
Creates a striped or repeating pattern:
/* 10px diagonal stripes */
background: repeating-linear-gradient(
45deg,
#2f855a,
#2f855a 10px,
#276749 10px,
#276749 20px
);
When to use each type
| Type | Best for |
|---|---|
linear-gradient |
Button backgrounds, hero sections, overlays, dividers |
radial-gradient |
Glows, spotlights, circular backgrounds, vignettes |
conic-gradient |
Pie charts, colour wheels, progress rings |
repeating-* |
Stripes, patterns, loading indicators |
Building gradients without memorising syntax
If you'd rather experiment visually than type CSS from memory, the CSS Gradient Generator at SnappyTools lets you build linear, radial, and conic gradients with a live preview, copy the CSS output, and export as Tailwind CSS classes — all in the browser with no account required.
The syntax above is worth understanding even if you use a generator, because you'll need to modify the output or debug it eventually.
The one thing most developers miss
The at position parameter for radial and conic gradients is underused. It lets you place the gradient origin exactly where you want it — which is the difference between a generic glow and a realistic spotlight effect.
/* Spotlight from top-left */
background: radial-gradient(circle at 20% 15%, rgba(255,255,255,0.15) 0%, transparent 60%);
Pair this with a dark background colour, and you have a premium-looking card effect in about four lines of CSS.
Top comments (0)