Like this
.gradient {
width: 500px;
height: 200px;
background: linear-gradient(to right,
red 20%,
green 20%,
green 40%,
blue 40%,
blue 60%,
purple 60%,
purple 80%,
yellow 80%,
yellow 100%
);
}
<div class="gradient"></div>
Answer from fen1x on Stack OverflowVideos
Like this
.gradient {
width: 500px;
height: 200px;
background: linear-gradient(to right,
red 20%,
green 20%,
green 40%,
blue 40%,
blue 60%,
purple 60%,
purple 80%,
yellow 80%,
yellow 100%
);
}
<div class="gradient"></div>
Gradient is created through the blending of colors. You can control the blending effect of colors by specifying range for each color like in below example of .flag:
- the color
#00ae00will be applied till33.3%of thediv. - from that point till
66.6%,whitewill be applied. - in the end
orangewill start from66.6%till the end of the box.
This way you can add as many colors as you want.
But one thing keep in mind, it looks fine when the degree is straight, if you change the angle, in some cases edges of colors may not look smooth (depends upon color and screen density) as you can see in .pixeleted
.flag{
background: linear-gradient(to right, #00ae00 33.3%, white 33.3%, white 66.6%, orange 66.6%);
}
.pixeleted{
background: linear-gradient(30deg, red 33.3%, black 33.3%, black 66.6%, red 66.6%);
}
div {
width: 290px;
height: 145px;
border: 2px solid #999;
display: inline-block;
}
<div class="flag"></div>
<div class="pixeleted"></div>
Eveyrone is giving the to bottom solution but the trivial solution is to consider to top and keep the percentage values you are using in the picture:
linear-gradient(to top, #mycolor3 10%, #mycolor2 45%, #mycolor1 85%);
example:
body {
background: linear-gradient(to top, red 10%, purple 45%, blue 85%);
margin: 0;
height: 100vh;
}
Concerning the percentage between (50% and 30%), they are probably the color hints (also called color interpolation hints). From the new specification
Between two color stops there can be a color interpolation hint, which specifies how the colors of the two color stops on either side should be interpolated in the space between them (by default, they interpolate linearly). There can only be at most one color interpolation hint between any two given color stops; using more than that makes the function invalid.
example:
body {
background:
/* First gradient with hints*/
linear-gradient(to top, red 10%, purple 45%, blue 85%) left /45% 100%,
/* Second gradient with hints*/
linear-gradient(to top, red 10%,27.5% ,purple 45%, 57% ,blue 85%) right/45% 100%;
background-repeat:no-repeat;
margin: 0;
height: 100vh;
}
You need to specify the points in ascending order. Just invert the values you have (you don't really need the purple but could add it if desired):
body {
height: 100vh;
overflow: hidden;
background: linear-gradient(to bottom, blue 15%, red 90%) center/cover no-repeat;
}