CSS linear gradient: how it works
A linear-gradient is a background image that blends colors along a straight line. You give it a direction (an angle) and two or more color stops, each with an optional position, and the browser draws the blend at any size, with no image file. It goes in the background or background-image property, and also works in border-image and mask-image. This page opens the generator on the "sunset" preset: change the angle and the stops, watch the preview and copy the CSS.
A real result
background: linear-gradient(135deg, #ff7e5f 0%, #feb47b 100%);The same gradient as a Tailwind arbitrary value:
bg-[linear-gradient(135deg,_#ff7e5f_0%,_#feb47b_100%)]In Tailwind the spaces become underscores, because a class name cannot contain them.
The angle decides the direction
In CSS 0deg points up and angles grow clockwise. The same two colors at six angles:
| Angle | CSS | Direction |
|---|---|---|
| 0deg | linear-gradient(0deg, #ff7e5f 0%, #feb47b 100%) | towards the top |
| 45deg | linear-gradient(45deg, #ff7e5f 0%, #feb47b 100%) | towards the top right |
| 90deg | linear-gradient(90deg, #ff7e5f 0%, #feb47b 100%) | towards the right (the same as "to right") |
| 135deg | linear-gradient(135deg, #ff7e5f 0%, #feb47b 100%) | towards the bottom right |
| 180deg | linear-gradient(180deg, #ff7e5f 0%, #feb47b 100%) | towards the bottom (the CSS default) |
| 270deg | linear-gradient(270deg, #ff7e5f 0%, #feb47b 100%) | towards the left |
More than two colors
Each stop is a color and a position. The "ocean" preset spreads five colors evenly, 25% apart:
background: linear-gradient(45deg, #051937 0%, #004d7a 25%, #008793 50%, #00bf72 75%, #a8eb12 100%);Two stops at the same position make a hard edge instead of a blend, which is how stripes and flags are drawn:
background: linear-gradient(90deg, #3b82f6 0%, #3b82f6 50%, #ffb224 50%, #ffb224 100%);Transparency
Each stop has its own opacity. Fading from transparent to 70% black is the usual way to make white text readable over a photo; the opacity is written into the color:
background: linear-gradient(180deg, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.7) 100%);Common mistakes
- Expecting a smooth gradient between two similar colors on a large area: the 256 levels of each channel show as bands. Add a third color or a subtle noise image to hide them.
- Using "transparent" as a color: in old browsers it blends towards transparent black and gets a gray tint. Use the same color with 0 opacity, as the generator does.
- Forgetting a fallback: a gradient only paints where the element has size. Give it a height, or the background shows nothing.
How to use the generator
- Pick a preset or paste an existing linear-gradient(...) to edit it.
- Set the angle and add, move or remove color stops; each stop has a color, position and opacity.
- Copy the CSS or the Tailwind class.
Frequently asked questions
How do I make a diagonal gradient in CSS?
Use an angle between the axes: 45deg goes to the top right, 135deg to the bottom right (the "sunset" preset).
What is the difference between 90deg and "to right"?
None for the horizontal case. For corners ("to bottom right") the angle adapts to the box shape so that the 50% line passes through the other corners, while 135deg is always exactly 135deg.
Can I animate a gradient?
Not by transitioning the gradient itself in most browsers. Animate background-position over a larger background-size, or an opacity layer, or register the colors with @property.