Skip to content
Hex Color Picker

Blog

CSS Gradients Guide: Linear, Radial and Conic, With Examples

By the Hex Color Picker Team. Published . 8 minute read.

A CSS gradient is a background image that the browser draws for you, so it needs no file download and stays sharp at any size. There are three types: linear-gradient() blends colors along a line, radial-gradient() blends them outward from a point, and conic-gradient() sweeps them around a point like a color wheel. Every gradient is defined by a list of color stops, and you can build one visually with the CSS gradient generator and copy the code.

This guide covers the syntax of all three types, how color stops and angles work, how to make hard edges and stripes, why some gradients look muddy in the middle, and how to keep text readable on top of them. All the examples are valid CSS you can paste into a stylesheet.

The basic idea

A gradient needs at least two colors. The browser blends from the first to the last, and the position of each color is its stop. You use a gradient anywhere an image is allowed, most often the background property:

.hero {
  background: linear-gradient(90deg, #1E90FF 0%, #8A2BE2 100%);
}

This reads as: draw a line at 90 degrees (left to right), start with dodgerblue at 0%, and finish with blueviolet at 100%. The blend between the two is calculated for every pixel.

Linear gradients

Linear gradients run along a straight line. You control the direction with an angle or with keywords:

.a { background: linear-gradient(90deg, #1E90FF, #8A2BE2); }      /* left to right */
.b { background: linear-gradient(to right, #1E90FF, #8A2BE2); }   /* same as 90deg */
.c { background: linear-gradient(180deg, #1E90FF, #8A2BE2); }     /* top to bottom */
.d { background: linear-gradient(45deg, #1E90FF, #8A2BE2); }      /* toward the top right */

Angles are measured clockwise from the top:

AngleDirection of the blendKeyword
0degBottom to topto top
90degLeft to rightto right
180degTop to bottomto bottom (the default)
270degRight to leftto left

If you leave out the direction, the default is 180deg, so the first color is at the top. If you only give colors, the stops are spaced evenly.

Color stops

Each color can have a position, written as a percentage or a length. Moving the stops changes where the blend happens:

/* The blend happens between 20% and 80%. Before 20% is solid blue, after 80% is solid violet. */
.stops { background: linear-gradient(90deg, #1E90FF 20%, #8A2BE2 80%); }

/* Three colors with the middle one pushed toward the start */
.three { background: linear-gradient(90deg, #1E90FF 0%, #FFFFFF 30%, #8A2BE2 100%); }

You can use as many stops as you like. The gradient generator supports up to five and writes the stops in order for you.

Hard edges and stripes

If two stops are at the same position, there is no blend between them. The color changes instantly, which is how you draw stripes and flat bands in CSS:

/* Half blue, half violet, with a sharp edge in the middle */
.split { background: linear-gradient(90deg, #1E90FF 50%, #8A2BE2 50%); }

/* Repeating diagonal stripes */
.stripes {
  background: repeating-linear-gradient(45deg, #1E90FF 0 10px, #FFFFFF 10px 20px);
}

The second example uses the two-position stop syntax, #1E90FF 0 10px, which means "this color from 0 to 10 pixels". repeating-linear-gradient() then tiles the pattern across the element.

Radial gradients

A radial gradient spreads outward from a center point, as a circle or an ellipse:

.spot  { background: radial-gradient(circle at center, #FFFFFF 0%, #1E90FF 100%); }
.glow  { background: radial-gradient(circle at 30% 20%, #8A2BE2, #1E90FF 60%, #0B1B3A); }
.wide  { background: radial-gradient(ellipse at top, #1E90FF, #0B1B3A); }

circle keeps a round shape, and ellipse (the default) stretches to fit the element. The position after at can be keywords like center and top, or coordinates. Radial gradients are good for spotlights, glows behind a product, and soft vignettes. Because the angle has no meaning for a radial gradient, the angle control on the generator is hidden for that type.

Conic gradients

A conic gradient sweeps around a center point, like the hands of a clock:

.wheel { background: conic-gradient(from 0deg at 50% 50%, #FF0000, #FFFF00, #00FF00, #00FFFF, #0000FF, #FF00FF, #FF0000); }

/* A simple pie chart: 40% blue, then orange to the end */
.pie {
  background: conic-gradient(#1E90FF 0 40%, #FFA500 40% 100%);
  border-radius: 50%;
}

The from value sets the starting angle, measured clockwise from the top. Conic gradients are the right tool for color wheels, pie and donut charts, and loading rings. For a pie chart, use hard stops, as in the example above, and round the element with border-radius: 50%.

Smooth blends and the grey middle

Gradients are normally calculated by averaging the red, green and blue values halfway between two colors. That works well for similar colors, but it can look dull when you blend colors that are far apart on the color wheel.

The classic example is blue to yellow. Pure blue is #0000FF and pure yellow is #FFFF00. Their midpoint, computed channel by channel, is rgb(128, 128, 128), which is #808080, a flat grey. So a blue-to-yellow gradient passes through a muddy grey band in the middle. Red to cyan does the same. Dodgerblue to blueviolet, which are neighbors on the wheel, midpoints at #545EF1, a vivid blue-violet, so there is no problem.

There are three common fixes:

  1. Add a stop in the middle. Put a bright intermediate color, such as a light green or white, between the two ends.
  2. Choose colors closer on the wheel. Analogous colors blend cleanly. See complementary vs analogous schemes.
  3. Change the interpolation color space. Recent browsers let you ask for the blend to be calculated in a different space, for example linear-gradient(in oklch, #0000FF, #FFFF00). Blending in a perceptual space can avoid the grey dip. This is a newer feature, so check a browser support table, and keep a normal gradient as a fallback.

Transparency in gradients

Gradients can fade to transparent, which is a common way to darken part of an image so text sits on it more readably:

.caption-bg {
  background: linear-gradient(to bottom, rgb(0 0 0 / 0%), rgb(0 0 0 / 65%));
}

You can also stack several backgrounds, separated by commas, with the first one on top:

.card {
  background:
    linear-gradient(to bottom, rgb(0 0 0 / 0%), rgb(0 0 0 / 60%)),
    url("photo.jpg") center / cover;
}

Here a dark gradient overlays a photograph, giving white text at the bottom a consistent backdrop. Transparent values are described in the guide to colors in CSS.

Fallbacks

Gradients are supported by every current browser, but it is still good practice to write a plain background color first, so that if the gradient is somehow not applied the element still has a sensible background:

.hero {
  background: #1E90FF;
  background: linear-gradient(90deg, #1E90FF 0%, #8A2BE2 100%);
}

That is exactly the form the gradient generator produces: a solid color line, then the gradient line. Browsers read both and use the last one they understand.

Readability and contrast on gradients

Text on a gradient is harder to check than text on a solid color, because the background changes along the way. Test the contrast at the lightest and darkest points behind the text, and make sure both pass. The thresholds and method are in the color contrast guide.

Some practical rules:

Banding

Large, smooth gradients between close colors can show visible bands, stripes where the color jumps by a step instead of blending evenly. It happens because each channel has only 256 values, so a gentle gradient across a wide area has to repeat each value over many pixels. A few things help: use a bigger difference between the end colors, avoid very large gradient areas with a very small color change, or overlay a faint noise texture to break up the steps.

Animating gradients

Browsers can't smoothly transition from one gradient to another with a simple transition. A reliable technique is to draw a gradient that starts and ends on the same color, make it twice as wide as the element, and animate its position by exactly one repeat, so the loop has no visible jump:

.shimmer {
  background: linear-gradient(90deg, #1E90FF, #8A2BE2, #1E90FF);
  background-size: 200% 100%;
  animation: slide 6s linear infinite;
}
@keyframes slide { to { background-position: 200% 0; } }

Respect users who prefer less motion, with a media query:

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

Performance

A gradient is cheap. The browser draws it directly, with no image to download, and it stays crisp on high-density screens and when the element is resized. Very large animated gradients can use more power on low-end devices, so animate sparingly.

Build one in the generator

The quickest way to get the code is the gradient generator. Pick a type, set the angle, edit the stops and copy the CSS. Start from a color you already have, using the hex color picker to find its code. If you'd like some guidance on which colors to combine, see how to build a color palette.

Common mistakes

  1. Forgetting the direction. The default runs top to bottom, which is often not what you want.
  2. Using the wrong angle convention. 0deg points up, not right.
  3. Blending complementary colors directly and getting a grey middle.
  4. Not testing contrast at both ends of a gradient behind text.
  5. No fallback color.
  6. Missing units in stops. #1E90FF 0 10px needs the px.
  7. A very faint gradient over a big area that shows banding.

Frequently asked questions

How do I make a gradient in CSS?

Set the background to a gradient function, for example linear-gradient(90deg, #1E90FF, #8A2BE2).

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

Linear blends along a line, radial blends outward from a point, and conic sweeps around a point.

How do I make a hard edge instead of a blend?

Put two stops at the same position, such as #1E90FF 50%, #8A2BE2 50%.

Why does my gradient look grey in the middle?

You are blending colors that are far apart on the color wheel, so the average is a desaturated grey. Add a middle color or choose closer colors.

Can I use more than two colors?

Yes, as many as you like. The generator supports up to five.

Do gradients work in every browser?

All current browsers support linear, radial and conic gradients. Newer features such as choosing the interpolation color space are more recent, so check support before relying on them.

Try it yourself

More guides