CSS Linear and Radial Gradients: Syntax, Examples, and Uses

CSS gradients let you blend colors smoothly without using image files. They are commonly used for backgrounds, overlays, buttons, illustrations, and subtle visual depth in modern layouts.

Quick answer: Use linear-gradient() for a straight color transition along a line, and radial-gradient() for a color transition that spreads outward from a center point. Both are usually used inside background or background-image.

Difficulty: Beginner

You'll understand this better if you know: basic CSS syntax, how background works, and how color values such as hex, rgb(), or named colors are written.

Browser support: Widely available since Jan 2018 (and more below)

1. What Are CSS Gradients?

CSS gradients are generated images created by the browser. Instead of loading a bitmap file, the browser calculates a smooth color blend based on the gradient function you write.

  • linear-gradient() creates a blend along a straight axis.
  • radial-gradient() creates a blend from a center point outward.
  • Gradients can use two or more colors, and each color can have a specific stop position.
  • You can apply them anywhere an image value is accepted, especially as a background.

Because gradients are computed by the browser, they scale cleanly on any screen size and do not pixelate like raster images.

2. Why CSS Gradients Matter

Gradients are useful when you want depth, emphasis, or a polished visual style without adding extra assets. They often improve performance and design flexibility at the same time.

They matter because they help you:

  • avoid extra image requests for simple visual effects
  • create responsive backgrounds that adapt to any element size
  • build reusable design patterns such as overlays, cards, and hero sections
  • control color transitions precisely with CSS values

They are not always the right choice for complex artwork, but for most interface backgrounds and accents, gradients are a strong default.

3. Basic Syntax

Both gradient functions are used as values in CSS properties like background-image. The function name is followed by commas separating colors and optional position hints.

Linear gradient syntax

A simple linear gradient uses a direction and at least two colors:

CSS
.banner {
  background-image: linear-gradient(to right, red, blue);
}

Here, the gradient starts on the left and ends on the right. The browser blends red into blue across the element.

Radial gradient syntax

A simple radial gradient starts from the center by default:

CSS
.badge {
  background-image: radial-gradient(yellow, orange, red);
}

This creates a circular or elliptical color blend, depending on the element’s shape and the options you provide.

4. Step-by-Step Examples

The best way to understand gradients is to see them in different situations. Each example below focuses on one practical pattern.

Example 1: Horizontal linear gradient

This example creates a simple left-to-right background transition.

CSS
.hero {
  background-image: linear-gradient(to right, #1d4ed8, #60a5fa);
  color: white;
}

This is a common choice for headers because it adds visual movement without distracting from text.

Example 2: Linear gradient with multiple stops

You can add more than two colors to create a more detailed blend.

CSS
.card {
  background-image: linear-gradient(135deg, #f8fafc, #e2e8f0, #cbd5e1);
}

The 135deg angle changes the direction of the blend. Extra color stops create a smoother or more stylized transition.

Example 3: Radial gradient for a spotlight effect

Radial gradients are useful when you want a soft glow or highlight in one area.

CSS
.spotlight {
  background-image: radial-gradient(circle at center, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0));
}

This pattern is often used for subtle glows, attention cues, or background decoration.

Example 4: Gradient overlay on top of an image

Gradients can be layered with other background images. The gradient acts like a tint or overlay.

CSS
.feature {
  background-image:
    linear-gradient(to bottom, rgba(0, 0, 0, 0.1), rgba(0, 0, 0, 0.7)),
    url("photo.jpg");
}

The gradient is listed first, so it paints on top of the image. This is a standard technique for making text readable over photos.

5. Practical Use Cases

Gradients show up in many real interface patterns. They are especially useful when the design needs to look polished without adding extra image assets.

  • hero section backgrounds that need a strong visual identity
  • button backgrounds with smooth hover-friendly color depth
  • image overlays that improve text contrast
  • subtle page backgrounds that look less flat than a solid color
  • progress indicators and decorative strips
  • glow, spotlight, and vignette effects in cards or banners

Use gradients when the visual effect is simple, scalable, and decorative. Use images when the design needs detailed artwork or photographic content.

6. Common Mistakes

Mistake 1: Using gradients without the background property

Beginners sometimes write a gradient as if it were a standalone property. A gradient is a value, not a property name.

Problem: The browser ignores unknown properties such as gradient, so the element appears unchanged.

CSS
.box {
  gradient: linear-gradient(red, blue);
}

Fix: Put the gradient inside background or background-image.

CSS
.box {
  background-image: linear-gradient(red, blue);
}

The corrected version works because gradients must be assigned to a property that accepts image values.

Mistake 2: Forgetting that the default direction may not match expectations

If you do not specify a direction, a linear gradient goes from top to bottom by default. That can surprise developers who expected a left-to-right blend.

Problem: The gradient is valid, but the visual result does not match the intended horizontal layout.

CSS
.header {
  background-image: linear-gradient(red, blue);
}

Fix: State the direction explicitly when layout direction matters.

CSS
.header {
  background-image: linear-gradient(to right, red, blue);
}

The corrected version works because the direction is now unambiguous.

Mistake 3: Expecting a radial gradient to stay perfectly circular

Radial gradients can appear elliptical when the element’s shape is not square. That is normal and often confusing at first.

Problem: The gradient follows the element’s box, so a wide element may produce an ellipse instead of a perfect circle.

CSS
.oval {
  background-image: radial-gradient(yellow, orange);
}

Fix: Use an explicit shape and position when you need predictable geometry.

CSS
.oval {
  background-image: radial-gradient(circle at center, yellow, orange);
}

The corrected version works because the gradient is instructed to render as a circle from a specific point.

7. Best Practices

Practice 1: Specify direction when the layout matters

Explicit directions make gradients easier to maintain and much less surprising to teammates.

CSS
.panel {
  background-image: linear-gradient(to bottom right, #0f172a, #334155);
}

This is clearer than relying on the default top-to-bottom behavior, especially in larger codebases.

Practice 2: Use transparent stops for overlays

When layering gradients over images, transparent color stops give you better contrast control than a flat opaque layer.

CSS
.card {
  background-image:
    linear-gradient(to top, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0)),
    url("team.jpg");
}

This approach keeps the image visible while improving text readability.

Practice 3: Keep color stops intentional and readable

Too many tightly packed stops can make a gradient look muddy or accidental. Use stops to create a deliberate transition.

CSS
.section {
  background-image: linear-gradient(
    90deg,
    #2563eb 0%,
    #60a5fa 50%,
    #dbeafe 100%
  );
}

Readable stop positions make the design easier to adjust later.

8. Limitations and Edge Cases

  • Gradients are rendered by the browser, but their appearance depends on the element’s size and shape.
  • Some gradients can look very different on narrow versus wide containers because the stop positions stretch with the box.
  • Gradients are not a substitute for detailed imagery or illustrations.
  • Very subtle color transitions may appear banded on some displays if the colors are too similar.
  • Because gradients are image values, they can be layered with other backgrounds, but property ordering matters.
  • When using gradients as overlays, text contrast still needs to be checked manually for accessibility.

Note: If a gradient seems to “not work,” the issue is often that another background layer or shorthand declaration is overriding it.

9. Practical Mini Project

Let’s build a simple promotional banner that uses both a linear gradient and a radial accent. This is a realistic pattern for a landing page or product highlight.

CSS
.promo {
  padding: 2rem;
  border-radius: 1rem;
  color: white;
  background-image:
    radial-gradient(circle at top right, rgba(255, 255, 255, 0.35), rgba(255, 255, 255, 0)),
    linear-gradient(135deg, #7c3aed, #2563eb);
}

.promo h2 {
  margin-top: 0;
}

This example creates a rich background: the linear gradient provides the main color transition, and the radial gradient adds a soft highlight in the corner. The result is simple, responsive, and easy to reuse.

10. Key Points

  • CSS gradients are generated images made directly in the browser.
  • linear-gradient() blends colors along a line.
  • radial-gradient() blends colors outward from a center point.
  • Gradients are usually used in background or background-image.
  • Color stops and direction control the final look.
  • Gradients scale cleanly and work well for responsive UI design.

11. Practice Exercise

Create a banner style that meets all of these requirements:

  • uses a linear gradient from dark blue to lighter blue
  • adds a radial gradient glow in the top-left corner
  • keeps the text readable with white lettering
  • uses only CSS

Expected output: A reusable banner with a layered background effect and clear text contrast.

Hint: Put the radial gradient first so it sits on top of the linear gradient.

One possible solution:

Show solution
CSS
.banner {
  padding: 3rem;
  color: white;
  background-image:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0)),
    linear-gradient(to right, #0f172a, #3b82f6);
}

Browser Support

FeatureChromeEdgeFirefoxSafariSafari iOS
Gradients
Widely available since Jan 2018
26+12+3.6+7+7+
Conic gradients
Widely available since May 2023
69+79+83+12.1+12.2+

Minimum versions with full support. Data from web-features 3.43.0 (the data behind MDN's Baseline), updated at every site build.

12. Final Summary

CSS linear and radial gradients are flexible, scalable ways to create color blends without image files. Linear gradients move along a line, while radial gradients spread from a center point, and both can be layered for richer effects.

For most interface work, the most important habits are to choose the right direction, place gradients in the correct background property, and use transparent stops when overlaying content. Those small details make the difference between a gradient that looks polished and one that feels accidental.

If you want to keep going, try combining gradients with background-size, background-position, and border-radius to build more advanced visual treatments.