Course contentsShow
← Web Development
Lesson 189 of 2,9823. CSS FundamentalsFree lesson

Gradients: Linear Gradients

Create smooth color transitions with linear-gradient(), control direction, and define multiple color stops.

What You'll Learn

You'll learn how to create smooth, multi-color transitions using CSS linear gradients instead of solid colors.

What Are Linear Gradients?

A linear gradient is a gradual blend between two or more colors along a straight line. Instead of filling an element with one solid color, you can create smooth transitions that flow in any direction you choose. Think of it like watching a sunset where orange gradually fades into purple across the sky.

The linear-gradient() Function

You use linear-gradient() anywhere you'd normally use a color value, most commonly with background or background-image:

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

This creates a gradient that flows from red at the top to blue at the bottom.

Controlling Direction

By default, gradients flow top-to-bottom, but you can change this:

/* Left to right */
background: linear-gradient(to right, red, blue);

/* Diagonal */
background: linear-gradient(to bottom right, red, blue);

/* Using degrees (0deg = up, 90deg = right) */
background: linear-gradient(90deg, red, blue);

Multiple Color Stops

You're not limited to two colors—add as many as you need:

background: linear-gradient(red, yellow, green, blue);

You can also control where each color appears using percentages:

/* Yellow appears only at 20% down */
background: linear-gradient(red, yellow 20%, blue);

This gives you precise control over how colors blend and where transitions occur.

Key Takeaway: Linear gradients use linear-gradient() to create smooth color transitions along a straight line, with customizable direction and multiple color stops for complex effects.