CSS Gradients Detailed Explanation

CSS Gradients Detailed Explanation

Gradients are a technique in CSS used to create smooth transitional background images, allowing you to generate gradual visual effects between two or more colors without using images. CSS gradients are divided into linear gradients (linear-gradient), radial gradients (radial-gradient), and the less commonly used conic gradients (conic-gradient). Let's delve into the details step by step.


1. Basic Concepts of Gradients

Gradients are defined via the background-image property (or background), for example:

div {
  background-image: linear-gradient(red, blue);
}

This code creates a vertical gradient from red to blue (default direction is top to bottom).


2. Linear Gradient (linear-gradient)

Syntax Structure:

linear-gradient([direction], color1 [position], color2 [position], ...);
  • Direction: Optional parameter, can be an angle (e.g., 45deg) or a keyword (e.g., to top right). Default value is to bottom (top to bottom).
  • Colors and Positions: A color value can be followed by a percentage or length value, indicating where the color transition begins.

Example Analysis:

(1) Default Direction Gradient

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

Effect: From top to bottom, red gradually changes to yellow.

(2) Specifying Direction

.box {
  background-image: linear-gradient(to right, red, yellow);
}

Effect: From left to right, red gradually changes to yellow.

(3) Multiple Colors and Position Control

.box {
  background-image: linear-gradient(to bottom, red 0%, yellow 50%, blue 100%);
}

Effect:

  • Starts as red from the top, changes to yellow at 50% height, and finally becomes blue at the bottom.
  • If positions are omitted, the browser will automatically distribute colors evenly.

(4) Using Angles

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

Effect: Gradient along a 45-degree angle (from bottom-left to top-right).


3. Radial Gradient (radial-gradient)

Radial gradients spread outward from a center point, with the following syntax:

radial-gradient([shape] [size] at [center], color1 [position], color2 [position], ...);
  • Shape: ellipse (ellipse, default) or circle (circle).
  • Size: Such as farthest-corner (to the farthest corner, default), closest-side (to the closest side), etc.
  • Center: Such as at center (default), at top left, etc.

Example Analysis:

(1) Default Radial Gradient

.box {
  background-image: radial-gradient(red, yellow);
}

Effect: From the center outward, red gradually changes to yellow (ellipse shape).

(2) Specifying Shape and Size

.box {
  background-image: radial-gradient(circle closest-side, red, blue);
}

Effect: Circular gradient, colors spread from the center to the closest side.

(3) Explicit Center Point

.box {
  background-image: radial-gradient(at top right, red, yellow);
}

Effect: Gradient outward starting from the top-right corner.


4. Advanced Gradient Techniques

(1) Hard-Edge Gradients (Color Bands)

By making adjacent color positions the same, you can create abrupt color blocks:

.box {
  background-image: linear-gradient(to right, red 50%, blue 50%);
}

Effect: Left half is red, right half is blue, with no transition in between.

(2) Repeating Gradients

  • repeating-linear-gradient: Repeats a linear gradient pattern.
.box {
  background-image: repeating-linear-gradient(45deg, red, red 10px, blue 10px, blue 20px);
}

Effect: Red and blue alternating diagonal stripes.

  • repeating-radial-gradient: Repeats a radial gradient.

(3) Combining Gradients with Backgrounds

Gradients can be combined with other background properties, such as:

.box {
  background: 
    linear-gradient(rgba(255,0,0,0.5), transparent),
    url("image.jpg");
}

Effect: Semi-transparent red gradient overlaid on an image.


5. Browser Compatibility and Considerations

  • Pay attention to prefix compatibility for gradients (e.g., older Webkit browsers require the -webkit- prefix).
  • Gradients are background images, so they cannot be used with the background-color property.
  • Use online tools (such as CSS Gradient Generators) for visual debugging of gradients.

By mastering the combination of gradient parameters, you can create complex background effects, reduce the use of image resources, and improve page loading performance.