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!
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! đ