How CSS Gradients Work
A practical guide to CSS gradients, covering linear, radial, and conic gradients, color stops, directions, transparency, repeating gradients, and common use cases.
CSS gradients let you create smooth transitions between two or more colors directly in CSS. They can be used for backgrounds, buttons, overlays, borders, cards, decorative shapes, loading effects, and many other interface elements without requiring image files.
A gradient is not a separate image that you have to create and upload. Instead, CSS calculates the colors across an area according to the gradient function you specify. This makes gradients flexible, scalable, and easy to modify in responsive layouts.
What Is a CSS Gradient?
A CSS gradient is an image generated by the browser from a mathematical description of color transitions. The most common types are linear gradients, radial gradients, and conic gradients. Each type determines how the browser distributes colors across the element.
Gradients are commonly used with the background or background-image property. For example, a simple linear gradient can be written as follows:
.box {
background: linear-gradient(to right, #2563eb, #9333ea);
}This creates a horizontal transition from blue to purple. The browser generates the intermediate colors automatically rather than requiring you to define every color between the two endpoints.
Linear Gradients
A linear gradient transitions colors along a straight line. You can control the direction of that line and define one or more color stops along it.
background: linear-gradient(to right, red, blue);In this example, the gradient starts with red on the left and transitions toward blue on the right. The direction can be changed using keywords such as to right, to left, to bottom, and to top.
.gradient {
background: linear-gradient(to bottom, #f97316, #facc15);
}You can also specify an angle instead of a direction keyword. Angles are measured clockwise, with 0deg pointing upward.
.gradient {
background: linear-gradient(135deg, #2563eb, #7c3aed);
}Angles are particularly useful when you want precise control over the visual direction of a gradient. For example, 90deg produces a gradient from left to right, while 180deg produces a gradient from top to bottom.
Color Stops
Color stops define the colors used in a gradient and their positions. If you provide only colors without explicit positions, the browser distributes the stops automatically.
background: linear-gradient(
to right,
#ef4444,
#f59e0b,
#22c55e
);You can explicitly specify positions using percentages or other supported length values. This gives you much more control over where each color appears.
background: linear-gradient(
to right,
#ef4444 0%,
#f59e0b 50%,
#22c55e 100%
);Color stops do not have to be evenly distributed. Moving a stop closer to another changes how quickly the colors transition.
background: linear-gradient(
to right,
#2563eb 0%,
#7c3aed 25%,
#ec4899 80%,
#f97316 100%
);Hard Color Transitions
Color stops can also be positioned at the same location to create an abrupt transition instead of a smooth blend. This technique is useful for stripes, separators, and simple geometric patterns.
.stripes {
background: linear-gradient(
to right,
#2563eb 0 25%,
#ffffff 25% 50%,
#2563eb 50% 75%,
#ffffff 75% 100%
);
}Because the color ranges meet at the same positions, the browser does not need to create a gradual transition between them.
Radial Gradients
A radial gradient spreads outward from a central point rather than moving along a straight line. It is useful for highlights, glows, circular backgrounds, and decorative effects.
.glow {
background: radial-gradient(circle, #60a5fa, #1e3a8a);
}By default, the browser determines the size and position of the radial gradient based on the element. You can explicitly define the shape, size, and position when more control is needed.
.highlight {
background: radial-gradient(
circle at center,
#ffffff,
#dbeafe 40%,
#2563eb 100%
);
}The at center portion positions the gradient at the center of the element. You can use other positions such as top left, bottom right, or percentage coordinates.
.spotlight {
background: radial-gradient(
circle at 20% 30%,
#ffffff,
transparent 40%
);
}Conic Gradients
A conic gradient rotates colors around a center point. Instead of progressing outward like a radial gradient, it changes color as the angle around the center changes.
.wheel {
background: conic-gradient(
#ef4444,
#f59e0b,
#22c55e,
#3b82f6,
#8b5cf6,
#ef4444
);
}Conic gradients are useful for color wheels, pie-chart-like decorations, progress indicators, gauges, and circular visual effects.
You can also control where the gradient starts and where its center is located.
.gauge {
background: conic-gradient(
from 90deg at 50% 50%,
#22c55e 0deg 240deg,
#e5e7eb 240deg 360deg
);
}Repeating Gradients
CSS also provides repeating versions of linear, radial, and conic gradients. A repeating gradient repeats the defined pattern across the available area.
.pattern {
background: repeating-linear-gradient(
45deg,
#e5e7eb 0,
#e5e7eb 10px,
#ffffff 10px,
#ffffff 20px
);
}Repeating gradients are useful for stripes, patterns, backgrounds, and decorative separators. Unlike a normal gradient, the browser continues the pattern after the final color stop.
.dots {
background: repeating-radial-gradient(
circle,
#2563eb 0,
#2563eb 4px,
transparent 4px,
transparent 12px
);
}Transparent Gradients
Gradients can use transparent colors to create overlays and fading effects. This is especially useful when a gradient needs to blend into an existing image or background.
.image-overlay {
background: linear-gradient(
to bottom,
rgba(0, 0, 0, 0.7),
transparent
);
}Modern CSS color functions can also be used when you want more explicit control over alpha transparency.
.fade {
background: linear-gradient(
to right,
rgb(37 99 235 / 0.8),
rgb(37 99 235 / 0)
);
}Multiple Gradients
Multiple gradients can be layered by separating them with commas. The first background layer is rendered above the layers that follow it.
.card {
background:
radial-gradient(circle at top right, #60a5fa, transparent 35%),
linear-gradient(135deg, #111827, #1f2937);
}Layering gradients makes it possible to create complex backgrounds without image assets. You can combine a large base gradient with smaller radial highlights, shadows, or decorative shapes.
Gradients on Text
A gradient can also be applied to text by using the background as the visual source and clipping it to the text shape.
.gradient-text {
background: linear-gradient(90deg, #2563eb, #9333ea);
background-clip: text;
-webkit-background-clip: text;
color: transparent;
}This technique is commonly used for headings and decorative labels. The text itself remains normal HTML text, while the gradient is displayed through the clipped background.
Gradients as Borders
A gradient cannot be assigned directly to border-color, but gradients can be used to create gradient borders with techniques such as background layers, masking, or pseudo-elements.
.card {
border: 2px solid transparent;
background:
linear-gradient(#111827, #111827) padding-box,
linear-gradient(135deg, #2563eb, #ec4899) border-box;
border-radius: 12px;
}The transparent border reserves space while the two background layers create the appearance of a gradient border. This is a useful pattern for cards, buttons, and highlighted components.
Gradient Size and Position
Gradients are background images, so they can be controlled with background-size and background-position. This is useful when a gradient should occupy only part of an element or when you want to animate or move it.
.hero {
background-image: linear-gradient(90deg, #2563eb, #9333ea);
background-size: 200% 100%;
background-position: left;
}Increasing background-size can make the visible transition appear larger or allow the background to move during an animation.
Animating CSS Gradients
Gradients can participate in animations, but animating a gradient directly is not always the simplest approach. A common technique is to create an oversized gradient and animate background-position.
.animated {
background: linear-gradient(
90deg,
#2563eb,
#9333ea,
#ec4899,
#2563eb
);
background-size: 300% 100%;
animation: move-gradient 6s linear infinite;
}
@keyframes move-gradient {
from {
background-position: 0% 50%;
}
to {
background-position: 100% 50%;
}
}This approach is commonly used for animated buttons, loading states, decorative headers, and subtle visual effects. Animations should remain restrained when they are not essential to the interface.
Gradients and Accessibility
A visually attractive gradient can still create accessibility problems. Text placed over a gradient may have good contrast against one part of the background but poor contrast against another.
When using gradients behind text, evaluate the contrast across the entire area where the text can appear. An overlay, darker gradient stop, text shadow, or different text placement may be necessary to keep the content readable.
Choosing the Right Gradient Type
| Gradient Type | How It Works | Common Uses |
|---|---|---|
| Linear | Transitions colors along a straight line | Buttons, backgrounds, overlays, separators |
| Radial | Transitions colors outward from a center point | Glows, highlights, spotlights, decorative backgrounds |
| Conic | Transitions colors around a center point by angle | Color wheels, gauges, circular indicators |
| Repeating linear | Repeats a linear gradient pattern | Stripes, patterns, decorative backgrounds |
| Repeating radial | Repeats a radial gradient pattern | Dots, rings, circular patterns |
| Repeating conic | Repeats an angular gradient pattern | Circular patterns and decorative effects |
Common CSS Gradient Mistakes
One common mistake is using too many colors. A gradient with many unrelated colors can become visually noisy and make an interface harder to read. In most UI designs, two or three carefully selected colors are enough.
Another mistake is choosing colors that have poor contrast with foreground content. Gradients should be evaluated as part of the complete component rather than as isolated color combinations.
Overusing gradients can also weaken visual hierarchy. If every button, card, heading, and background uses a different gradient, important elements may no longer stand out.
Finally, avoid relying on gradients for information that users need to understand. Decorative color transitions are useful for visual styling, but important states should also be communicated through text, icons, structure, or other accessible signals.
Practical Gradient Examples
A simple modern button can use a linear gradient with a small number of color stops:
.button {
background: linear-gradient(135deg, #2563eb, #7c3aed);
color: white;
border: 0;
border-radius: 8px;
padding: 10px 18px;
}A subtle page background can combine a radial highlight with a neutral base:
.page {
background:
radial-gradient(
circle at top,
rgb(59 130 246 / 0.12),
transparent 40%
),
#f8fafc;
}An image can use a gradient overlay to improve text readability:
.hero {
background:
linear-gradient(
to bottom,
transparent 30%,
rgb(0 0 0 / 0.8)
),
url("/hero.jpg") center / cover;
}CSS Gradients vs Images
CSS gradients are ideal when the visual consists primarily of color transitions or geometric patterns. They scale with the element and can be modified directly through CSS variables, media queries, and component styles.
Images are more appropriate when the design requires photographs, detailed illustrations, textures, or complex artwork that would be impractical to reproduce with CSS.
Using a CSS gradient instead of an image can simplify assets and make a design easier to customize. However, CSS is not automatically better for every visual. The appropriate choice depends on the complexity and purpose of the design.
Using CSS Variables with Gradients
CSS custom properties make gradients easier to maintain because the colors can be defined once and reused throughout a component or theme.
:root {
--gradient-start: #2563eb;
--gradient-end: #9333ea;
}
.button {
background: linear-gradient(
135deg,
var(--gradient-start),
var(--gradient-end)
);
}This approach is especially useful for design systems and applications with light and dark themes. The same gradient definition can use different variables depending on the active theme.
How Browsers Render Gradients
The browser treats a gradient as a generated image. It calculates the color at different points based on the gradient type, direction, color stops, and other parameters, then renders the resulting image as a background or other supported image value.
Because the gradient is generated from CSS rather than downloaded as a separate bitmap, it can adapt naturally when the dimensions of the element change. This is one reason gradients work well in responsive interfaces.
Best Practices for CSS Gradients
- Use two or three well-chosen colors for most interface gradients.
- Choose the gradient direction to support the visual hierarchy of the component.
- Use explicit color stops when precise positioning is important.
- Check text contrast across the entire gradient rather than only at its endpoints.
- Use radial gradients for localized highlights and glows.
- Use conic gradients for angular and circular visualizations.
- Use repeating gradients when creating regular patterns or stripes.
- Layer multiple gradients when a single gradient cannot produce the desired effect.
- Prefer CSS gradients over image assets when the design is primarily a color transition.
- Keep decorative gradients subtle when they compete with important interface content.
What is a CSS gradient?
A CSS gradient is a browser-generated image that creates a smooth or repeating transition between colors. It is commonly used as a background without requiring a separate image file.
What are the main types of CSS gradients?
The main types are linear gradients, radial gradients, and conic gradients. CSS also provides repeating versions of these gradient types.
What is a linear gradient?
A linear gradient transitions between colors along a straight line. Its direction can be controlled with keywords such as to right or with an angle such as 135deg.
What is a radial gradient?
A radial gradient transitions colors outward from a center point. It is commonly used for highlights, glows, spotlights, and circular effects.
What is a conic gradient?
A conic gradient transitions between colors around a center point according to angle. It is useful for color wheels, gauges, circular indicators, and decorative patterns.
What are color stops in CSS gradients?
Color stops define the colors used by a gradient and optionally specify where those colors should appear. Explicit stop positions provide more precise control over the transition.
Can CSS gradients be transparent?
Yes. Gradients can use transparent or partially transparent colors to create fades, overlays, and effects that blend with the content underneath.
Can CSS gradients be animated?
Yes. A common technique is to create an oversized gradient and animate its background-position. This can produce smooth moving gradient effects without replacing the gradient itself.
Conclusion
CSS gradients provide a powerful way to create flexible backgrounds, overlays, highlights, patterns, and decorative effects without external image files. Linear gradients work along a straight direction, radial gradients spread from a center point, and conic gradients transition around a center by angle.
Understanding color stops, positions, transparency, repetition, layering, and gradient direction gives you enough control to build most common gradient effects directly in CSS. For production interfaces, the most important considerations are not only visual quality but also readability, accessibility, maintainability, and appropriate use of motion.