HTML&CSS · December 20, 2023

Css Basic: spreadMethod

CSS Basics: spreadMethod

In CSS, the spreadMethod property is used in conjunction with the linearGradient() and radialGradient() functions to define how the gradient colors are spread within a shape or element. This property allows you to control the distribution of colors in a gradient, giving you more control over the visual appearance of your designs.

Understanding spreadMethod

The spreadMethod property has three possible values:

  • pad: This is the default value. It extends the last color of the gradient beyond the shape's boundaries, creating a solid color fill.
  • reflect: This value reflects the gradient colors at the shape's boundaries, creating a mirrored effect.
  • repeat: This value repeats the gradient colors beyond the shape's boundaries, creating a seamless pattern.

By using these different spread methods, you can achieve various visual effects and enhance the overall design of your website or application.

Examples

Let's take a look at some examples to better understand how the spreadMethod property works:

Example 1: pad

.gradient {
  background: linear-gradient(to right, red, blue);
  spreadMethod: pad;
}

In this example, the gradient colors will extend beyond the shape's boundaries, creating a solid fill. The colors will smoothly transition from red to blue within the shape, and the last color (blue) will continue beyond the shape.

Example 2: reflect

.gradient {
  background: linear-gradient(to right, red, blue);
  spreadMethod: reflect;
}

With the reflect value, the gradient colors will be reflected at the shape's boundaries, creating a mirrored effect. This can be useful when you want to add depth or dimension to your designs.

Example 3: repeat

.gradient {
  background: linear-gradient(to right, red, blue);
  spreadMethod: repeat;
}

Using the repeat value, the gradient colors will repeat beyond the shape's boundaries, creating a seamless pattern. This can be particularly effective when you want to create a background with a repeating gradient.

Conclusion

The spreadMethod property in CSS allows you to control how gradient colors are spread within a shape or element. By using the pad, reflect, or repeat values, you can achieve different visual effects and enhance the overall design of your website or application.

To learn more about CSS and how it can improve your website's performance and design, consider exploring Server.HK, a leading VPS hosting company that offers reliable and high-performance hosting solutions.