Skip to content
Devtoolry
Web Development

CSS Gradients: Linear, Radial and Conic with Practical Examples

Learn CSS gradients by example: angles, colour stops, hard stops, stripes, radial glows, conic charts, gradient text and borders, and smoother oklch blends.

Photo of Avinash KoriBy Avinash KoriPublished 5 min read

Three panels showing a linear gradient, a radial gradient and a conic gradient with their colour stops marked

Gradients are one of the most useful tools in CSS: backgrounds and hero sections, buttons with depth, subtle glows, progress bars, stripes, pie charts, even gradient text, all without a single image file. They're resolution-independent, tiny, and easy to animate or theme with custom properties.

The syntax is compact but has a few details that trip people up: which way angles point, how colour stops work, and why some gradients look muddy in the middle. This guide covers all three gradient types with practical examples. Build and tweak any of them visually with the CSS Gradient Generator.

Gradients are images

The first thing to know: a CSS gradient is an <image> value, not a colour. That means it goes in background-image (or the background shorthand), not background-color, and it can be layered with other backgrounds:

CSS
.hero {
  background-color: #312e81;                              /* fallback */
  background-image: linear-gradient(135deg, #312e81, #7c3aed);
}

Always set a solid background-color underneath. It shows while the page loads, in high-contrast modes, and in any environment that doesn't render the gradient, and it's what tools use to check text contrast.

Linear gradients

CSS
background: linear-gradient(135deg, #5b5bf0 0%, #a855f7 100%);

A linear gradient blends colours along a straight line. The first argument sets the direction:

  • Keywords: to right, to bottom (the default), to top left, …
  • Angles: 0deg points up, and angles go clockwise: 90deg points right, 180deg down. That's different from math class, where 0° points right. 135deg therefore runs from top-left to bottom-right.

Corner keywords like to top right adapt to the element's shape so the gradient line meets the corner exactly, while a fixed angle like 45deg doesn't. On a wide banner, those two look noticeably different.

Colour stops

Each colour can have a position. Without positions, colours are spread evenly. With them, you control where each colour is reached:

CSS
/* Purple holds until 30%, then blends to pink by 70% */
background: linear-gradient(to right, #5b5bf0 30%, #ec4899 70%);

A bare percentage between two colours is a colour hint: it moves the midpoint of the blend.

CSS
background: linear-gradient(to right, #5b5bf0, 20%, #ec4899); /* reaches the halfway colour at 20% */

Hard stops and stripes

When two stops share the same position, there's no blending, just a sharp edge:

CSS
/* A two-tone split */
background: linear-gradient(to right, #5b5bf0 50%, #e5e7eb 50%);

/* A progress bar at 65% */
background: linear-gradient(to right, #22c55e 65%, #e5e7eb 65%);

repeating-linear-gradient repeats the pattern, which makes stripes easy:

CSS
/* Diagonal warning stripes, 20px each */
background: repeating-linear-gradient(45deg, #facc15 0 20px, #1f2937 20px 40px);

The two-position syntax (#facc15 0 20px) is shorthand for two stops of the same colour.

Radial gradients

CSS
background: radial-gradient(circle at top left, #a855f7, transparent 60%);

A radial gradient spreads outward from a point. The parts:

  • Shape: circle or ellipse (the default, which stretches with the element).
  • Size: where the gradient ends: closest-side, closest-corner, farthest-side, farthest-corner (the default), or explicit lengths.
  • Position: at center (the default), at top left, at 30% 20%, …

Radial gradients that fade to transparent are the classic way to add soft glows. Layer a few for an ambient background:

CSS
.page {
  background-color: #0a0c12;
  background-image:
    radial-gradient(40% 50% at 15% 10%, rgb(91 91 240 / 0.35), transparent),
    radial-gradient(35% 45% at 85% 20%, rgb(168 85 247 / 0.25), transparent);
}

The first gradient listed is drawn on top.

Conic gradients

CSS
background: conic-gradient(from 0deg, #5b5bf0, #ec4899, #f59e0b, #5b5bf0);

A conic gradient sweeps colours around a centre point, like the hands of a clock. Repeating the first colour at the end avoids a hard seam. With hard stops, conic gradients make pie charts in pure CSS:

CSS
.pie {
  width: 160px;
  aspect-ratio: 1;
  border-radius: 50%;
  background: conic-gradient(#5b5bf0 0 45%, #22c55e 45% 75%, #f59e0b 75% 100%);
}

Add a radial-gradient mask or an inner circle and it becomes a donut chart. For anything interactive or labelled, a real chart is better for accessibility, but for a decorative indicator this is hard to beat.

Gradient text

CSS
.gradient-text {
  background: linear-gradient(90deg, #5b5bf0, #a855f7);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

The background is clipped to the shape of the text and the text itself is made transparent. Keep it for large headings: thin text with a gradient is hard to read, and check that both ends of the gradient have enough contrast with the page background. The text stays real text, so it's still selectable and readable by screen readers.

Gradient borders

border-color doesn't accept gradients, but two backgrounds with different clipping do the job, including with rounded corners:

CSS
.card {
  border: 2px solid transparent;
  border-radius: 12px;
  background:
    linear-gradient(#ffffff, #ffffff) padding-box,
    linear-gradient(135deg, #5b5bf0, #ec4899) border-box;
}

The first layer fills the inside (the padding box) with the card colour; the second fills the whole border box with the gradient, which shows only through the transparent border.

Avoiding muddy gradients

Blend blue into yellow and the middle often turns a dull grey. By default, browsers interpolate gradients in the sRGB colour space, where the straight line between two complementary colours passes through desaturated territory.

Modern browsers let you choose the colour space:

CSS
background: linear-gradient(in oklch, #2563eb, #facc15);

OKLCH is a perceptual colour space, so the blend keeps its brightness and saturation more evenly. Support arrived in Chrome and Edge 111, Safari 16.2 and Firefox 127; older browsers ignore the whole declaration, so keep an sRGB version first as a fallback:

CSS
background: linear-gradient(#2563eb, #facc15);
background: linear-gradient(in oklch, #2563eb, #facc15);

Another trick that works everywhere: add an intermediate colour stop yourself, picked from a more saturated hue between the two. The Color Converter helps you find in-between colours by adjusting hue in HSL.

Banding is the other common problem: visible steps in long, subtle gradients, especially dark ones. Shorten the distance, increase the colour difference slightly, or add a very faint noise texture on top.

Performance and accessibility tips

  • Gradients are cheap to render and need no network request. Animating a gradient's colours repaints the element every frame, though; for animated effects, animate background-position on an oversized gradient, or opacity of a layered element, instead.
  • Check text contrast against the lightest and darkest parts of the gradient behind it, not just the middle.
  • Use custom properties for themeable gradients: linear-gradient(135deg, var(--brand-start), var(--brand-end)) adapts to dark mode automatically.
  • Respect reduced motion if you animate gradients: wrap animations in @media (prefers-reduced-motion: no-preference).

If you're designing assets that leave the browser, such as email signatures, note that many email clients don't support CSS gradients; use a solid colour or an image there, as explained in our HTML email signature guide.

Summary

CSS gradients are images: linear-gradient blends along a line (with 0deg pointing up), radial-gradient spreads from a point, and conic-gradient sweeps around one. Colour stops control positions, shared positions make hard edges for stripes, bars and pie charts, and layering creates glows, gradient text and gradient borders. Always add a solid fallback colour, interpolate in oklch for cleaner blends, and check text contrast across the whole gradient.

Photo of Avinash Kori

Written by

Avinash Kori

Founder of Devtoolry

Avinash is a full-stack web developer from Surat, India, with eight years of experience in PHP, Laravel and Angular. He built Devtoolry so developers can solve everyday problems quickly with free tools that run entirely in the browser, and writes practical guides on the formats, encodings and techniques behind them.

Connect on LinkedIn

Try it yourself

The free tools used in this article. They run in your browser, so nothing you paste is uploaded.