For the background-image, instead of giving it a link to a static image, let’s tell CSS to generate a dynamic image. The firsttype is called a linear-gradient.
For example, from red to green:
main {
background-color: violet;
background-image: linear-gradient(red, green);
}
As you can see, a linear-gradient() creates linear bands of colors, and in this example, they are from red to green.
linear-gradient() => linear bands of colors
Also notice that this dynamic image has completelycovered the violetbackground. As you already know, CSS places background-image on top of background-color.
As we have learned in the previous lesson, CSS allows multiple layers of backgroundimages. They can be any staticordynamic images in any order.
Let’s addanotherlayer: linear-gradient() from blue to orange
main {
background-color: violet;
background-image:
linear-gradient(blue, orange),
linear-gradient(red, green);
}
A comma is required to separate these two layers. The order we write here will be the order that we see.
To see the bottomlayer, for example, we can replace orange with rgb(0, 0, 0, 0) which produces total transparency:
Now, let’s discuss the differences. First, let’s create another layer: radial-radient(), at 30% from the left, and 40% from the top, from red to yellow
If the ellipse keyword is used, both radii need to be specified even if they have the same length.
ellipse=> requires both radii
Finally, like linear gradients, we can also make radial gradientsrepeat. Let’s do an example: repeating-radial-gradient(), a circle with a radius of 100px located at the center from red to orange
main {
width: 100vw;
height: 100vh;
background-color: violet;
background-image:
repeating-radial-gradient(circle 100px at center, red, orange),
radial-gradient(ellipse 100px50px at 30%40%, red, yellow),
radial-gradient(at 50%40%, yellow 0%19%, 20%, lightblue);
}
As you can see, the length of each ring is 100px which is repeated to cover the entirebackground.
The thirdtype of gradient is called a conic-gradient.
Let’s see an example: background-image is conic-gradient() from red to violet
main {
background-color: violet;
background-image: conic-gradient(red, violet);
}
Again, we can specify as many colors as we like.
As you can see, a conic-gradient()spiralsclockwise around a center like a cone.
conic-gradient() => colors spiral clockwise around center
Like radial gradients, we can position this centeranywhere. For example, at 30% from the left, and 40% from the top:
main {
background-color: violet;
background-image: conic-gradient(at 30%40%, red, violet);
}
By default, the spiralbeginsfrom 0deg:
main {
background-color: violet;
background-image: conic-gradient(from 0deg at 30%40%, red, violet);
}
If we want, we can change this to any angle we want. For example, 45deg:
main {
background-color: violet;
background-image: conic-gradient(from 45deg at 30%40%, red, violet);
}
Like linear and radial gradients, we can specify color starts and color stops. For example, red is from 0deg to 90deg, and violet is from 90deg to 360deg:
main {
background-color: violet;
background-image: conic-gradient(from45deg at 30% 40%, red 0deg90deg, violet 90deg360deg);
}
360deg is a full circle.
If we combine this with a radial-gradient(), we can create a pie chart.
For example:
radial-gradient()
at 30% from the left, and 40% from the top (to give it the same center)
startingfromtransparentfrom 0% to 20%
and ending atwhite from 20% to 100%
main {
background-color: violet;
background-image:
radial-gradient(at 30%40%, transparent 0%20%, white 20%100%),
conic-gradient(from 45deg at 30%40%, red 0deg90deg, violet 90deg360deg);
}
Lastly, like linear and radialgradients, we can also make conic gradientsrepeat.
Let’s demonstrate this by creating a roulette wheel:
repeating-conic-gradient()
from 0deg
at center
from red to black
main {
background-color: violet;
background-image:
repeating-conic-gradient(from 0degatcenter, red, black),
radial-gradient(at30%40%, transparent0%20%, white 20%100%),
conic-gradient(from 45deg at30%40%, red 0deg 90deg, violet 90deg 360deg);
}
If a roulette wheel has 36 pockets, each pocket should be 10deg in length.
Therefore:
red is from 0deg to 10deg
and black is from 10deg to 20deg
main {
background-color: violet;
background-image:
repeating-conic-gradient(from0deg at center, red 0deg10deg, black 10deg20deg),
radial-gradient(at 30% 40%, transparent 0% 20%, white 20% 100%),
conic-gradient(from45deg at 30% 40%, red 0deg90deg, violet 90deg360deg);
}
As you can see, the red and blackrepeat until 360deg is reached.
Now, let’s use radial-gradient() to make it into a wheel:
radial-gradient()
circle
at center
from white0% to 45%
to transparent45% to 65%
then white again from 65% to 100%
main {
background-color: violet;
background-image:
radial-gradient(circle atcenter, white 0%45%, transparent45%65%, white 65%100%),
repeating-conic-gradient(from 0degatcenter, red 0deg 10deg, black 10deg 20deg),
radial-gradient(at30%40%, transparent0%20%, white 20%100%),
conic-gradient(from 45deg at30%40%, red 0deg 90deg, violet 90deg 360deg);
}
Again, when these conditions are met, CSS will repeat the gradient pattern until the entirebackground is covered.
Concept Quiz
Take my Programming Concept Quiz to check your understanding! For every correct choice, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Sample Quiz Questions for Lesson 13:
Question 1:
CSS Gradients are:
Static images
Static colors
Dynamic images
Dynamic colors
Question 2:
Which type of gradient creates linear bands of colors?
linear-gradient()
radial-gradient()
conic-gradient()
Question 3:
Which type of gradientradiates out from a centerpoint?
linear-gradient()
radial-gradient()
conic-gradient()
Question 4:
Which type of gradientspiralsclockwise around a centerpoint?
linear-gradient()
radial-gradient()
conic-gradient()
Question 5:
Multiple layers of backgroundimages are NOT allowed.
True
False
Question 6:
The transparent keyword is equal to:
rgb(255, 255, 255)
rgb(255, 255, 255, 255)
rgb(0, 0, 0)
rgb(0, 0, 0, 0)
Question 7:
Which value controls where the transitionoccurs?
color hint
color start
color stop
Question 8:
Which value controls where the colorbegins?
color hint
color start
color stop
Question 9:
Which value controls where the colorends?
color hint
color start
color stop
Question 10:
We can specify as many colors to transition as we want.
True
False
Question 11:
To create a hard line, the color stop of the color mustequal the color start of the next color.
True
False
Question 12:
By default, the firstcolor will start at 0.
True
False
Question 13:
By default, the lastcolor will stop at 100%.
True
False
Question 14:
By default, a radial-gradient() is a:
circle
ellipse
Question 15:
A conic-gradientspiralscounterclockwise.
True
False
Question 16:
A full circle is equal to:
0deg
90deg
180deg
360deg
Question 17:
To create a gradient, one color is enough.
True
False
Question 18:
To make linear gradientsrepeat, the total length of the color bands must be ____ than the size of the background.
less
greater
Question 19:
To make radial gradientsrepeat, the total length of the color rings must be ____ than the size of the background.
less
greater
Question 20:
To make conic gradientsrepeat, the total length of the color slices must be ____ than the size of the background.
Check out my Interactive Coding Exercises to put to practice what you have learned! There, you will also find interactive hints to help you understand each line of code. Likewise, for every correct solution, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
Review what you have learned by playing my Syntax Flashcard Game! These flashcards are designed to help you commit to memory all the newcodesyntaxes you learned in this lesson. Likewise, for every correct answer, you will earn SW Coins which you can redeem for coupons towards the purchase of any of my Udemy courses!
If you have successfully developed the critical and creative thinking to combine multiplegradients to generate almost any design pattern of your imagination, then you are ready to take on the challenge of designing page layouts that can adapt to any device screen size. In the next two lessons, we will learn about two powerful CSS technologies called Flexbox and Grid to make creating responsive layouts easier than ever before!
When you use my referral link above 👆 to become a Medium member, all proceeds will be donated towards the construction of the Silicon Wat Campus for children in Ukraine and Cambodia ❤️