Gradients
Definition and purpose
A gradient is a generated image that changes color across a line, outward from a center or around an angle. It has no intrinsic dimensions.
Hard stops
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { height: 80px; background-image: linear-gradient(to right, red 50%, blue 50%); } </style> </head> <body> <div id="a" aria-hidden="true"></div> </body></html>Equal-position stops produce a sharp boundary.
Syntax and mental model
linear-gradient(to right, red, blue) changes along a line; radial-gradient(circle, white, navy) radiates outward; conic-gradient(red, blue, red) sweeps around a center. Stops set colors and positions. Repeating variants repeat the stop pattern rather than tile the entire box.
Radial and conic
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { height: 80px; background-image: radial-gradient(circle, white, navy); } #b { height: 80px; background-image: conic-gradient(red, blue, red); } </style> </head> <body> <div id="a" aria-hidden="true"></div> <div id="b" aria-hidden="true"></div> </body></html>A changes with distance from center; B changes by angle.
Common mistakes
Use gradients as image values, not background-color values. Check text contrast across the entire gradient or keep text on a solid surface.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Background layers and positioning. Next lesson: Borders and rounding.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Gradients — knowledge check
Five questions on gradients.
Which property accepts a gradient?
Which gradient changes around a center by angle?
What creates a sharp transition?
Which repeats a linear stop pattern?
What must be checked when putting text over gradients?