Linear gradients in CSS: angles, corners and the gradient line

How CSS linear-gradient() works: angles and to-corner keywords, how long the gradient line is, stops, hints, stripes and Tailwind v4.

Updated

A linear gradient blends colours along a straight line. In CSS it's the linear-gradient() function, and like every gradient it's an image, so it goes in background or background-image, not background-color:

.hero {
  background: linear-gradient(135deg, #0b1020, #1d4ed8 50%, #93c5fd);
}

That runs from a near-black navy in the top-left corner to light blue in the bottom-right. The direction is optional. linear-gradient(red, blue) goes from top to bottom.

Most of the syntax is easy. The direction is where it gets surprising: 45deg and to top right look the same in a square box and differ in any other shape, and the colours at the corners depend on how long the gradient line is. The demo below puts each part on the same box. Drag the slider to see the gradient line change length as the angle turns.

Angle or corner keyword, hard edge at 50%

45deg
to top right

The gradient line

45deg · line 0px for 0×0

Stops and hints

two stops
hint at 30%
hard stops

Diagonal stripes

hard edges
1px blend

The syntax

linear-gradient(
  [angle | to side-or-corner] [in colorspace],
  color-stop, [hint,] color-stop, ...
)
  • Direction: an angle such as 45deg, or a keyword: to top, to right, to bottom left and so on. The default is to bottom.
  • Colour space: in oklab, in oklch and the others, to change how the stops blend.
  • Stops: colours, each with an optional position along the gradient line. 0% is the start of the line and 100% the end.

Angles and keywords

An angle says which way the gradient runs. 0deg points up, and angles turn clockwise, so 90deg points right, 180deg points down and 270deg points left. That's the compass, not the maths convention, where 0 points right and angles turn anticlockwise.

The side keywords are names for four of those angles:

KeywordAngle
to top0deg
to right90deg
to bottom180deg
to left270deg

Any angle unit works: 0.25turn is 90deg, and so are 100grad and 1.5708rad. turn reads well when you animate a full rotation.

The corner keywords, to top right and the other three, aren't fixed angles. They depend on the box, which is the next two sections.

How long the gradient line is

The gradient line runs through the centre of the box at the angle you give. Its length is set so that the 0% and 100% colours land exactly on two opposite corners. For a box w wide and h tall at angle a:

length = |w × sin(a)| + |h × cos(a)|

At 90deg that's just the width, and at 0deg the height. At other angles the length depends on both sides, because the line has to reach the corners. A 600×200 box at 45deg has a gradient line of 600 × 0.707 + 200 × 0.707 = 566px: shorter than the width, but almost three times the height.

So the colours at the corners are set and everything else follows from them. In Chrome 154, linear-gradient(45deg, #000, #fff) on a 600×200 box is pure black at the bottom-left corner and pure white at the top-right, as the formula says. The other two corners are 25% and 75% of the way along: the top-left is rgb(63 63 63) and the bottom-right rgb(191 191 191). In a square at 45deg, those two corners would both sit at 50%.

That's why a diagonal gradient behind a hero can look right on a laptop and wrong on a phone. The angle stays the same, but the box changes shape, so the gradient line changes length and the colours shift across the box. If the colours have to stay put, give the gradient a fixed size with background-size, or pick the angle in each media query.

to top right isn't 45deg

A corner keyword picks the angle for you, from the box's shape. The spec puts it at the angle where the 50% line, the line across the box where the blend is halfway, runs exactly through the other two corners. For to top right, the 50% line runs from the top-left corner to the bottom-right corner, whatever the box's shape.

In a square, that's 45deg. In a 600×200 box, it's atan(200 / 600) = 18.43deg. You can see it with a hard edge at 50%:

linear-gradient(to top right, #000 50%, #fff 50%)  /* edge from corner to corner */
linear-gradient(45deg, #000 50%, #fff 50%)         /* edge at 45° through the centre */

In Chrome 154 on 600×200, the to top right edge starts at the top-left corner and ends at the bottom-right, crossing x = 150, 300 and 450 (to within 2px) a quarter, half and three quarters of the way down. The 45deg edge crosses the top edge at x = 200 and the bottom edge at x = 400. And linear-gradient(18.4349deg, …) draws the same edge as to top right, to within a pixel.

Which one to use:

  • A corner keyword keeps the diagonal pinned to the corners. Use it when the gradient should look the same in a wide banner and a tall card: a fade into one corner, or a two-tone split from corner to corner.
  • An angle keeps the direction the same in every box. Use it when the gradient lines up with something that has an angle of its own, such as a slanted section edge or a stripe pattern.

Stops, positions and hints

With no positions, the stops spread out evenly: three colours sit at 0%, 50% and 100%. A stop can take a length or a percentage, and two positions hold a colour across a stretch:

/* three solid bands with hard edges */
linear-gradient(90deg, #fde68a 0 30%, #be123c 30% 70%, #4c0519 70%)

When two stops share a position, the colour changes at once, with no blend. A position smaller than the one before it is treated as equal to it, so #fde68a 30%, #be123c 0 is also a hard edge at 30%.

A bare percentage between two colours is a colour hint: it moves the halfway point of that blend. In Chrome 154, linear-gradient(90deg, #000, 30%, #fff) on a 600px box reaches mid-grey at x = 179, 30% of the way along, where it would be at 300 without the hint. Radial gradients in CSS uses the same hints and hard edges on a circle.

Stripes

repeating-linear-gradient() repeats its stops along the gradient line until the box is full. Stops with two positions make crisp stripes:

background: repeating-linear-gradient(45deg, #0f172a 0 10px, #f8fafc 10px 20px);

The 20px period is measured along the gradient line, at right angles to the stripes. Across a row of pixels, the stripes are wider: at 45deg the edges come every 10 / sin 45° = 14.1px, and in Chrome 154 they land 14px apart.

Diagonal hard edges have the same problem as curved ones: on a pixel grid, a smooth slanted edge needs blended pixels. In headless Chrome 154, those stripes had no blended pixels at all on a 600px row, so the edges show stairs. Giving each edge one pixel to blend over fixes it. The same row then had 58 blended pixels, and every row had between 57 and 60:

background: repeating-linear-gradient(
  45deg,
  #0f172a 0 9px,
  #f8fafc 10px 19px,
  #0f172a 20px
);

Horizontal and vertical stripes with whole-pixel widths don't need this, because their edges sit on the pixel grid.

Colour space and transparent

Stops written as hex, rgb(), hsl() or colour names blend in sRGB by default, which takes the middle of some blends dark and grey. Stops in newer syntax, such as oklch(), blend in OKLab unless you say otherwise. In Chrome 154, linear-gradient(90deg, #ff0000, #0000ff) is rgb(127 0 127) halfway, a dark purple. The same gradient written in oklab is rgb(140 83 162) halfway, a lighter, brighter violet:

background: linear-gradient(90deg, #ff0000, #0000ff);
background: linear-gradient(90deg in oklab, #ff0000, #0000ff);

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 second declaration and keep the first. CSS gradient vs WebGL gradient compares the colour spaces, and the colour posts, starting with blue gradients in CSS, measure them hue by hue. Pastel gradients in CSS has a pink-to-mint blend that goes grey in both sRGB and OKLab, where only in oklch keeps the colour, and green gradients in CSS shows red to green turning olive halfway.

Fading to transparent is safe in current browsers. In Chrome 154, linear-gradient(90deg, red, transparent) over white is rgb(255 128 128) halfway, a clean pink with no grey. That's what makes scrims work: a dark-to-transparent fade under text on a photo. Gradient backgrounds for websites covers scrims and text contrast in detail.

Animating a linear gradient

A transition on background doesn't animate a gradient. The browser can't blend one gradient image into another, so it jumps. There are two ways to make one move.

Slide it. Make the gradient bigger than the box and animate background-position. That's the usual "animated gradient" effect, and animated gradient backgrounds in Tailwind CSS builds it with a seamless loop.

Turn it. Put the angle in a registered custom property, so the browser knows it's an angle it can interpolate:

@property --angle {
  syntax: "<angle>";
  inherits: false;
  initial-value: 0turn;
}

.spin {
  --angle: 0turn;
  background: linear-gradient(var(--angle), #1e1b4b, #6366f1, #f0abfc);
  animation: turn 12s linear infinite;
}

@keyframes turn {
  to {
    --angle: 1turn;
  }
}

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

@property is in Chrome 85, Safari 16.4 and Firefox 128. Without it, the angle jumps between keyframes instead of turning. Both ways repaint the element on every frame. For a gradient that fills a hero, the CSS vs WebGL post measured what that costs and when a transform is cheaper. The same @property trick with a conic gradient, clipped to a thin ring, makes an animated gradient border.

Linear gradients in Tailwind

Tailwind v4 names linear gradients bg-linear-*, with the same from-, via- and to- stops as before:

<div class="bg-linear-to-tr from-sky-500 to-indigo-900"></div>
<div class="bg-linear-45 from-sky-500 to-indigo-900"></div>

bg-linear-to-tr is to top right and bg-linear-45 is 45deg, so the two sections above apply as they are. Checked in Tailwind 4.3.3, the output differs from v3 in three ways:

  • Both ask for OKLab, with a fallback for older browsers. bg-linear-45 sets the direction to 45deg, then to 45deg in oklab inside @supports (background-image: linear-gradient(in lab, red, red)).
  • A modifier picks the colour space: bg-linear-45/srgb gives 45deg in srgb, and /oklch and the others work the same way.
  • An arbitrary value replaces the colour space. bg-linear-[to_top_right] compiles to plain to top right, so the colours decide. Tailwind's own colours are oklch() values, so they still blend in OKLab. Hex colours such as from-[#ff0000] blend in sRGB. To be sure of OKLab, write it inside the brackets: bg-linear-[to_top_right_in_oklab].

bg-gradient-to-* from v3 still works, but it compiles to in oklab with no fallback. Gradient text in Tailwind uses the same utilities with bg-clip-text.

A linear gradient that moves

Linear Flow is a linear blend drawn by a shader. Its angle and stops drift slowly, its blend lines bow as if the light wraps round a curved surface, and a soft light leak runs along them. It's one of the free components, and the quietest of them: good behind dense content.

Keep it moving

Install it with the shadcn CLI:

npx shadcn@latest add https://gradients.sh/r/linear-flow.json

The shader measures its blend along a gradient line too, the same way CSS does. It divides each pixel's distance along the direction by abs(dir.x) * h.x + abs(dir.y) * h.y, where h is half the frame's size. That's half the CSS line length from earlier, so the ends of the blend sit near the corners at any angle and in any box shape.

Before the shader starts, and in browsers without WebGL, the component paints a CSS version of itself. For Linear Flow with its default colours, that's one linear gradient over the background colour:

background:
  linear-gradient(
    120deg,
    #c13b3b 0%,
    #140a0e 33.3%,
    #2c55c5 66.7%,
    #eef2ff 100%
  ),
  #06070c;

It's a still of the same colours at a fixed angle, 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.