Skip to content
Tutorials Center
My learning

Topics

Lesson 50 of 139 ยท Backgrounds and Effects

Gradients

Create CSS gradients with linear-gradient(), radial-gradient() and conic-gradient(), control color stops and hints, and build stripes and patterns with no image files.

A gradient is a smooth transition between two or more colors. In CSS, gradients are images that the browser draws for you. You do not download anything, they stay sharp at any size and zoom level, and you can change them with a single edit.

Because a gradient is an image, you use it anywhere an image is allowed: most often in background-image, but also in border-image, mask-image and list-style-image. In this lesson you will learn the three gradient shapes, how color stops work, and how repeating gradients turn into stripes and patterns.

Linear gradients

linear-gradient() blends colors along a straight line. The simplest version takes two colors and goes from top to bottom:

CSS
.banner {
  background-image: linear-gradient(#38bdf8, #6366f1);
}

To change the direction, add an angle or a to keyword as the first argument:

  • to right, to left, to top, to bottom (the default).
  • to top right and other corners: the gradient heads into that corner.
  • Angles: 0deg points up, 90deg points right, 180deg points down. Angles turn clockwise.

You can add as many colors as you like. Without positions, they are spread out evenly.

Linear gradient directions

There is a subtle difference between to top right and 45deg. An angle is always exactly that angle, so in a wide box 45deg does not end in the corner. A corner keyword adjusts the angle so the middle color line runs between the other two corners, which usually looks better on rectangles.

Color stops

Each color in the list is a color stop. You can give a stop a position, as a percentage or a length:

CSS
background: linear-gradient(90deg, #6366f1 0%, #ec4899 70%, #f59e0b 100%);

Here the blend from indigo to pink takes up 70% of the width, and pink to orange takes the last 30%. Some rules to know:

  • Before the first stop, the first color is solid. After the last stop, the last color is solid. So linear-gradient(red 40%, blue 60%) is solid red for 40%, blends for 20%, then solid blue.
  • Two stops at the same position make a hard edge, with no blend at all. That is the key to stripes and flags.
  • A stop can take two positions to make a solid band: #ec4899 30% 60% is pink from 30% to 60%.

Color hints

Between two stops, the blend is linear: the halfway color appears halfway between them. A color hint is a lone position between two stops that moves that midpoint:

CSS
/* the midpoint between the two colors moves to 20% */
background: linear-gradient(90deg, #1e293b, 20%, #e2e8f0);

The blend now changes quickly at first and slowly afterwards. Hints are handy for natural-looking shadows and fades.

Stops, hard edges and hints

The flag uses a small shortcut. A stop position smaller than the one before it is bumped up to match, so white 0 66.6% really starts at 33.3%. Writing 0 saves you from repeating the previous number.

Radial gradients

radial-gradient() spreads colors outward from a center point, in a circle or an ellipse:

CSS
background: radial-gradient(#fde047, #f97316);                 /* ellipse, centered */
background: radial-gradient(circle, #fde047, #f97316);         /* circle */
background: radial-gradient(circle at 30% 20%, white, #0ea5e9); /* moved center */
background: radial-gradient(circle 80px at center, #fde047 98%, transparent); /* fixed size */

The first argument describes the shape: circle or ellipse (the default), an optional size, and at followed by a position written just like background-position. The size can be a length, or one of four keywords that say where the last color stop lands:

  • farthest-corner (the default): the gradient reaches the corner furthest from the center.
  • closest-side, farthest-side, closest-corner: reach the nearest side, the furthest side or the nearest corner.

Conic gradients

conic-gradient() sweeps colors around a center point, like the hands of a clock. Stops are positioned with angles or percentages of a full turn:

CSS
background: conic-gradient(red, yellow, lime, aqua, blue, magenta, red);
background: conic-gradient(from 90deg at 50% 50%, #6366f1, #ec4899);

Conic gradients with hard stops make pie charts with no JavaScript, and adding border-radius: 50% turns the square into a circle:

Radial and conic gradients

The donut is two layers: a white circle on top of a conic pie. Change 72% to show a different score. In a real progress chart you would also show the number as text, because a gradient has no accessible meaning.

Repeating gradients

Each gradient function has a repeating version: repeating-linear-gradient(), repeating-radial-gradient() and repeating-conic-gradient(). They take the stops you give them and tile that pattern forever. The last stop's position sets the length of one repeat.

CSS
/* a 20px stripe pattern: 10px dark, 10px light */
background: repeating-linear-gradient(45deg, #334155 0 10px, #64748b 10px 20px);

Combined with hard stops, that gives you stripes, rings and checkerboards without any image file:

Patterns with repeating gradients

The checkerboard uses a different trick: an ordinary conic gradient with four quarters, sized to 26px 26px with background-size so it tiles. The "lined paper" combines two layers, a red margin line on top of repeating blue lines. You built layered backgrounds like this in Multiple Backgrounds and background-clip.

Transparent works best as a color. Older browsers blended transparent as "transparent black", which gave fades a gray tint. Modern browsers blend in premultiplied colors, so linear-gradient(red, transparent) fades cleanly. If you need to be extra safe, fade to the same color at zero alpha, like rgb(255 0 0 / 0).

Interpolation color spaces

By default, gradients blend in the sRGB color space, which can produce a muddy gray zone between opposite colors like blue and yellow. Modern browsers let you choose a different space with in:

CSS
background: linear-gradient(in oklch to right, blue, yellow);
background: linear-gradient(to right in oklab, blue, yellow);

oklab gives even, natural-looking blends, and oklch keeps colors vivid by traveling around the hue wheel. This syntax is supported in current Chrome, Edge, Firefox and Safari (all since 2023 or later). Older browsers ignore the whole declaration, so put a plain gradient before it as a fallback. You will meet these color spaces in Modern Color: oklch() and color-mix().

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which direction does linear-gradient(90deg, red, blue) run?

    0deg points up and angles turn clockwise, so 90deg points to the right.
  2. 2. How do you make a hard edge between two colors with no blending?

    When two stops share a position, the blend between them has zero length, so the color changes instantly.
  3. 3. In a repeating gradient, what sets the length of one repeat?

    The pattern from the first stop to the last stop is tiled, so 0 10px, 10px 20px repeats every 20px.
  4. 4. Which gradient function is the natural choice for a pie chart?

    Conic gradients sweep colors around a center point, so hard stops become slices.
  5. 5. What does a color hint such as linear-gradient(black, 20%, white) change?

    A hint is a position without a color. It moves the point where the two neighboring colors are mixed half and half.

Summary

  • Gradients are images drawn by the browser: crisp at any size and usable wherever an image is.
  • linear-gradient() takes a direction (to right, 45deg) and color stops.
  • Stops accept one or two positions; equal positions make hard edges, and hints move the midpoint.
  • radial-gradient() grows a circle or ellipse from a point; conic-gradient() sweeps around one.
  • Repeating gradients tile the stops; with hard edges they create stripes, rings and patterns.
  • Modern browsers can blend in oklab or oklch with the in keyword; provide a fallback.

Next: Sizing Images: object-fit and aspect-ratio โ€” fit images into any box without stretching them.