Radial gradients in CSS: shapes, size keywords and glows

How CSS radial-gradient() works: circles and ellipses, the four size keywords, rings, dots and glows.

Updated

A radial gradient spreads colour outward from one point, in an ellipse or a circle. In CSS it's the radial-gradient() function, used anywhere an image goes, usually background:

.glow {
  background: radial-gradient(
    circle at 50% 100%,
    #93c5fd,
    #1d4ed8 40%,
    #0b1020 70%
  );
}

That's a circle centred on the bottom edge, light blue in the middle, fading to a near-black navy. Everything before the first colour is optional. With none of it, radial-gradient(red, blue) draws an ellipse the shape of its box, centred, reaching out to the corners.

The demo shows each part of the syntax on the same box: the four size keywords, ellipse against circle, a colour hint, and the patterns from later in this post. It's all plain CSS, plus a few lines of JavaScript for the spotlight.

Size keywords, at 25% 50%

closest-side
closest-corner
farthest-side
farthest-corner

Shape and hints

ellipse (default)
circle
hint at 20%

Patterns

repeating rings
dot grid
vignette
spotlight: move the pointer

The syntax

radial-gradient(
  [shape] [size] [at position] [in colorspace],
  color-stop, [hint,] color-stop, ...
)
  • Shape: ellipse (the default) or circle.
  • Size: where the gradient ends. A keyword (closest-side, closest-corner, farthest-side, farthest-corner), one length for a circle, or two lengths or percentages for an ellipse.
  • Position: the centre, after at. It takes the same values as background-position: at center, at 25% 50%, at right 20px top 10px.
  • Colour space: in oklab, in oklch and the others, to change how the stops blend.
  • Stops: colours, each with an optional position along the ray from the centre to the ending shape. 0% is the centre and 100% is the ending shape.

A ray is the line from the centre out to the ending shape. Every stop position is measured along it. On an ellipse, 50% means halfway to the edge of the ellipse in that direction, so the stops stretch with the shape.

Ellipse or circle

Centred and with no size, an ellipse takes the proportions of its box. Move the centre and a keyword-sized ellipse changes shape, because its radii come from the distances to the sides (next section). A circle stays round in any box:

radial-gradient(#fde68a, #ea580c, #431407)          /* ellipse */
radial-gradient(circle, #fde68a, #ea580c, #431407)  /* circle */

For an explicit size, a circle takes one length and an ellipse takes two, horizontal then vertical:

radial-gradient(circle 200px, ...)    /* valid */
radial-gradient(300px 120px, ...)     /* ellipse, valid */
radial-gradient(60% 40%, ...)         /* ellipse, valid */
radial-gradient(circle 50%, ...)      /* invalid */

The last one is the common mistake. A circle can't take a percentage, because a percentage of a box that isn't square has two answers. Chrome 154's CSS.supports() returns false for it, and the browser drops the whole declaration, so the element gets no background at all. Use a length, or an ellipse with two equal-looking percentages.

The four size keywords

The keyword picks which side or corner of the box the ending shape reaches:

  • closest-side: the ending shape touches the nearest side. For an ellipse, it touches the nearest side horizontally and the nearest side vertically.
  • farthest-side: the same, using the farthest sides.
  • closest-corner: the ending shape passes through the nearest corner.
  • farthest-corner: passes through the farthest corner. This is the default.

They differ most when the centre is off to one side. Here is what each one gives for a 600×200 box with the centre at 25% 50%, which is 150px from the left edge and 100px from the top and bottom. The demo above uses the same box shape:

KeywordEllipse radii (x × y)Circle radius
closest-side150 × 100px100px
closest-corner212 × 141px180px
farthest-side450 × 100px450px
farthest-corner636 × 141px461px

For an ellipse, the corner keywords are the side keywords times √2, about 1.414. The spec gives a corner ellipse the same proportions as the matching side ellipse, then scales it to pass through the corner, and the corner sits exactly one side distance across and one down. So closest-corner is always closest-side scaled up by √2, in any box. A hard edge at 100% shows it: in Chrome 154 the closest-side ellipse above ends 300px from the left of the box, and the closest-corner one ends at 362px, 150 + 212.

That √2 explains a surprise with the default. A centred radial-gradient(red, blue) is farthest-corner, so the last colour lands on the corners and the middle of each side is only 1/√2, 70.7%, of the way along the ray. In Chrome 154, a 400×200 box with that gradient is rgb(75 0 180) at the middle of its right edge, a 29/71 mix of red and blue, and pure blue only in the corners. If the last colour should reach the sides, use farthest-side, or put the last stop at 70.7%.

Positions, including off the box

at takes keywords, percentages or lengths, and the centre doesn't have to be inside the box:

/* a glow rising from below the bottom edge */
background: radial-gradient(
  80% 60% at 50% 120%,
  #c4b5fd,
  #4c1d95 50%,
  #0b0618 80%
);

At 120%, the centre sits a fifth of the box's height below it, so only the top of the ellipse shows, like light from just over a horizon. The size keywords measure from wherever the centre is, so with an off-box centre, give an explicit size. Percentage sizes are percentages of the box, not of the distance to an edge.

Stops, hints and hard edges

Stops work as in linear-gradient. A stop can take two positions to hold a colour for a stretch, and stops at the same position give a hard edge:

/* a solid disc with a sharp rim */
radial-gradient(circle 80px, #000 100%, #fff 100%)

A bare percentage between two colours is a colour hint. It moves the halfway point of that blend:

radial-gradient(circle 100px, #000, 20%, #fff)

Without the hint, black meets white halfway at 50px out. With it, the halfway grey, rgb(128 128 128) in Chrome 154, sits at 20px, so the black falls off fast and the white takes over most of the circle.

Hard edges on a radial gradient are curved, and on a pixel grid a curve needs blended pixels to look smooth. In Chrome 154 with software rendering, a hard stop came out with no blended pixels at all along the diagonal of the rim: the stairs show. Giving the edge one pixel to blend over smooths it:

radial-gradient(circle 80px, #000 calc(100% - 1px), #fff 100%)

Fading to transparent

transparent is safe to fade to. It's black at zero opacity, and in older engines a fade from red to transparent passed through dark, half-transparent red. Current browsers blend stops with premultiplied alpha, so the colour stays red while only the opacity drops. In Chrome 154, radial-gradient(circle 100px, red, transparent) over white measures rgb(255 129 129) halfway out, a clean pink, with no grey fringe.

That's what makes stacked glows work. Each layer fades to transparent, and the layers underneath show through:

background:
  radial-gradient(60% 80% at 20% 30%, #f0abfc, transparent 70%),
  radial-gradient(60% 80% at 80% 70%, #67e8f9, transparent 70%), #0f172a;

Stack four or five of those and you have a mesh gradient in CSS. Pastel gradients in CSS builds one from four pastel radial gradients over a warm off-white.

Colour space

Put in oklab or another colour space with the shape, size and position, before the first comma. Chrome 154 accepts it before or after them:

background: radial-gradient(circle at 25% 50%, #fde68a, #be123c);
background: radial-gradient(circle at 25% 50% in oklab, #fde68a, #be123c);

The first line is the fallback. in oklab works in Chrome and Edge 111, Safari 16.2 and Firefox 127, and older browsers drop the whole second declaration and keep the first. OKLab keeps the middle of the blend from going dark and dull the way sRGB often does. CSS gradient vs WebGL gradient shows the difference, and the colour posts measure it for each hue.

Repeating rings

repeating-radial-gradient() repeats its stops outward until the box is full. Stops with two positions make crisp bands:

background: repeating-radial-gradient(
  circle at 50% 100%,
  #1e1b4b 0 12px,
  #4338ca 12px 24px
);

Keep the rings at least a few pixels wide. Rings close to one pixel wide fall between pixels and can break into moiré, curved patterns that aren't in the CSS.

Dots, vignettes and spotlights

Three patterns cover most of what radial gradients are used for on real sites.

A dot grid. A tiny circle, tiled with background-size:

background:
  radial-gradient(circle, #64748b 1.5px, transparent 2px) 0 0 / 16px 16px,
  #f8fafc;

The half-pixel gap between 1.5px and 2px blends the dot's edge, for the reason in the hard-edges section.

A vignette. A transparent centre that darkens toward the sides, layered over any background:

background:
  radial-gradient(closest-side, transparent 55%, rgb(0 0 0 / 0.65)),
  linear-gradient(135deg, #f0abfc, #6366f1);

closest-side makes the dark edge touch all four sides. With the default farthest-corner, the darkest part only reaches the corners.

A spotlight that follows the pointer. Put the centre in two custom properties and set them from JavaScript:

.spotlight {
  background:
    radial-gradient(
      240px circle at var(--x, 50%) var(--y, 50%),
      rgb(255 255 255 / 0.22),
      transparent 70%
    ),
    #0b1020;
}
el.addEventListener("pointermove", (e) => {
  const box = el.getBoundingClientRect();
  el.style.setProperty("--x", `${e.clientX - box.left}px`);
  el.style.setProperty("--y", `${e.clientY - box.top}px`);
});

When the properties are only used in background, changing them repaints the element without a layout, which is cheap enough to do on every pointer move.

Animating a radial gradient

A transition on background does nothing for a gradient: the browser can't blend one gradient image into another, so it jumps. To animate one, put the part that moves in a custom property and register it with @property, so the browser knows it's a percentage it can interpolate:

@property --glow-y {
  syntax: "<percentage>";
  inherits: false;
  initial-value: 120%;
}

.hero {
  --glow-y: 120%;
  background: radial-gradient(
    80% 60% at 50% var(--glow-y),
    #c4b5fd,
    #4c1d95 50%,
    #0b0618 80%
  );
  animation: rise 6s ease-in-out infinite alternate;
}

@keyframes rise {
  to {
    --glow-y: 95%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .hero {
    animation: none;
  }
}

That moves the centre of the glow up and down. The same trick works for sizes (syntax: "<length-percentage>") and colours (syntax: "<color>"). @property is in Chrome 85, Safari 16.4 and Firefox 128. Without it, the animation jumps between its end states instead of moving. Declare --glow-y: 120% on the element too, as above, so a browser that ignores @property still has a value and draws the gradient.

Seamless looping gradient animations covers making a loop like this repeat with no visible restart.

Radial gradients in Tailwind

Tailwind v4 has a bg-radial utility, used with the same from-, via- and to- stops as linear gradients:

<div class="bg-radial from-sky-200 via-blue-500 to-indigo-950"></div>

Two things about what it compiles to, checked in Tailwind 4.3.3:

  • bg-radial blends in OKLab: it compiles to radial-gradient(in oklab, …), with no plain fallback. In a browser older than the versions above, the element has no gradient. bg-linear-to-* writes a fallback first; bg-radial doesn't.
  • An arbitrary value replaces that in oklab. bg-radial-[at_25%_25%] compiles to radial-gradient(at 25% 25%, …), so the colours decide. Tailwind's own colours are oklch() values, which still blend in OKLab; hex colours such as from-[#fde68a] blend in sRGB. To be sure of OKLab with a custom shape or position, write it in: bg-radial-[circle_at_25%_25%_in_oklab].

Underscores stand for spaces inside the brackets, so any of the syntax above fits in an arbitrary value.

A radial glow that moves

A CSS radial gradient can move its centre, size and colours, but its shape stays a smooth ellipse. Radial Glow is a shader that draws what CSS can't: a dark disc low in the frame with light spilling round its rim, a corona, and god rays that drift outward. It's one of the free components.

Rising soon

Install it with the shadcn CLI:

npx shadcn@latest add https://gradients.sh/r/radial-glow.json

Before the shader starts, and in browsers without WebGL, the component paints a CSS version of itself. For Radial Glow that's two radial gradients and the background colour, built from the same colours. With the default colours it's:

background:
  /* the lit rim: a thin ring of colour at 54–82% of the ray */
  radial-gradient(
    42% 58% at 50% 82%,
    transparent 54%,
    color-mix(in oklab, #f3f1ff 80%, transparent) 58%,
    color-mix(in oklab, #9db4ff 45%, transparent) 64%,
    transparent 82%
  ),
  /* the haze outside the ring, fading through the palette */
  radial-gradient(
      84% 116% at 50% 82%,
      transparent 27%,
      color-mix(in oklab, #f3f1ff 45%, transparent) 29%,
      color-mix(in oklab, #9db4ff 35%, transparent) 42.3%,
      color-mix(in oklab, #5a46cb 25%, transparent) 55.7%,
      color-mix(in oklab, #1d1240 15%, transparent) 69%,
      transparent 85%
    ),
  #020204;

Both layers are ellipses centred low in the box and start transparent, so the middle keeps the dark background colour and reads as the disc. The ring stays transparent to 54% and reaches full colour by 58%, a short blend that reads as a rim. It's a still picture of the same shape, so the page never shows an empty box while the shader loads.

Animated gradient background in shadcn/ui covers placing a component like this behind a hero and theming it with CSS variables.