Linear Gradients: Directions, Color Stops, Hints and Repeating Gradients
linear-gradient() in depth: directions as keywords and angles, how the gradient line reaches corners, color stop positions, hard stops and stripes, color hints, fading to transparent, repeating-linear-gradient() patterns and everyday uses.
kvi5hnuCode Sprout team member9 min readOn this page
A linear gradient is an image that blends colors along a straight line. The browser generates it at any size, so it costs no download and stays sharp at any zoom level. Gradients are images, not colors: they go anywhere an image does, mostly background-image, but also border-image, mask-image and list-style-image. They're used for subtle depth on buttons, overlays that keep text readable over photos, stripes and dividers, and fades at the edge of scrolling content.
.panel { background-image: linear-gradient(#fff, #f4efe6); } /* top to bottom, white to cream */
Direction
The optional first argument sets the direction of the gradient line, along which the colors change. Without it, the gradient runs top to bottom.

- Keywords:
to top,to right,to bottom(the default),to left, and corners such asto top right. - Angles:
0degpoints up, and angles go clockwise:90degisto right,180degisto bottom. Any angle unit works, such as0.25turn(article 29).
Corners and angles differ on non-square boxes, though not where you might expect. Both 45deg and to top right end in the corners: the bottom-left corner gets the first color and the top-right the last. The difference is the angle of the color bands. With 45deg, the bands always run at 45°, so on a wide box the top-left and bottom-right corners get different colors. to top right tilts the line to fit the box, so the middle color runs exactly from the top-left corner to the bottom-right corner.
The gradient line's start and end points are where lines through the two corners, perpendicular to it, cross it. So with any angle, the two corners the line runs between get exactly the first and last colors, and nothing is cut off.
Color stops
Each color is a color stop. Without positions, they're spread evenly: with three colors, at 0%, 50% and 100%. A position, a percentage of the gradient line or a length, puts a stop exactly where you want:
linear-gradient(90deg, #8a3b12, #f4c95d 30%, #2f7d4f) /* the yellow is at 30%, not 50% */
- Before the first stop, the gradient is solid in the first color; after the last stop, in the last color.
- Stops without positions are spread evenly between the positioned stops around them.
- A stop positioned before an earlier one is moved to the earlier stop's position. That's what makes hard edges, below.
Hard stops and stripes
Two stops at the same position switch colors instantly, with no blend. A color can also take two positions, a start and an end, making a solid band:
/* three solid bands */
linear-gradient(90deg, #8a3b12 0 33%, #f4efe6 33% 67%, #2f7d4f 67% 100%)
That's how gradients draw flags, progress bars, split backgrounds and stripes without any images.
Color hints
A bare position between two stops is a color hint: it moves the midpoint of the blend, where the two colors are mixed half and half. By default the midpoint is halfway between the stops:
linear-gradient(90deg, #8a3b12, 20%, #f4c95d) /* the blend reaches its midpoint at 20%, then eases out */
The blend isn't linear on either side of a hint, but curved so that it stays smooth. A hint is useful for gradients that should fade quickly at one end, like shadows and overlays.

Read each bar against the ruler: the color is solid before the first positioned stop and after the last one, and the out-of-order stop in the fifth bar is pulled up to 60%, which leaves a hard edge.
body { font: 0.75rem system-ui, sans-serif; margin: 1rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(9rem, 1fr)); gap: 0.5rem; }
.g { block-size: 4.5rem; border-radius: 6px; display: grid; place-items: end start; padding: 0.25rem; color: #2b2118; font-weight: 700; }
.g span { background: rgb(255 255 255 / 0.85); padding: 0 0.25rem; border-radius: 3px; }
.a { background: linear-gradient(#f4c95d, #8a3b12); }
.b { background: linear-gradient(to right, #f4c95d, #8a3b12); }
.c { background: linear-gradient(to top right, #f4c95d, #f4efe6, #8a3b12); }
.d { background: linear-gradient(45deg, #f4c95d, #f4efe6, #8a3b12); }
.e { background: linear-gradient(to right, #f4c95d, #f4efe6 25%, #8a3b12); }
.f { background: linear-gradient(to right, #f4c95d 0 33%, #f4efe6 33% 67%, #2f7d4f 67%); }
.h { background: linear-gradient(to right, #8a3b12, 15%, #f4efe6); }
.i { background: linear-gradient(to right, #2f7d4f, transparent), repeating-linear-gradient(45deg, #ddd 0 6px, #fff 6px 12px); }
Transparency
Fading to transparent works as you'd hope: gradients blend in premultiplied alpha, so linear-gradient(red, transparent) fades red out, with no gray in the middle. (Old browsers blended to transparent, which is transparent black, and produced a grayish band.)
Which color space
By default, gradients between colors written in the older sRGB syntaxes, such as hex, rgb(), hsl() and named colors, blend in sRGB, for compatibility. Gradients using newer syntaxes like oklch() blend in Oklab. Measured in Chrome, linear-gradient(90deg, red, blue) has a dull rgb(127 0 127) in the middle, while the same colors in oklch() give a brighter rgb(140 83 162).
You can choose the color space explicitly: linear-gradient(90deg in oklch, red, blue). Article 51 covers the options and when each looks best.
Repeating linear gradients
repeating-linear-gradient() takes the same arguments, but repeats the stops endlessly in both directions along the line. The pattern's length is the distance from the first stop's position to the last's, so give the last stop a length:
.stripes {
background: repeating-linear-gradient(
45deg,
#f4efe6 0 10px,
#e8dcc8 10px 20px /* the pattern is 20px long */
);
}
With percentage positions, the pattern is a percentage of the gradient line, which changes with the box's size; lengths keep stripes a fixed width. Some uses:
/* lined paper: a 1px line every 1.5rem */
.notes { background: repeating-linear-gradient(transparent 0 calc(1.5rem - 1px), #d8cfc0 calc(1.5rem - 1px) 1.5rem); }
/* a "disabled" or "in progress" stripe */
.pending { background: repeating-linear-gradient(-45deg, #f4c95d 0 8px, #f9dd8f 8px 16px); }
If the pattern's length is zero, or so small that the screen can't show it (a fraction of a device pixel), the specification tells browsers to paint a solid color, the gradient's average color, instead of stripes. Chrome paints a solid color too, but tested in Chrome 157, repeating-linear-gradient(to right, red 0 0.1px, blue 0.1px 0.2px) came out solid blue, the last stop's color, not purple. Either way, a stripe pattern that's too fine disappears, so keep patterns at least a few pixels long.
You can get similar patterns from a normal gradient with background-size and background-repeat (article 48), which is sometimes easier to line up.

The two pairs in the second row show the difference between length and percentage stops, and the last panel shows the background-size alternative.
Practical gradients
body { font: 0.95rem/1.4 system-ui, sans-serif; margin: 1rem; display: grid; gap: 1rem; justify-items: start; }
.btn {
font: inherit; color: white; border: 0; border-radius: 8px; padding: 0.6rem 1.1rem;
background: linear-gradient(#3a9461, #2f7d4f 60%, #286b43); /* subtle depth */
}
.fade {
inline-size: 100%; block-size: 2px; border: 0; margin: 0;
background: linear-gradient(to right, transparent, #8a3b12, transparent);
}
.photo {
inline-size: 100%; block-size: 8rem; border-radius: 10px; display: grid; align-items: end;
background:
linear-gradient(to top, rgb(0 0 0 / 0.7), 40%, rgb(0 0 0 / 0)), /* overlay with a hint */
linear-gradient(#7fb4d1 0 55%, #2f7d4f 55%); /* stand-in for a photo */
}
.photo p { color: white; margin: 0.75rem; }
.progress {
inline-size: 100%; block-size: 1.5rem; border-radius: 99px; overflow: hidden;
background: linear-gradient(to right, #2f7d4f var(--value), #e8dcc8 var(--value)); /* hard stop at the value */
display: grid; place-items: center; font-size: 0.8rem; font-weight: 700;
}
- Depth: a gradient a few shades either side of a button's color, with a slight dark band at the bottom.
- Faded dividers: transparent at both ends.
- Overlays: dark at the edge where the text sits, transparent elsewhere, with a hint to keep most of the image clear. Check the text's contrast against the lightest part of the image under it.
- Progress and meters: a hard stop at a custom property's value, so JavaScript only sets
--value. For real progress, use a<progress>orrole="progressbar"element so screen readers get the value.
Gradients can't be transitioned directly, since they're images. Registering a custom property as a <color> or <percentage> with @property (article 28) and using it inside the gradient makes the gradient animatable.
What's current
- Two-position color stops (
#8a3b12 0 33%) and color hints work in all browsers. - Choosing the interpolation color space,
linear-gradient(in oklch, ...), is Baseline since 2024 (Chrome 111, Safari 16.2, Firefox 127). - Gradients blend in premultiplied alpha in every current browser, so fades to
transparentstay clean.
Common questions
Why is my gradient banded or striped instead of smooth?
Very subtle gradients over large areas can show visible steps on 8-bit displays. Add a little contrast, use a different color space such as in oklab, or layer a faint noise texture.
Why don't my diagonal bands run corner to corner?
With an angle such as 45deg, the bands keep that angle whatever the box's shape, so on a wide box they don't line up with the diagonal. Use to top right to make the middle color run from corner to corner.
How do I make stripes?
Use hard stops: two colors at the same position. For stripes that repeat, use repeating-linear-gradient() with lengths, ending the last stop at the pattern's length.
Why does my background-color change not affect the gradient?
The gradient is an image painted on top of the color. If the gradient is opaque, the color only shows while the gradient isn't drawn.
Can I transition between two gradients?
Not directly. Animate a registered custom property used in the gradient, or fade between two layers with opacity.
Summary
linear-gradient(direction, stops...)makes an image; the default direction isto bottom.- Angles start at
0deg(up) and go clockwise;to top rightand other corners tilt the line to fit the box, so the middle color runs corner to corner. - Stops can have one or two positions; equal positions make hard stops and stripes.
- A bare position between stops is a color hint, moving the blend's midpoint.
- Fades to
transparentare clean. Legacy colors blend in sRGB, modern ones in Oklab, or choose within <space>. repeating-linear-gradient()repeats the stops; give the last stop a length to set the pattern size.
Practice questions
Write a gradient that's solid rust (#8a3b12) for the left 30% and solid cream (#f4efe6) for the rest, with no blend.
Answer
background: linear-gradient(to right, #8a3b12 30%, #f4efe6 30%);
What's the direction of this gradient, and where is the yellow stop?
.box { background: linear-gradient(270deg, red, yellow, blue 80%); }
Answer
It runs right to left (270deg is to left). Red is at 0% (the right edge) and blue at 80%, so the yellow is halfway between them, at 40%. After 80%, the gradient is solid blue.
Write diagonal stripes, 8px of #f4c95d then 8px of white, repeating.
Answer
background: repeating-linear-gradient(45deg, #f4c95d 0 8px, white 8px 16px);
A gradient from red to blue looks dull purple in the middle. Why, and how can you brighten it?
Answer
Named colors are legacy sRGB colors, so the gradient blends in sRGB, which darkens the midpoint. Blend in a perceptual space instead: linear-gradient(in oklch, red, blue) or in oklab.
Previous: Multiple Backgrounds and the background Shorthand
Next: Radial and Conic Gradients, and Choosing the Interpolation Color Space

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.