The Art of Color: CSS Gradients

Discover the functions that create beautiful, smooth color transitions, from simple lines to complex repeating patterns.

Lesson ProgressStep 1 of 10
0 EXP

Welcome! Let's explore CSS gradients, a way to create smooth transitions between two or more colors.

/* Welcome to the CSS Gradient simulator */

The `linear-gradient()` Function

The `linear-gradient()` function creates a background image consisting of a smooth color transition along a straight line.

The most basic syntax involves just two colors. By default, the gradient runs from top to bottom.

background: linear-gradient(red, blue);

This creates a gradient that starts red at the top and smoothly transitions to blue at the bottom.

System Check

If you write `linear-gradient(blue, green);`, what will be the default direction?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Linear Master

Construct a well-formed linear gradient with a direction and colors.

🏗️
Radial Whiz

Correctly structure a radial gradient with shape and position.

✍️
Conic Expert

Prove your mastery of the conic-gradient syntax.

Mission: Create a Diagonal Gradient

Set the `background` property to be a `linear-gradient` at `45deg`, starting from `blue` and ending at `green`. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> Awaiting input...

Challenge: Order the Gradient

The syntax for gradients is precise. Drag the parts into the correct order to build a valid `radial-gradient`.

at top left,
radial-gradient(
blue)
circle
red,

Challenge: Complete the Syntax

Fill in the missing parts to create a conic gradient that starts from a 45-degree angle.

background:(,);

Consult A.D.A.

Community Holo-Net

Peer Project Review

Submit your "Diagonal Gradient" project for feedback from other Net-Runners.

Beyond Solid Colors: The Art & Science of CSS Gradients

In modern web design, solid colors are just the beginning. To create depth, visual interest, and a sense of realism, designers turn to gradients. A **gradient** is a smooth transition between two or more colors. CSS provides powerful, native functions to create these effects directly in your stylesheet, offering better performance and more flexibility than traditional image-based backgrounds.

Chapter 1: The `linear-gradient()` Deep Dive

The most common gradient is the `linear-gradient()`. It creates a color transition along a straight line, known as the "gradient line."

Syntax and Direction

The syntax is `background: linear-gradient(direction, color1, color2, ...);`. The direction is the most flexible part:

  • No Direction (Default): If omitted, the gradient runs from `to bottom`.
  • Side/Corner Keywords: You can use keywords like to right, to top left, or to bottom right.
  • Angles: For precise control, use angles. 0deg is `to top`, 90deg is `to right`, and 45deg is a diagonal line to the top-right corner.

Color Stops and Hard Stops

You aren't limited to two colors. You can add as many as you like. By default, they are spaced evenly. To control their position, add a percentage after the color.

/* Red starts at 0%, transitions to yellow at 50%, then to blue at 100% */
background: linear-gradient(to right, red, yellow 50%, blue);

To create a "hard stop" or a solid band of color, make two color stops share the same position. This is great for creating stripes.

/* A solid red stripe from 0% to 30%, then a hard switch to blue */
background: linear-gradient(to right, red 30%, blue 30%);

Chapter 2: The `radial-gradient()`

A `radial-gradient()` radiates colors from a central point. By default, it creates an ellipse, but its shape, size, and position are all customizable.

Shape and Position

The syntax is `background: radial-gradient(shape size at position, color1, color2, ...);`.

  • Shape: Can be circle or ellipse (the default).
  • Position: Uses the at keyword, just like `background-position` (e.g., `at center`, `at top left`, `at 25% 75%`).
  • Size Keywords: This is the most complex part. You can set the gradient's size using keywords that define its ending shape:
    • `closest-side`
    • `closest-corner`
    • `farthest-side`
    • `farthest-corner` (the default)
/* A perfect circle, centered, ending at the closest side */
background: radial-gradient(circle closest-side at center, white, black);

Chapter 3: The `conic-gradient()`

Conic gradients are perfect for pie charts, color wheels, or radar sweeps. The colors transition in a circle *around* a center point, rather than radiating *out* from it.

/* A simple 3-color pie chart */
background: conic-gradient(red, yellow, green);

Like other gradients, you can control the rotation and position using the from and at keywords. You can also set hard stops to create distinct slices.

/* A 'Pac-Man' shape, rotated 45 degrees */
background: conic-gradient(from 45deg, yellow 90deg, black 90deg);

Chapter 4: Repeating Gradients for Patterns

By simply adding `repeating-` to the front of the function (e.g., `repeating-linear-gradient()`), you can create intricate patterns. The gradient simply repeats itself infinitely. This is how designers create pinstripes, checkerboards, and other complex backgrounds with pure CSS.

/* Creates 10px blue stripes with 10px white gaps */
background: repeating-linear-gradient(
  45deg,
  blue,
  blue 10px,
  white 10px,
  white 20px
);
Key Takeaway: CSS gradients are a powerful tool for modern UI design. They replace heavy images, are fully customizable, and can be animated. Master linear, radial, and conic gradients to add depth, create patterns, and build beautiful, performant interfaces.

CSS Gradients Glossary

Gradient
A smooth, progressive transition between two or more specified colors. In CSS, this is a type of <image> data type.
`linear-gradient()`
A CSS function that creates an image consisting of a progressive transition between two or more colors along a straight line (the gradient line).
`radial-gradient()`
A CSS function that creates an image consisting of a progressive transition between two or more colors that radiate from an origin (the center). Its shape can be a circle or an ellipse.
`conic-gradient()`
A CSS function that creates an image consisting of a gradient with color transitions rotated around a center point, similar to a pie chart or color wheel.
`repeating-linear-gradient()`
A function that creates a linear gradient and then repeats it infinitely in all directions. Used to create patterns like stripes.
`repeating-radial-gradient()`
A function that creates a radial gradient and then repeats it infinitely from the center outwards. Used to create patterns like concentric circles.
Color Stop
A specific point on the gradient line where a color is defined. You can optionally specify its position with a percentage or length (e.g., `red 40%`).
Hard Stop
A sudden, sharp transition between colors, created by defining two color stops at the same position (e.g., `red 50%, blue 50%`).
Gradient Line
The imaginary line used in a `linear-gradient()` that defines the direction of the color transition. Its direction can be set with keywords (e.g., `to right`) or an angle (e.g., `45deg`).
Sizing Keywords
Keywords used in `radial-gradient()` to define the size of the gradient's ending shape. Examples: `closest-side`, `farthest-corner`.
`at` Keyword
Used in `radial-gradient()` and `conic-gradient()` to specify the position of the gradient's center (e.g., `at top left`, `at 20% 50%`).
`from` Keyword
Used in `conic-gradient()` to specify a starting angle for the gradient rotation (e.g., `from 90deg`).

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching CSS and building modern, responsive web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest CSS specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!