# Backgrounds & Gradients

> Add depth, lighting, and vibrant patterns to elements using pure CSS gradients.

Subject: CSS | Lesson 15 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/backgrounds-and-gradients
Previous: [Custom Properties (CSS Variables)](https://codewithanimation.in/css/custom-properties.md) | Next: [Borders, Radius & Shadows](https://codewithanimation.in/css/borders-radius-and-shadows.md)

## What it is

While `background-color` fills an element with a single flat color, CSS gradients allow you to smoothly transition between two or more colors.

Because gradients are drawn by the browser's graphics engine, they are infinitely scalable and look perfectly crisp on high-resolution Retina displays.

## Why it matters

Modern web design relies heavily on gradients to create realistic lighting, premium metallic effects, or vibrant brand backgrounds.

If you had to use JPG or PNG images for these effects, your website would load slower, and the graphics would become blurry when resized. CSS gradients solve this by being mathematically generated instantly.

## How it works

In CSS, a gradient is actually considered an image. Therefore, you must apply it using the `background-image` (or shorthand `background`) property, never `background-color`.

CSS provides three main types of gradients:

1. **Linear**: Transitions along a straight line (`linear-gradient(90deg, red, blue)`).

2. **Radial**: Transitions outward from a central point, forming a circle or ellipse (`radial-gradient(circle, red, blue)`).

3. **Conic**: Sweeps colors around a central point, like a radar sweep or color wheel (`conic-gradient(red, blue)`).

## Common mistake: The old image-slice trick

In the early days of the web, creating a gradient button meant opening Photoshop, drawing a gradient, saving a 1px wide image slice, and repeating it horizontally using `background-repeat: repeat-x`.

If the marketing team wanted to change the button color from blue to green, you had to re-export entirely new images. Today, you just change one line of code.

## Tip: Layering multiple gradients

Because gradients act as images, you can stack them on top of each other by separating them with commas.

By combining a transparent linear gradient on top of a solid color (or even another gradient), you can create complex, textured patterns purely in CSS.

## Try it: Try it

Toggle between linear, radial, and conic gradient modes, and adjust the color stops to see how the math engine draws the transitions.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/backgrounds-and-gradients

## Check yourself

### Question 1

Which CSS property must you use to apply a gradient?

- A. background-image
- B. background-gradient
- C. background-color

Correct answer: A

CSS treats gradients as dynamically generated images. Therefore, they are applied via the background-image property (or the 'background' shorthand).

- Why not B: There is no 'background-gradient' property in CSS.
- Why not C: background-color only accepts single solid colors (HEX, RGB, OKLCH, etc).

### Question 2

Which type of gradient sweeps colors around a central axis, often used to create pie charts or color wheels?

- A. conic-gradient
- B. radial-gradient
- C. circular-gradient

Correct answer: A

A conic gradient wraps colors around a center point, changing as the angle rotates from 0 to 360 degrees.

- Why not B: Radial gradients radiate outward from the center like ripples in a pond, rather than sweeping around an axis.
- Why not C: There is no 'circular-gradient' function; you would use radial-gradient(circle) instead.

### Question 3

What happens if you provide three colors to a gradient but do not specify their positions (e.g., linear-gradient(red, green, blue))?

- A. The browser spaces them out evenly at 0%, 50%, and 100%
- B. The gradient fails to render
- C. All colors are clumped at 0%

Correct answer: A

If you omit color stop percentages, the browser will automatically distribute the colors evenly. The first color starts at 0%, the last ends at 100%, and the rest fill the space between.

- Why not B: Positions (color stops) are optional in CSS gradients.
- Why not C: The browser automatically distributes unspecified color stops evenly across the gradient.

## Remember this

- Gradients are applied using `background-image`.
- Linear gradients follow a straight line.
- Radial gradients expand outward from a center point.
- Conic gradients rotate around a center axis.
- You can layer multiple gradients together by separating them with commas.
