On this page
CSS Gradients (Linear, Radial, Conic)
What Are CSS Gradients?
CSS gradients are images generated by the browser that smoothly transition between two or more colors. They are used in background-image (and other places that accept an image), need no file download, and scale perfectly. Our focus keyword is CSS gradients, and this lesson covers linear-gradient, radial-gradient and conic-gradient, plus trending topics such as CSS gradient text, gradient borders, repeating gradients, CSS pie charts, mesh-style gradients, and interpolating in OKLCH for smoother colors.
1. linear-gradient()
A linear gradient moves along a straight line. Syntax: linear-gradient(direction, color-stop, color-stop, ...).
- Default direction is
to bottom(top to bottom). - Use keywords:
to right,to bottom right. - Or angles:
45deg,90deg(to right),180deg(to bottom),0deg(to top).
background: linear-gradient(to right, #22c55e, #3b82f6);
background: linear-gradient(135deg, #f59e0b 0%, #ef4444 100%);2. Color Stops and Hints
- A color stop has a color and an optional position:
red 20%. - Two stops at the same position make a hard edge (no blending):
red 50%, blue 50%. - Two positions on one color:
red 20% 40%makes a solid band. - A color hint is a lone percentage between stops (
red, 30%, blue) that moves the midpoint of the transition. - Use
transparentfor fades. Modern browsers handle transparent correctly, blending in premultiplied alpha.
3. radial-gradient()
A radial gradient radiates from a center point. Syntax: radial-gradient(shape size at position, stops).
- Shape:
circleorellipse(default). - Size:
closest-side,farthest-corner(default),closest-corner,farthest-side, or explicit lengths. - Position:
at center,at top left,at 30% 70%.
background: radial-gradient(circle at 30% 30%, #fde68a, #f59e0b 60%, #b45309);4. conic-gradient()
A conic gradient rotates colors around a center point, like a color wheel or the sweep of a radar. Syntax: conic-gradient(from angle at position, stops). Stops use angles or percentages of a full circle.
- Pie charts: hard stops such as
#3b82f6 0 40%, #f59e0b 0 70%, #22c55e 0. - Color wheels:
conic-gradient(red, yellow, lime, aqua, blue, magenta, red). - Checkerboards:
repeating-conic-gradientwith a small background-size.
5. Repeating Gradients
Use repeating-linear-gradient(), repeating-radial-gradient() and repeating-conic-gradient() to tile the color stops. They are ideal for stripes, rings and loading indicators.
6. Interpolation Color Space
By default gradients blend in sRGB, which can look gray or muddy in the middle (for example blue to yellow). You can choose the color space: linear-gradient(in oklch, #2563eb, #facc15) or in oklab. Add hue interpolation like in oklch longer hue for rainbow effects. This is a trending technique for vibrant, smooth gradients.
7. Popular Gradient Techniques
Gradient text
Use a gradient background with background-clip: text and a transparent text color.
Gradient borders
Layer two backgrounds: a solid fill clipped to padding-box and a gradient clipped to border-box, with a transparent border. Or use border-image (which does not work with rounded corners).
Overlays for text readability
linear-gradient(to top, rgb(0 0 0 / 70%), transparent) over a photo.
Mesh-like backgrounds
Stack several radial-gradient() layers with transparent ends over a base color for a soft, modern look.
Skeleton loaders
Animate background-position of a wide linear gradient to create a shimmer effect.
8. Performance and Accessibility
- Gradients are cheap to render, but very large animated gradients can cost repaint time. Animate
transformorbackground-positionrather than changing the stops. - Check text contrast against the lightest and darkest parts of the gradient.
- Provide a solid
background-colorfallback for old browsers.
Best Practices
- Use two to three colors for clean gradients. More stops need more care.
- Use OKLCH interpolation to avoid dull midpoints.
- Store gradients in custom properties:
--grad: linear-gradient(...). - Use hard stops for charts and stripes, soft stops for backgrounds.
Common Mistakes
- Forgetting that
0degpoints up, not right. - Using gradients with
background-coloronly and expectingbackground-color: linear-gradient()to work. Gradients belong inbackground-imageor thebackgroundshorthand. - Placing a color hint next to another hint, which is invalid.
- Expecting
border-imagewith a gradient to followborder-radius.
Summary
Linear gradients follow a line, radial gradients spread from a point and conic gradients sweep around a point. Add repeating variants, hard stops and OKLCH interpolation and you can build backgrounds, charts, patterns and loaders with pure CSS. Practice with the live examples below.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Using conic-gradient and border-radius: 50%, draw a pie chart 200px wide and tall. Segments: 50% blue (#3b82f6), 30% orange (#f59e0b), 20% green (#22c55e). Use hard stops.
Press Run to execute.
Show expected output
A circle divided into 50%, 30% and 20% colored segments.This is a self-check — compare your result with the expected output above.
Make the .bar element look like a striped progress bar using repeating-linear-gradient at 45deg with two shades of green. Keep height 24px and rounded corners.
Press Run to execute.
Show expected output
A 24px tall bar with diagonal green stripes and rounded corners.This is a self-check — compare your result with the expected output above.
Was this page helpful?