Learn

Gradient Basics for UI

CSS gradients are one of the most underused tools in a developer's kit. Here's what you need to know to use them well.

What is a CSS gradient?

A CSS gradient is a function that generates an image by smoothly transitioning between two or more colors. Because it's treated as an image by the browser, it can be used anywhere an image is valid — most commonly as a background or background-image.

Unlike a solid color, gradients have no "source file" — they're computed live by the browser. This makes them infinitely scalable, resolution-independent, and zero-cost to load.

The three types

CSS supports three gradient functions:

  • linear-gradient() — colors flow in a straight line at a given angle.
  • radial-gradient() — colors radiate outward from a center point.
  • conic-gradient() — colors rotate around a center point (like a pie chart).

Each has a repeating- variant (repeating-linear-gradient() etc.) that tiles the gradient pattern.

Basic syntax

/* Linear — 135 degree diagonal */
background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);

/* Radial — circle from center */
background: radial-gradient(circle, #f97316 0%, #1e1b4b 100%);

/* Conic — sweeping from top */
background: conic-gradient(from 0deg, #f43f5e, #6366f1, #f43f5e);

Color stops

A gradient needs at least two color stops. Each stop defines a color and an optional position (percentage or length along the gradient axis).

/* Two stops — from 0% to 100% (default) */
background: linear-gradient(to right, #ff6b35, #4776e6);

/* Three stops with custom positions */
background: linear-gradient(90deg, #ff6b35 0%, #f7931e 40%, #ffcc02 100%);

Positions are optional — if omitted, stops are evenly distributed. Explicit positions let you control "dwell time" (how long each color holds before transitioning to the next).

When to use gradients in UI

  • Hero backgrounds — gradients add depth without heavy images.
  • Buttons and badges — a subtle gradient gives a polished, elevated feel.
  • Card backgrounds — differentiate sections without borders or shadows.
  • Data visualization — sequential and diverging color scales.
  • Progress bars and range inputs — the fill can be a gradient to show range.
  • Glassmorphism overlays — semi-transparent gradients for frosted-glass effects.

When not to use gradients

  • As a background behind dense body text — contrast is inconsistent.
  • When the design calls for a flat, minimalist aesthetic.
  • As a substitute for semantic color choices in data (use categorical colors instead).

Quick tips

  • Use to right, to bottom etc. instead of angles for directional gradients that should adapt to LTR/RTL layouts.
  • Gradients can be layered — add multiple backgrounds separated by commas.
  • Use background-size and background-position to tile or offset gradients.