CSS Gradients
Introduction
CSS or Cascading Style Sheets is the language used by front-end developers to add attractiveness to their website. It helps in describing the presentation of web pages, including colors, layouts, and fonts, thus making a web page presentable to its users. Front-end developers with good skills in CSS are in high demand these days. This article will help you to learn about the CSS gradients and improve your skills in the same.
Recommended Topic, Difference Between CSS and SCSS
CSS Gradients
CSS gradients are background images that help create a smooth transition between two or more colors on our website. The two major types of CSS gradients are:
- Linear Gradients
- Radial Gradients
A detailed study of these two CSS gradients is as follows. Please note this article is concerned with the CSS part only. For the HTML part, we just create a class called box and call it. The code snippet is as follows:
<div class="box"></div> |
Linear Gradients
As the name suggests, the flow of these CSS gradients is done in a straight path. These CSS gradients usually travel up, down, left, right, or diagonally. To create a linear gradient, we must define at least two color stops. Colors stops are the point of transition from one color to another. The various linear gradients with their code snippets in CSS is as follows:
Top to Bottom gradient
This is the default type of CSS gradients. As the name suggests, the gradient travels from top to bottom. We use the background-image property to create an image that can be used as the background of an element. The pseudo-code for the same is-
- Using two colors as arguments.
.box { background-image: linear-gradient(red, blue); width: 900px; height: 300px; } |
On running the code, we can see the following background:
- Using multiple colors as arguments.
.box { background-image: linear-gradient(violet, indigo, blue, green, yellow, orange, red); width: 900px; height: 300px; } |
On running the code, we can see the following background:
If you want to set where the colors stop, you can do so by passing in arguments as shown:
.box { background-image: linear-gradient(red 50%, blue); width: 900px; height: 300px; } |
Here we have set the red color to 50%. The output, in this case, will be:
.box { background-image: linear-gradient(red 200px, blue); width: 900px; height: 300px; } |
Here we have set the red color to 200px. The out, in this case, will be:
To left or to right gradient
In this type of CSS gradients, we can control the direction of the color. This can be done by adding ‘to left’ or ‘to right’ words in the arguments. The examples for the same is as follows:
- Example of ‘to left’:
.box { background-image: linear-gradient(to left, red, blue); width: 900px; height: 300px; } |
Output generated:
- Example of ‘to right’:
.box { background-image: linear-gradient(to right, red, blue); width: 900px; height: 300px; } |
Output generated:
Diagonal gradient
If we want more control over the direction of our CSS gradients, we can specify the direction or angle to do so. The following examples will help you to understand better.
- By specifying the direction: ‘to right top’ or ‘to left top.’
.box { background-image: linear-gradient(to right top, red, blue); width: 900px; height: 300px; } |
Here the gradient travels from the bottom left to the top right, as shown in the output image below.
.box { background-image: linear-gradient(to left top, red, blue); width: 900px; height: 300px; } |
Here the gradient travels from the bottom right to the left top, as shown in the output image below.
- By specifying the degree of the gradient.
.box { background-image: linear-gradient(45deg, red, blue); width: 900px; height: 300px; } |
Here we have specified the degree of the gradient as 45 degrees. The output produced is as follows:
We can perform a few more operations on the CSS gradients, such as set some transparency value or produce a repeating gradient.
- By setting the transparency value.
.box { background-image: linear-gradient(to left, rgba(0,0,255,0), rgba(0,0,255,1)); width: 900px; height: 300px; } |
Here we set the transparency value for the color blue. The gradient travels from the left to the right direction, gradually decreasing in intensity. The output produced is as follows:
- By producing a repeating gradient.
.box { background-image: repeating-linear-gradient(red, green 10%, blue 20%); width: 900px; height: 300px; } |
Here we make a repeating sequence of red, green, and blue. The output produced will be:
Radial Gradients
This type of CSS gradients is similar to circles. To do so, we use the radial gradient command. There are various CSS gradients under the radial type. Some of them have been discussed below:
Evenly spaced radial-gradient
This can be considered the default radial gradient type. We just call the radial gradient command and pass the color/s of our choice and run our code. An example code snippet is given below:
.box { background-image: radial-gradient(red, yellow, blue); width: 900px; height: 300px; } |
The output produced is as follows:
Differently spaced radial-gradient
In this type of CSS gradients, we can control the amount of color we want by either specifying the percentage or pixels. An example code snippet is given below:
.box { background-image: radial-gradient(red 10%, yellow 25%, blue 60%); width: 900px; height: 300px; } |
The output so produced is as follows:
Using different size keywords
In this type of CSS gradients, we can define the size parameter as:
- closest-side: In this option, the gradient will meet the closest side of an element.
- farthest-side: In this option, the gradient will meet the farthest side of an element.
- closest corner: In this option, the gradient will meet the closest corner of an element.
- farthest-corner: In this option, the gradient will meet the farthest corner of an element.
An example of the same is given below:
.box { background-image: radial-gradient(closest-side at 65% 45%, red, yellow, blue); width: 900px; height: 300px; } |
The output produced is:
- Repeating radial-gradient: Just like we did in the repeating linear-gradient, here too, we can make repeating circles with colors of our choice. An example code snippet is given below:
.box { background-image: repeating-radial-gradient(red, green 10%, blue 15%); width: 900px; height: 300px; } |
The above code will produce the following output.
Frequently Asked Questions
Q1.) What do you mean by CSS, and what are its benefits?
Ans.) CSS stands for Cascading Style Sheets. They are the helping elements that assist a user in increasing the attractiveness of a plain HTML website. Usually, a front-end developer is responsible for the looks of a website. So learning CSS is a must for front-end developer aspirants.
Key Takeaways
In a nutshell, this article covered the topic of CSS gradients. We briefly discussed CSS and then did a detailed study of CSS gradients by learning about the two major types of CSS gradients with ample examples.
Curious about CSS and want to learn more about it? Click here: What Are The Major Difference Between CSS, CSS2 And CSS3.
Want to become a frontend developer but need some practice? Here are some of the interview questions related to CSS: CSS Interview Questions For Beginner in 2021: Part 1
Want to ace the coding rounds of big tech companies? Try our Attempt Unlimited Online Mock Test Series to start your preparation.
Written by Aditya Narayan Joardar