CSS linear-gradient: directions and color stops

CSS linear-gradient() creates an image whose colors change along a line. With background-image: linear-gradient(to right, yellow, red), yellow on the left blends into red on the right. Use an image property to apply the gradient. background-color accepts a single color value.

Choose the gradient direction

.box {
  background-image: linear-gradient(to right, yellow, red);
}
Direction From first color to last
to top or 0deg Bottom to top
to right or 90deg Left to right
to bottom or 180deg Top to bottom (default)
to left or 270deg Right to left

If you omit the direction, the first color is at the top and the last is at the bottom. Angles increase clockwise from 0deg, which points up. A corner keyword such as to top right depends on the box’s aspect ratio. Use 45deg if you need a fixed direction. See CSS Images · Linear Gradients.

Run four gradient examples

Save the following as linear-gradient.html and open it. Four swatches compare a horizontal gradient, a vertical one, a controlled transition, and a sharp join. No image files are required.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Comparing linear gradients</title>
  <style>
    body { margin: 20px; font: 16px/1.6 system-ui, sans-serif; color: #203348; }
    h1 { font-size: 24px; }
    main { max-width: 600px; display: grid; gap: 16px; }
    figure { margin: 0; }
    figcaption { margin-bottom: 6px; }
    .swatch { height: 85px; border: 1px solid #6e8295; border-radius: 6px; }
    .basic { background-image: linear-gradient(to right, yellow, red); }
    .vertical { background-image: linear-gradient(to bottom, yellow, red); }
    .stops { background-image: linear-gradient(to right, yellow 50%, red 60%, purple 100%); }
    .hard { background-image: linear-gradient(to right, #1768a5 50%, #11958e 50%); }
  </style>
</head>
<body>
<h1>Comparing linear gradients</h1>
<main>
  <figure><figcaption>Rightward: yellow to red</figcaption>
    <div class="swatch basic" role="img" aria-label="A background changing from yellow at left to red at right"></div>
  </figure>
  <figure><figcaption>Downward: yellow to red</figcaption>
    <div class="swatch vertical" role="img" aria-label="A background changing from yellow at top to red at bottom"></div>
  </figure>
  <figure><figcaption>Color stops at 50%, 60%, and 100%</figcaption>
    <div class="swatch stops" role="img" aria-label="Yellow holds halfway across, then blends through red to purple"></div>
  </figure>
  <figure><figcaption>Two stops at the same position</figcaption>
    <div class="swatch hard" role="img" aria-label="Blue and teal meet at a sharp boundary in the center"></div>
  </figure>
</main>
</body>
</html>

The first two swatches use the same colors along different axes. The third stays yellow halfway across, then passes through red to purple. The fourth changes from blue to teal at the center.

Control solid bands and transitions

In yellow 50%, red 60%, purple 100%, each percentage marks where the gradient line reaches that color. Yellow fills 0–50%, the yellow-to-red transition spans 50–60%, and red blends into purple from 60–100%. Change only red 60% to red 80%. The first blend gets longer and the second gets shorter. Restore 60% afterward.

If every position is omitted, the first and last colors go at 0% and 100%, and the remaining stops are evenly spaced. In the last swatch, the two 50% stops leave no distance for a blend. Moving the second stop to 40% still gives a join at 50%, because it cannot precede the first. Move both stops to change the split. See CSS Images · Color Stop Fixup.

Layer a gradient and troubleshoot a missing background

To add a translucent overlay to a photo, list the gradient with alpha colors first in background-image and the photo’s url(...) second. The first layer is painted on top. If the gradient is invisible on an empty element, check its height or padding. A later background shorthand can reset the image, so inspect the computed background-image too. Check text readability over both light and dark areas of the result. See CSS Backgrounds · Layering.