FreeVector

Guide · Design workflow

How to Make a CSS Gradient That Looks Right

A CSS gradient is an image the browser generates from a progressive transition between two or more colors, written in a function such as linear-gradient() and applied wherever an image is accepted. It needs no image file, scales without blur and can be edited as text. The difficult part is making the transition look smooth, keeping the colors from turning gray and knowing which syntax and fallback the browser will actually honor. This guide covers those decisions for linear, radial and conic gradients.

Key takeaways

  • A gradient is an image, so it belongs in background-image or background, never in background-color.
  • Name the direction, the stops and the interpolation space explicitly instead of relying on defaults.
  • RGB interpolation sends opposite hues through gray; oklab or oklch keeps the middle saturated.
  • Banding comes from limited color levels, so add stops, reduce the span or overlay subtle noise.
  • Stack radial gradients and blend modes to approximate a mesh gradient without an image file.
  • Ship a solid fallback color and check text contrast independently of the gradient behind it.

What is a CSS gradient, and where can you use one?

A CSS gradient is a browser-generated image made of a progressive transition between colors. MDN describes the result as a gradient data type, a special kind of image, which means it can go anywhere an image is accepted.

That distinction explains a common error. A gradient cannot be used on background-color, because that property expects a color rather than an image. Put the gradient in background-image or the background shorthand and set a solid color underneath.

Gradients have no intrinsic dimensions, so the browser stretches them to the element's box. The same declaration therefore looks different on a wide hero and a narrow card, which is why the element shape belongs in the design decision.

CSS images also carry no alternative text. MDN notes that a gradient used to draw a chart is invisible to screen reader users, so any information in the graphic needs a semantic equivalent in the document.

  • linear-gradient(): colors progress along a straight line.
  • radial-gradient(): colors radiate from a center point.
  • conic-gradient(): colors rotate around a center point.
  • The repeating- versions repeat a stop pattern to fill the box.

How does linear-gradient() read?

A linear gradient is defined by an axis, called the gradient line, and two or more color stops. The browser draws perpendicular bands of color along that line, which is why the result looks like a smooth ramp.

The direction can be a side or corner keyword, such as to right or to bottom left, or an angle. MDN states that the default is to bottom, that 0deg equals to top, and that angles increase clockwise from there.

Stops without positions are distributed evenly. A hint between two stops moves the midpoint of the transition, and two stops at the same position create a hard edge instead of a fade.

A short declaration such as linear-gradient(135deg, #12304F, #12A594) is often enough. Add explicit stop positions when the transition has to peak at a particular place rather than in the middle.

When is radial-gradient() the right choice?

A radial gradient starts at a center point and expands into an ending shape. MDN describes the center as the place where the 0 percent ellipse sits and the ending shape as the 100 percent boundary.

The defaults are an ellipse, centered in the box, sized to the farthest corner. The shape can be changed to circle, and the size can use closest-side, closest-corner, farthest-side or farthest-corner, or an explicit radius.

Radial gradients suit spotlights, glows, vignettes and soft color pools behind content. A transparent outer stop lets the gradient fade into a solid background without a visible edge.

A declaration such as radial-gradient(circle at 30% 20%, #FFC857, transparent 60%) places a warm pool in the upper left. Check the fade on the actual element, because the ending shape changes with the box's aspect ratio.

What can conic-gradient() do that the others cannot?

A conic gradient rotates color transitions around a center point instead of radiating outward. MDN explains that the starting point is north, or 12 o'clock, and that the from angle rotates the whole gradient clockwise.

Its color stops are angles rather than lengths. Two stops at the same angle create a hard boundary, which makes conic gradients useful for pie charts, color wheels, checkerboards and segmented rings.

The at position moves the center using the same values as background-position. Repeating-conic-gradient() repeats the pattern around a full rotation.

Use conic gradients for decoration with care. MDN warns that a pie chart drawn this way has no native alternative text, so a real chart needs markup or a text summary as well. MDN also lists conic-gradient() as widely available since November 2020.

Why do gradients turn gray, and how do you fix the interpolation?

Many gradients look muddy in the middle because the browser mixes the red, green and blue channels separately. Josh Comeau calls this the gray dead zone: a yellow to blue fade passes through gray because all three channels converge toward their middle value, 127.5.

The fix is to choose a better interpolation space. CSS allows an in keyword before the stops, for example linear-gradient(in oklab, #FFC857, #12304F), so the browser blends in a space that behaves more like human vision.

The CSS Images Level 4 draft explains why this matters. Gamma-encoded sRGB is too dark at the midpoint, CIE Lab gives a more perceptually uniform result, and Oklab produces almost the same result as CIE Lab.

MDN documents Oklab as the default interpolation space for color values, but declaring it explicitly keeps the result predictable. Comeau put browser support for the in syntax at around 91 percent in late 2025, so it is worth providing a plain sRGB gradient first and the enhanced version after it.

  • in oklab: a good default for smooth, natural transitions.
  • in oklch: adds hue controls such as shorter hue and longer hue.
  • in hsl longer hue: travels the long way around the color wheel for a vivid rainbow.
  • No keyword: the browser uses its default, which may vary with support.

How do you reduce banding?

Banding appears when a smooth transition is rendered with too few color levels. Wikipedia describes each pixel being rounded to the nearest digital color level, which turns a gradual fade into visible steps.

The problem is strongest in long, low-contrast gradients, such as a pale sky or a soft dark overlay across a full screen. The eye detects the boundaries between adjacent levels even when the difference is small.

Wikipedia lists dithering and more bits per channel as remedies. On the web, the practical version is to shorten the color span, add intermediate stops or overlay a very subtle noise texture.

CSS-Tricks shows a grain overlay built with SVG noise. Keep it faint and place it behind text, because visible grain on a content surface looks like a rendering defect rather than a texture.

  • Reduce the distance between the lightest and darkest stop.
  • Add one or two intermediate stops in the same hue family.
  • Overlay low-opacity noise for large, soft gradients.
  • Test on a cheap 8-bit display, where banding shows first.

How do you build mesh-like gradients with layers?

CSS has no native mesh gradient. The closest approach is to stack several radial gradients in one background, each with a soft transparent edge, so their color pools overlap like a mesh.

MDN explains that backgrounds are stacked from top to bottom, with the first specified on top. That order decides which pool covers the others, and background-blend-mode can change how the layers combine.

A useful recipe is three or four large radial gradients at different positions, one base color underneath and a grain layer on top. Keep the palette to three or four hues so the result reads as one surface rather than a pile of effects.

A declaration such as background: radial-gradient(circle at 20% 20%, #12A594, transparent 55%), radial-gradient(circle at 80% 30%, #FFC857, transparent 50%), #12304F gives a simple three-color field to refine. Check the corners, where overlapping transparent stops can leave a visible seam.

How do you keep gradients performant?

A gradient is cheap to download but not free to paint. The browser rasterizes it into the element's box, so a full-screen gradient on a large display costs more than a small card gradient, and animated changes force repainting.

web.dev recommends restricting animations to opacity and transform to keep them on the compositing stage, and avoiding properties that trigger layout or paint. Animating gradient stops or background-position falls into the expensive category.

If a gradient has to move, place it on an oversized pseudo-element and translate that layer with transform. Use will-change sparingly, as web.dev advises, and only after measuring a real problem.

Test on a low-end phone with the page scrolled, not only on a desktop monitor. The visual result may be identical while the frame rate is not.

What fallbacks does a CSS gradient need?

Start with a solid color on background-color, then declare the gradient on background-image. If the gradient fails to parse or a feature is unsupported, the solid color remains and the page stays readable.

Use @supports for progressive enhancement. A plain sRGB gradient can sit in the base rule, with an oklab or oklch version inside the feature query, so older browsers keep a reasonable result instead of losing the background entirely.

Forced colors mode and print need their own checks. High-contrast settings may remove backgrounds, and a gradient that prints smoothly on screen can band or disappear on paper.

Text contrast is a separate decision. A gradient behind a headline can pass at one end and fail at the other, so place text on a solid area, a scrim or a container with its own background.

  • Set background-color before background-image.
  • Put the simple gradient in the base rule.
  • Add the enhanced interpolation inside @supports.
  • Check forced colors, print and dark themes.
  • Keep text on a predictable surface.

How do you export CSS from a gradient generator?

A generator is useful for exploring angles and stop positions, especially when you want to compare several options quickly. cssgradient.io builds linear and radial gradients, offers a color picker and angle control, and provides a copy button for the generated CSS.

Its output includes a solid fallback color alongside the gradient declaration, which is a good habit to keep. The tool also offers a maximum compatibility setting for very old browsers, though most current projects do not need that output.

Clean the export before it enters a codebase. Check the angle direction, put the stops in ascending order, remove unused compatibility code, replace repeated values with custom properties and format the declaration for readability.

The preview in a generator is not a guarantee. It may use a different interpolation space or a different display profile, so paste the CSS into the real page and compare it there.

  • Keep the solid fallback color.
  • Confirm the angle and the stop order.
  • Remove prefixes and compatibility code you do not support.
  • Move repeated colors into custom properties.
  • Compare the result in the browser, not only in the preview.

What should you check before shipping a gradient?

Run the same checks on every gradient, whether it came from a generator or from hand-written CSS. Most failures are visible in a browser within a minute, but only if the page is tested at the sizes and settings where it will be used.

Check the fallback first, then the text on top, then the rendering at phone width. Finish with banding, dark mode and print if those contexts apply.

  1. Confirm the solid fallback color appears when the gradient is removed.
  2. Check text contrast on the busiest part of the gradient.
  3. View the element at 390 pixels wide and at desktop width.
  4. Inspect a large area on a low-quality display for banding.
  5. Test the light and dark themes.
  6. Check print or PDF output if the gradient appears there.
  7. Confirm that no meaning depends on the gradient alone.

If the gradient fails one of these checks, change the simplest thing that fixes it. A shorter color span or a solid scrim usually beats another layer of effects.

Frequently asked questions

What is the difference between linear, radial and conic gradients?

A linear gradient progresses along a straight line, a radial gradient expands from a center point, and a conic gradient rotates around a center point. All three accept color stops and can repeat. The choice depends on the shape of the effect: a band, a glow or a wheel.

The syntax for stops is similar, but conic stops use angles rather than lengths.

Why does my CSS gradient look gray in the middle?

The browser is probably interpolating in RGB, where opposite hues pass through a gray midpoint. Josh Comeau describes this as the gray dead zone, with all three channels converging near 127.5. Add an interpolation keyword such as in oklab or in oklch before the stops, and keep a plain sRGB version as a fallback for browsers without support.

How do I stop a CSS gradient from banding?

Reduce the distance between the lightest and darkest colors, add intermediate stops, or overlay very subtle noise. Banding comes from limited color levels, so a shorter, more gradual span gives the browser fewer visible steps. Test the result on a low-quality display and at the largest size the gradient will appear, because small previews hide the problem.

Can I animate a CSS gradient?

You can animate gradient values, but it is usually expensive because the browser has to repaint the area. Animating background-position or color stops also tends to look uneven. For movement, put the gradient on an oversized pseudo-element and animate transform or opacity instead, which web.dev recommends for keeping animation on the compositing stage.

Do CSS gradients need vendor prefixes?

Current browsers do not need prefixes for linear and radial gradients, and conic gradients have been widely available since November 2020 according to MDN. Very old output from generators may still include prefixed declarations for legacy browsers. Remove them unless the project has a documented requirement, and keep a solid fallback color instead.

How do I make a mesh gradient in CSS?

Stack several radial gradients in one background, each with a soft transparent edge, and let their color pools overlap. Add background-blend-mode if the layers need to mix differently, and put a faint noise layer on top. CSS has no native mesh gradient, so the result is an approximation that depends on careful stop positions and a limited palette.

Sources and further reading