CSS Gradients
Gradients are the superpower of CSS backgrounds — smooth colour transitions rendered entirely in the browser with no images required. From simple two-colour fades to complex conic pie charts and infinite tiling patterns, CSS gradients are endlessly versatile.
linear-gradient — directions
and colour stopsradial-gradient — circles,
ellipses and positionsconic-gradient — pie charts and
colour wheelsWhat is a CSS Gradient?
A CSS gradient is a generated image — it behaves exactly like a background-image but is created by the browser from a mathematical formula
instead of loading a file. There are four gradient functions in CSS:
background-repeat.background-image,
not background-color. You can layer multiple gradients with commas, and
they respond to all background-size, background-position and background-repeat properties.
linear-gradient()
A linear-gradient transitions between colours
along a straight line. The first argument is the direction, followed by two or more
colour stops.
Directions
Colour Stops
Colour stops control where each colour starts and ends. You can use %, px or leave positions out for automatic distribution.
Hard stop — same position twice creates a sharp edge:
Colour hint — a single % between two stops biases the midpoint:
/* Direction keywords */
.a { background: linear-gradient(to bottom, #264de4, #a855f7); }
.b { background: linear-gradient(to right, #264de4, #a855f7); }
.c { background: linear-gradient(to bottom right, #264de4, #a855f7); }
/* Angle — 0deg = upward, 90deg = rightward, 180deg = downward */
.d { background: linear-gradient(135deg, #264de4, #a855f7); }
/* 3+ colour stops */
.e { background: linear-gradient(135deg, #264de4, #a855f7, #ec4899); }
/* Explicit stop positions */
.f { background: linear-gradient(to right,
#264de4 0%,
#a855f7 60%,
#ec4899 100%
); }
/* Hard stop — sharp colour change */
.g { background: linear-gradient(to right,
#264de4 50%, #a855f7 50%
); }
/* Transparent stops — overlay effect */
.h { background: linear-gradient(to bottom,
transparent, rgba(0,0,0,0.6)
); }
radial-gradient()
A radial-gradient radiates colours outward from a
central point. You can control the shape (circle or ellipse), the size
keyword and the centre position.
/* Basic circle */
.a { background: radial-gradient(circle, #4a8af4, #0f172a); }
/* Custom position */
.b { background: radial-gradient(circle at top left, #4a8af4, #0f172a); }
.c { background: radial-gradient(circle at 20% 80%, #4a8af4, #0f172a); }
/* Size keywords */
.d { background: radial-gradient(circle farthest-corner, #4a8af4, #0f172a); }
.e { background: radial-gradient(circle closest-side, #4a8af4, #0f172a); }
/* Explicit radius */
.f { background: radial-gradient(circle 80px at center, #4a8af4, #0f172a); }
/* Ellipse dimensions */
.g { background: radial-gradient(ellipse 80% 40% at center, #a855f7, #0f172a); }
/* Spotlight glow effect */
.glow {
background:
radial-gradient(circle at 30% 40%, rgba(38,77,228,.35), transparent 50%),
radial-gradient(circle at 75% 65%, rgba(168,85,247,.3), transparent 50%),
#0f172a;
}
conic-gradient()
A conic-gradient sweeps colour stops around a
central point, starting from a given angle. The result looks like a pie chart or a colour wheel. It's the
newest of the gradient types (all modern browsers since 2021).
/* Basic colour wheel */
.wheel {
background: conic-gradient(#264de4, #a855f7, #ec4899, #264de4);
border-radius: 50%; /* make it circular */
}
/* Pie chart — hard stops with % or deg */
.pie {
background: conic-gradient(
#264de4 0% 25%, /* blue — 25% slice */
#22c55e 25% 60%, /* green — 35% slice */
#f97316 60% 80%, /* orange — 20% slice */
#ef4444 80% 100% /* red — 20% slice */
);
border-radius: 50%;
}
/* Starting angle */
.rotated {
background: conic-gradient(from 90deg, #264de4, #a855f7, #264de4);
}
/* Custom centre position */
.offset {
background: conic-gradient(from 0deg at 30% 50%, #264de4, #a855f7, #264de4);
}
/* Progress ring using conic + mask */
.progress {
background: conic-gradient(#264de4 70%, #e5e7eb 70%);
border-radius: 50%;
/* 70% filled ring — pair with a mask for donut style */
}
Repeating Gradients
Repeating gradient variants tile the gradient pattern infinitely. Unlike using background-repeat, the tiling is seamless and defined by the gradient itself
— the pattern simply continues from where the last stop ended.
/* Diagonal stripes — 10px wide */
.stripes {
background: repeating-linear-gradient(
45deg,
#264de4 0, #264de4 10px, /* blue stripe 10px */
#0f172a 10px, #0f172a 20px /* dark stripe 10px */
);
}
/* Grid lines (horizontal + vertical) */
.grid {
background:
repeating-linear-gradient(90deg, transparent 0, transparent 39px,
rgba(38,77,228,.1) 39px, rgba(38,77,228,.1) 40px),
repeating-linear-gradient(0deg, transparent 0, transparent 39px,
rgba(38,77,228,.1) 39px, rgba(38,77,228,.1) 40px);
}
/* Dot pattern using radial */
.dots {
background: repeating-radial-gradient(
circle 10px, #264de4 0, #264de4 5px, transparent 5px, transparent 10px
);
}
/* Conic checkerboard */
.checker {
background: repeating-conic-gradient(#264de4 0deg 90deg, #0f172a 90deg 180deg);
background-size: 30px 30px; /* tile size */
}
Creative Gradient Effects
Gradients unlock a wide range of creative UI effects — gradient text, animated borders, mesh backgrounds and more — all pure CSS:
/* ── Gradient text ── */
.gradient-text {
background: linear-gradient(135deg, #264de4, #a855f7, #ec4899);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
/* ── Gradient border with mask technique ── */
.gradient-border {
position: relative;
background: #0f172a; /* element background */
border-radius: 10px;
}
.gradient-border::before {
content: '';
position: absolute;
inset: -2px; /* border thickness */
border-radius: inherit;
background: linear-gradient(135deg, #264de4, #a855f7);
z-index: -1;
}
/* ── Mesh gradient background ── */
.mesh {
background:
radial-gradient(ellipse at top left, rgba(38,77,228,.5), transparent 50%),
radial-gradient(ellipse at bottom right, rgba(168,85,247,.4), transparent 50%),
#0f172a;
}
/* ── Gradient progress bar ── */
.progress-fill {
background: linear-gradient(90deg, #264de4, #4a8af4, #a855f7);
height: 10px;
border-radius: 999px;
width: 75%; /* percentage filled */
}
Try It Yourself
Edit the code below to experiment with all four gradient types. Try changing the
angle, colour stops or swapping linear-gradient with radial-gradient.
Quick Check — Chapter 17
linear-gradient() — transitions between colours
along a straight line in any direction (keyword or angle). The most commonly used.radial-gradient() — radiates colours outward from a
central point in a circle or ellipse shape. Used for spotlight glows and soft ambient
backgrounds.conic-gradient() — sweeps colour stops around a
central point like a clock face. Used for pie charts, colour wheels and decorative sector
patterns.repeating-*-gradient() — each of the above three
has a repeating variant (repeating-linear-gradient, repeating-radial-gradient, repeating-conic-gradient) that tiles the pattern infinitely.All gradient functions produce images, not colours — they go in
background-image, not background-color.
linear-gradient(to right, #264de4 50%, #a855f7 50%)Both
#264de4 and #a855f7 are
declared at exactly 50%, so the colour changes instantly at that point without blending.This technique creates stripes, flags and division lines. For example, three equal thirds for a tricolour:
linear-gradient(to bottom, #ff9900 0% 33%, #fff 33% 66%, #128807 66% 100%)
radial-gradient — colours radiate outward
from the centre in concentric circles/ellipses. The transition goes from the centre to the
edges.conic-gradient — colours rotate around the
centre like clock hands. The transition sweeps through angles from 0° to 360°. The result looks like a
pie chart or colour wheel when viewed from above.Practical uses:
radial-gradient = spotlight, glow, ambient
background. conic-gradient = pie charts (border-radius: 50%), progress rings, colour pickers.
repeating-linear-gradient, repeating-radial-gradient, repeating-conic-gradient) work exactly like their non-repeating
counterparts, but once the defined pattern ends, it tiles infinitely.The key is that the last stop must not reach 100% — the remaining space is filled by repeating. For example:
repeating-linear-gradient(45deg, #264de4 0, #264de4 10px, #0f172a 10px, #0f172a 20px)This creates a stripe from 0–20px, then that stripe tiles to fill the entire element — creating infinite diagonal stripes without needing
background-repeat.Hard stops (same position twice) within repeating gradients create clean stripe patterns. Soft stops create banded gradient rings.
0deg = gradient goes upward (first colour at bottom, last at top)
90deg = gradient goes right (first colour at left, last at right) — equivalent to
to right180deg = gradient goes downward — equivalent to
to bottom (the default!)270deg = gradient goes left — equivalent to
to left45deg = diagonal top-left → bottom-right
135deg = diagonal top-right → bottom-left
Keyword alternatives:
to top, to bottom, to left, to right, to top right, etc.
linear-gradient when no direction is specified?linear-gradient(90deg, #264de4, #a855f7) produce?- CSS gradients are generated images — they go in
background-imageand respond to all background sub-properties. linear-gradient(direction, stop1, stop2, …)— transitions along a line. Direction: keyword (to right) or angle (90deg). Default = top to bottom. 0deg = upward, 90deg = rightward.- Colour stops control where each colour begins/ends. Explicit positions are optional. Hard stops (same position twice) create sharp edges. Colour hints (single %) bias the midpoint.
radial-gradient(shape size at position, stops)— radiates from a centre. Shape = circle or ellipse. Size keywords: closest-side, farthest-corner (default).conic-gradient(from angle at position, stops)— sweeps around a centre. Used for pie charts (border-radius: 50%), colour wheels and progress rings.- Repeating variants tile the gradient infinitely:
repeating-linear-gradient,repeating-radial-gradient,repeating-conic-gradient. Used for stripes, dot grids and checkerboards. - Creative effects: gradient text (
background-clip: text; color: transparent), gradient borders (pseudo-element technique), mesh gradients (multiple layered radials), gradient progress bars. - Multiple gradients can be stacked with commas — the first is on top. Combine with
background-coloras a fallback.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.