CSS Gradients
CSS Gradients allow you to display smooth transitions between two or more specified colors. Gradients are actually a type of <image> data type, which means they can be used anywhere an image can be used, most commonly in the background-image property.
CSS defines two main types of gradients: Linear Gradients and Radial Gradients.
Linear Gradients (linear-gradient())
Linear gradients create a color transition along a straight line. You need to specify at least two colors (called "color stops").
Syntax
linear-gradient([<angle> | to <side-or-corner>], <color-stop1>, <color-stop2>, ...);
- Direction (Optional):
to <side-or-corner>: Can beto right,to left,to top,to bottom,to top right, etc.<angle>: An angle value, such as45deg.0degis equivalent toto top,90degis equivalent toto right.- If omitted, the default is
to bottom.
- Color Stops: Color values, optionally followed by a position (percentage or length).
Examples
Radial Gradients (radial-gradient())
Radial gradients create a color transition radiating outward from a central point in a circular or elliptical shape.
Syntax
radial-gradient([<shape> <size> at <position>], <color-stop1>, <color-stop2>, ...);
- Shape (Optional):
circleorellipse(default). - Size (Optional): Defines the size of the ending shape. Can be keywords (
closest-side,farthest-side,closest-corner,farthest-corner) or specific lengths/percentages. - Position (Optional): Uses the
atkeyword to define the center point of the gradient, similar tobackground-position. - Color Stops: Same as linear gradients.
Examples
Repeating Gradients
The repeating-linear-gradient() and repeating-radial-gradient() functions can create repeating gradient patterns. You need to specify explicit positions for color stops, and the browser will repeat this pattern to fill the entire background.
Gradients are a powerful tool for implementing complex, beautiful backgrounds with pure CSS, without using any image files.