Skip to main content
Syllabus
On this page

CSS Gradients (Linear, Radial, Conic)

Badar KhalilUpdated September 29, 2026 5 min read

What Are CSS Gradients?

CSS gradients are images generated by the browser that smoothly transition between two or more colors. They are used in background-image (and other places that accept an image), need no file download, and scale perfectly. Our focus keyword is CSS gradients, and this lesson covers linear-gradient, radial-gradient and conic-gradient, plus trending topics such as CSS gradient text, gradient borders, repeating gradients, CSS pie charts, mesh-style gradients, and interpolating in OKLCH for smoother colors.

1. linear-gradient()

A linear gradient moves along a straight line. Syntax: linear-gradient(direction, color-stop, color-stop, ...).

  • Default direction is to bottom (top to bottom).
  • Use keywords: to right, to bottom right.
  • Or angles: 45deg, 90deg (to right), 180deg (to bottom), 0deg (to top).
Code
background: linear-gradient(to right, #22c55e, #3b82f6);
background: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);

2. Color Stops and Hints

  • A color stop has a color and an optional position: red 20%.
  • Two stops at the same position make a hard edge (no blending): red 50%, blue 50%.
  • Two positions on one color: red 20% 40% makes a solid band.
  • A color hint is a lone percentage between stops (red, 30%, blue) that moves the midpoint of the transition.
  • Use transparent for fades. Modern browsers handle transparent correctly, blending in premultiplied alpha.

3. radial-gradient()

A radial gradient radiates from a center point. Syntax: radial-gradient(shape size at position, stops).

  • Shape: circle or ellipse (default).
  • Size: closest-side, farthest-corner (default), closest-corner, farthest-side, or explicit lengths.
  • Position: at center, at top left, at 30% 70%.
Code
background: radial-gradient(circle at 30% 30%, #fde68a, #f59e0b 60%, #b45309);

4. conic-gradient()

A conic gradient rotates colors around a center point, like a color wheel or the sweep of a radar. Syntax: conic-gradient(from angle at position, stops). Stops use angles or percentages of a full circle.

  • Pie charts: hard stops such as #3b82f6 0 40%, #f59e0b 0 70%, #22c55e 0.
  • Color wheels: conic-gradient(red, yellow, lime, aqua, blue, magenta, red).
  • Checkerboards: repeating-conic-gradient with a small background-size.

5. Repeating Gradients

Use repeating-linear-gradient(), repeating-radial-gradient() and repeating-conic-gradient() to tile the color stops. They are ideal for stripes, rings and loading indicators.

6. Interpolation Color Space

By default gradients blend in sRGB, which can look gray or muddy in the middle (for example blue to yellow). You can choose the color space: linear-gradient(in oklch, #2563eb, #facc15) or in oklab. Add hue interpolation like in oklch longer hue for rainbow effects. This is a trending technique for vibrant, smooth gradients.

Gradient text

Use a gradient background with background-clip: text and a transparent text color.

Gradient borders

Layer two backgrounds: a solid fill clipped to padding-box and a gradient clipped to border-box, with a transparent border. Or use border-image (which does not work with rounded corners).

Overlays for text readability

linear-gradient(to top, rgb(0 0 0 / 70%), transparent) over a photo.

Mesh-like backgrounds

Stack several radial-gradient() layers with transparent ends over a base color for a soft, modern look.

Skeleton loaders

Animate background-position of a wide linear gradient to create a shimmer effect.

8. Performance and Accessibility

  • Gradients are cheap to render, but very large animated gradients can cost repaint time. Animate transform or background-position rather than changing the stops.
  • Check text contrast against the lightest and darkest parts of the gradient.
  • Provide a solid background-color fallback for old browsers.

Best Practices

  • Use two to three colors for clean gradients. More stops need more care.
  • Use OKLCH interpolation to avoid dull midpoints.
  • Store gradients in custom properties: --grad: linear-gradient(...).
  • Use hard stops for charts and stripes, soft stops for backgrounds.

Common Mistakes

  • Forgetting that 0deg points up, not right.
  • Using gradients with background-color only and expecting background-color: linear-gradient() to work. Gradients belong in background-image or the background shorthand.
  • Placing a color hint next to another hint, which is invalid.
  • Expecting border-image with a gradient to follow border-radius.

Summary

Linear gradients follow a line, radial gradients spread from a point and conic gradients sweep around a point. Add repeating variants, hard stops and OKLCH interpolation and you can build backgrounds, charts, patterns and loaders with pure CSS. Practice with the live examples below.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Try it Yourself HTML
Output

Press Run to execute.

Exercise: Build a CSS Pie ChartHTML

Using conic-gradient and border-radius: 50%, draw a pie chart 200px wide and tall. Segments: 50% blue (#3b82f6), 30% orange (#f59e0b), 20% green (#22c55e). Use hard stops.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
A circle divided into 50%, 30% and 20% colored segments.

This is a self-check — compare your result with the expected output above.

Exercise: Striped Progress BarHTML

Make the .bar element look like a striped progress bar using repeating-linear-gradient at 45deg with two shades of green. Keep height 24px and rounded corners.

Try it Yourself HTML
Output

Press Run to execute.

Show expected output
A 24px tall bar with diagonal green stripes and rounded corners.

This is a self-check — compare your result with the expected output above.

Quiz: CSS Gradients Quiz Pass 70%
Loading quiz…

Was this page helpful?