
CSS Custom Properties and Variables: How to Use var()
22/07/2023
CSS Transitions: Syntax, Timing, and Interaction States
27/07/2023
CSS gradients create gradual changes between colors. They are generated images, so you can use them with background, background-image, or another property that accepts an image. This lesson covers linear and radial gradients.
CSS gradients: linear and radial
1. Linear gradients
Use linear-gradient() for a color transition along a line. The syntax accepts a direction or angle followed by two or more color stops.
background: linear-gradient(direction or angle, color, color, color...); background: linear-gradient(direction or angle, color stop, color stop, color stop...); background: linear-gradient(color, color, color…);
What the arguments mean:
- Direction or angle: Use keywords such as
to top,to bottom,to left, orto right. You can also use an angle such as90deg. - Color: Add the colors you want at different stops. Use named colors, hexadecimal values, RGB, or HSL.
- Position: Place a color stop with a percentage or length. When positions are omitted, the browser distributes the stops.
Modern browsers support the standard gradient syntax. For older browsers, check the lesson on vendor prefixes before using legacy syntax.
Try these rules on different containers and compare the direction and the spacing of the color stops.
background: linear-gradient(90deg, rgba(176,174,238,1) 0%, rgba(29,30,31,1)); background: linear-gradient(to right, red,brown,orange,yellow,blue,green,violet,pink); background: linear-gradient(to top, #000, #ccc); background: linear-gradient(to right,red 300px,brown 400px,orange 700px);
2. Radial gradients
Use radial-gradient() for a transition that expands outward from a center. Its syntax is similar to a linear gradient, with options for the shape, size, and center.
background: radial-gradient(shape, color, color, color...); background: radial-gradient(shape at position, color, color, color...); background: radial-gradient(shape, color stop, color stop, color stop...); background: radial-gradient(color, color, color...); background: radial-gradient(farthest-side, color, color, ...);
What the arguments mean:
- Shape: Use
circleorellipse. An ellipse is the default when the arguments do not imply a circle. You can also specify a size. - Color: List the colors at the points where you want the gradient to change.
- Position: Set the distance of each color stop along the gradient line. Use
atin the first argument to position the center. - farthest-side: Sizes the ending shape to reach the farthest side or sides, depending on whether it is a circle or an ellipse.
Try the following examples and compare their shapes, positions, and color stops.
background: radial-gradient(ellipse, rgba(176,174,238,1) 0%, rgba(29,30,31,1)); background: radial-gradient(circle,red,brown,orange,yellow,blue); background: radial-gradient(circle,red 300px,brown 400px,orange 700px); background: radial-gradient(#000, #ccc); background: radial-gradient(farthest-side, #ff5733, #ffca3a);
3. Gradient generators
A visual generator lets you adjust the gradient and copy the resulting CSS. Check the contrast anywhere text will appear over the gradient.

background: rgb(176,174,238); background: radial-gradient(circle, rgba(176,174,238,1) 0%, rgba(29,30,31,1) 100%);
4. Gradient patterns
Gradients can create detailed repeating patterns without an image download. Compare how different layers and repeated color stops form the shapes.
Proposed exercise: Apply a gradient to a container in your web project using the colors from its style guide.