Gradient In CSS
Table of contents
In CSS, gradients are used to create smooth transitions between two or more colors. Gradients can be created using the linear-gradient()
or radial-gradient()
functions.
Here's an example of how to create a linear gradient:
.gradient {
background: linear-gradient(to right, #ff0000, #0000ff);
}
In this example, the background
property is used to set the background of an element to a linear gradient. The linear-gradient ()
function takes two or more color values as arguments and creates a gradient between them.
The to the right
parameter sets the direction of the gradient, in this case from left to right. Other possible values for this parameter include to left
, to top
, to bottom
, and to bottom right
, among others
Here's an example of how to create a radial gradient:
.gradient {
background: radial-gradient(circle at center, #ff0000, #0000ff);
}
In this example, the background
property is used to set the background of an element to a radial gradient. The radial-gradient()
function takes two or more color values as arguments and creates a gradient between them.
The the the circle at the center
parameter sets the shape and position of the gradient. Other possible values for this parameter include ellipse
, closest-side
, and farthest-side
, among others.
Gradients can also be combined with other CSS properties, such as background-image
and background-repeat
, to create more complex effects.
.gradient {
background-image: linear-gradient(to right, #ff0000, #0000ff), url("image.jpg");
background-repeat: no-repeat;
background-size: cover;
}
In this example, the background-image
property is used to set the background of an element to a linear gradient and an image. The background-repeat
property is set to no-repeat
to prevent the image from repeating, and the background-size
property is set to cover
to make the image fill the entire element.
Overall, gradients in CSS are versatile tools that can be used to create a wide variety of visual effects.
Subscribe to my newsletter
Read articles from Saurav Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by