Welcome to the world of CSS gradients! In this tutorial, we will explore the three main types of gradients you can use in your web designs: linear, radial, and conic gradients. Gradients can add depth, color, and style to your elements, and are a fantastic way to create visually appealing designs. By the end of this tutorial, you’ll see how to create your own CSS gradients with ease.

To understand gradients, it’s essential to know a little bit about the CSS background-image property. Gradients are specified using the linear-gradient(), radial-gradient(), and conic-gradient() functions within this property.

Now let’s dive into it!

Linear Gradients

Linear gradients are created by transitioning between two or more colors along a straight line. To create a linear gradient, you’ll need to use the linear-gradient() function in your CSS. Here's a basic example of a linear gradient:

background-image: linear-gradient(to right, red, blue);

This example creates a linear gradient that transitions from red to blue, with the red on the left and blue on the right. The to right keyword specifies the direction of the gradient. You can also use to left, to top, to bottom, or any combination of these keywords to control the gradient direction.

Radial Gradients

Radial gradients are created by transitioning between colors along a circular path. To create a radial gradient, you’ll need to use the radial-gradient() function in your CSS. Here's a basic example of a radial gradient:

background-image: radial-gradient(circle, red, blue);

This example creates a radial gradient that transitions from red to blue in a circular pattern. The circle keyword specifies the shape of the gradient, which can also be ellipse for an elliptical gradient.

Conic Gradients

Conic gradients are created by transitioning between colors along a conical path. To create a conic gradient, you’ll need to use the conic-gradient() function in your CSS. Here's a basic example of a conic gradient:

background-image: conic-gradient(red, blue);

This example creates a conic gradient that transitions from red to blue in a circular pattern, with the red at the top and blue at the bottom. Conic gradients can be used to create unique, eye-catching designs.

Gradient Repeats

By default, gradients are not repeated, but you can control the repeat behavior using the repeat keyword. For example, to create a repeated linear gradient:

background-image: linear-gradient(to right, red, blue) repeat;

This example creates a repeated linear gradient that transitions from red to blue, with the red on the left and blue on the right.

Multiple Color Stops

Gradients can include multiple color stops to create more complex transitions. To add additional color stops, separate the colors with commas:

background-image: linear-gradient(to right, red, yellow, blue);

This example creates a linear gradient that transitions from red to blue, with a yellow color stop in the middle.

Gradient Transformations

You can apply transformations to gradients using the transform property in CSS. This allows you to rotate, scale, or translate gradients as needed. For example, to rotate a linear gradient by 45 degrees:

background-image: linear-gradient(45deg, red, blue);

This example creates a linear gradient that transitions from red to blue and is rotated by 45 degrees.

Conclusion

Now that you’ve learned about linear, radial, and conic gradients, as well as how to control their properties and apply transformations, you’re ready to create stunning gradient-based designs in your projects. Don’t forget to check out my e-book for even more in-depth knowledge and techniques. Happy coding!


Tim profile image

A little about me..

Hey, I’m Tim! 👋

I’m a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.

While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.

Thanks for reading! 🎉