Tutorials CSS Gradients
Ch 17 / 60
Chapter 17 Beginner 8 min read CSS

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.

What you'll learn
linear-gradient — directions and colour stops
radial-gradient — circles, ellipses and positions
conic-gradient — pie charts and colour wheels
Repeating gradient variants
Colour stops, hints and hard transitions
Gradient text, borders and UI patterns

What 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:

linear-gradient()
Transitions between colours along a straight line. The most commonly used gradient. Supports any direction: top, bottom, left, right, diagonal angles.
radial-gradient()
Radiates colours outward from a central point in a circular or elliptical shape. Great for spotlight effects, glows and soft backgrounds.
conic-gradient()
Rotates colour stops around a central point — like the hands of a clock sweeping a colour wheel. Used for pie charts, colour wheels and decorative patterns.
repeating-*-gradient()
Repeating variants of the above three. The gradient pattern tiles infinitely, creating stripes, rings and sector patterns without background-repeat.
Gradients are images, not colours
Because gradients are treated as images, they go in 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

to bottom
to right
to top right
45deg
135deg
-45deg

Colour Stops

Colour stops control where each colour starts and ends. You can use %, px or leave positions out for automatic distribution.

#264de4 0%
#a855f7 50%
#ec4899 100%
0%25%50%75%100%

Hard stop — same position twice creates a sharp edge:

Colour hint — a single % between two stops biases the midpoint:

CSS — linear-gradient
/* 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.

circle
Equal x/y radii
ellipse
Proportional to box
at top left
Custom centre
at 20% 80%
Percentage position
farthest-corner
Extends to far corner
closest-side
Stops at nearest edge
circle 60px
Exact radius
ellipse 80% 40%
Width & height
CSS — radial-gradient
/* 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).

colour wheel
pie chart (3 equal)
pie chart (unequal)
rotated 45deg
offset centre
checkerboard
CSS — conic-gradient
/* 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
repeating-linear, 45deg
Horizontal lines
repeating-linear, 0deg
Vertical lines
repeating-linear, 90deg
Dot grid
repeating-radial circles
Checkerboard
repeating-conic
Subtle texture
semi-transparent stripe
CSS — Repeating gradients
/* 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-clip: text + color: transparent
Gradient Border
Gradient Border
mask technique on pseudo-element
Mesh Background
Mesh Gradient
Multiple radial gradients layered
Shine Button
Shine / Glass Overlay
Semi-transparent white gradient overlay
Gradient Progress Bars
linear-gradient fills in pill container
🇮🇳
Tricolour Flag
Hard stops at 33.3% each
CSS — Creative gradient effects
/* ── 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 */
}
Free Resource
CSS3 Cheat Sheet — All Properties & Selectors
Complete reference covering all gradient functions, colour stop syntax and background properties.
Download PDF

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.

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 17

Questions & Answers
CSS provides four gradient functions:

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.
A hard stop is created by giving two adjacent colour stops the same position. This eliminates the transition between them, producing a sharp edge:

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%)
Both are centred at a point, but they differ in how colour transitions are applied:

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 gradients (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.
CSS gradient angles start at 12 o'clock (top) and rotate clockwise — the opposite of mathematical convention:

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 right
180deg = gradient goes downward — equivalent to to bottom (the default!)
270deg = gradient goes left — equivalent to to left
45deg = 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.
Multiple Choice Questions
Question 1 of 5
CSS gradients are treated as which type of property value?
Question 2 of 5
What is the default direction of linear-gradient when no direction is specified?
Question 3 of 5
Which gradient type sweeps colours around a central point like a pie chart?
Question 4 of 5
How do you create a sharp colour edge (hard stop) in a gradient?
Question 5 of 5
What does linear-gradient(90deg, #264de4, #a855f7) produce?
Chapter Summary
  • CSS gradients are generated images — they go in background-image and 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-color as a fallback.
~8 min read
Beginner level
Chapter 17 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel