Skip to main content

CSS Gradients

A smooth blend between two or more colors inside an element comes from the CSS gradient functions, which you pass to the background-image property.

Three types of gradients are available:

  • Linear Gradients — colors change along a line running down, up, left, right, or diagonally
  • Radial Gradients — colors spread outward from a central point
  • Conic Gradients — colors rotate around a center point

CSS linear-gradient() Function

Call the CSS linear-gradient() function when colors should change along a straight line, whether that line runs down, up, left, right, or diagonally.

Color stops are the colors being blended, and at least two of them are required. A direction (or an angle) and a starting point are optional additions.

Top to Bottom (default)

In this linear gradient, red sits at the top of the element and blends downward into yellow at the bottom.

Prompt
Show a linear gradient on a div that goes from top to bottom, starting red and ending yellow. The div should be tall enough to clearly see the gradient.
Output: complete HTML page

Bottom to Top

Reversing the direction puts red at the bottom of the element, and the blend climbs until it reaches yellow at the top.

Prompt
Show a linear gradient on a div that goes from bottom to top, starting red and ending yellow.
Output: complete HTML page

Left to Right

Running sideways, this linear gradient begins with red on the left edge and reaches yellow on the right edge.

Prompt
Show a linear gradient on a div that goes from left to right, starting red and ending yellow.
Output: complete HTML page

Diagonal

Here the blend crosses the element diagonally, with red in the top-left corner and yellow in the bottom-right corner.

Prompt
Show a linear gradient on a div that goes diagonally from top-left to bottom-right, starting red and ending yellow.
Output: complete HTML page

Syntax

styles.css
background-image: linear-gradient(direction, color-stop1, color-stop2, ...);

Linear Gradient - Using Angles

The predefined directions (to bottom, to top, to right, to left, etc.) can give way to an angle value when you need finer control over where the gradient points.

  • "to top" matches 0deg
  • "to right" matches 90deg
  • "to bottom" matches 180deg
  • "to left" matches 270deg

Example

With its angle set to 180deg, this linear gradient blends from red to yellow.

Prompt
Show a linear gradient on a div using a 180deg angle, going from red to yellow.
Output: complete HTML page

Syntax

styles.css
background-image: linear-gradient(angle, color-stop1, color-stop2);

Linear Gradient - Multiple Color Stops

A linear gradient is not limited to two color stops.

Three Color Stops

Three colors run from top to bottom in this gradient: red, yellow, and green.

Prompt
Show a linear gradient on a div with three color stops: red, yellow, and green, going from top to bottom.
Output: complete HTML page

Rainbow Gradient

All the rainbow colors run from left to right here, with some text placed on top of them.

Prompt
Show a linear gradient going left to right with rainbow colors (red, orange, yellow, green, blue, indigo, violet) on a div, with centered white text reading "Rainbow Background".
Output: complete HTML page

Linear Gradient - Transparency

Fading effects are possible because CSS gradients can include transparency.

Set the color stops with the rgba() function, whose last value controls it on a scale from 0 to 1: 0 is fully transparent, and 1 is fully opaque (no transparency).

Example

Moving from left to right, this gradient begins fully transparent and ends as full red.

Prompt
Show a linear gradient on a div going from left to right, starting fully transparent red (rgba(255,0,0,0)) and ending with full red (rgba(255,0,0,1)).
Output: complete HTML page

CSS repeating-linear-gradient() Function

To repeat a linear gradient pattern, use the CSS repeating-linear-gradient() function.

Example

Here the repeating pattern is red, then yellow at 10%, then green at 20%.

Prompt
Show a repeating linear gradient on a div using red, yellow at 10%, and green at 20%.
Output: complete HTML page