Gradient text color
Is there a generator , or an easy way to generate text like this but without having to define every letter
So something like this:
.rainbow {
background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
<span class="rainbow">Rainbow text</span>
But not with rainbow colors but generate with other colors (for example white to grey/light blue gradient etc) I can't find an easy solution for this. Any solutions?
Solution 1:
I don't exactly know how the stop stuff works. But I've got a gradient text example. Maybe this will help you out!
_you can also add more colors to the gradient if you want or just select other colors from the color generator
.rainbow2 {
background-image: -webkit-linear-gradient(left, #E0F8F7, #585858, #fff); /* For Chrome and Safari */
background-image: -moz-linear-gradient(left, #E0F8F7, #585858, #fff); /* For old Fx (3.6 to 15) */
background-image: -ms-linear-gradient(left, #E0F8F7, #585858, #fff); /* For pre-releases of IE 10*/
background-image: -o-linear-gradient(left, #E0F8F7, #585858, #fff); /* For old Opera (11.1 to 12.0) */
background-image: linear-gradient(to right, #E0F8F7, #585858, #fff); /* Standard syntax; must be last */
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
.rainbow {
background-image: -webkit-gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
background-image: gradient( linear, left top, right top, color-stop(0, #f22), color-stop(0.15, #f2f), color-stop(0.3, #22f), color-stop(0.45, #2ff), color-stop(0.6, #2f2),color-stop(0.75, #2f2), color-stop(0.9, #ff2), color-stop(1, #f22) );
color:transparent;
-webkit-background-clip: text;
background-clip: text;
}
<span class="rainbow">Rainbow text</span>
<br />
<span class="rainbow2">No rainbow text</span>
Solution 2:
The way this effect works is very simple. The element is given a background which is the gradient. It goes from one color to another depending on the colors and color-stop percentages given for it.
For example, in rainbow text sample (note that I've converted the gradient into the standard syntax):
- The gradient starts at color
#f22
at0%
(that is the left edge of the element). First color is always assumed to start at0%
even though the percentage is not mentioned explicitly. - Between
0%
to14.25%
, the color changes from#f22
to#f2f
gradually. The percenatge is set at14.25
because there are seven color changes and we are looking for equal splits. - At
14.25%
(of the container's size), the color will exactly be#f2f
as per the gradient specified. - Similarly the colors change from one to another depending on the bands specified by color stop percentages. Each band should be a step of
14.25%
.
So, we end up getting a gradient like in the below snippet. Now this alone would mean the background applies to the entire element and not just the text.
.rainbow {
background-image: linear-gradient(to right, #f22, #f2f 14.25%, #22f 28.5%, #2ff 42.75%, #2f2 57%, #2f2 71.25%, #ff2 85.5%, #f22);
color: transparent;
}
<span class="rainbow">Rainbow text</span>
Since, the gradient needs to be applied only to the text and not to the element on the whole, we need to instruct the browser to clip the background from the areas outside the text. This is done by setting background-clip: text
.
(Note that the background-clip: text
is an experimental property and is not supported widely.)
Now if you want the text to have a simple 3 color gradient (that is, say from red - orange - brown), we just need to change the linear-gradient specification as follows:
- First parameter is the direction of the gradient. If the color should be red at left side and brown at the right side then use the direction as
to right
. If it should be red at right and brown at left then give the direction asto left
. - Next step is to define the colors of the gradient. Since our gradient should start as red on the left side, just specify
red
as the first color (percentage is assumed to be 0%). - Now, since we have two color changes (red - orange and orange - brown), the percentages must be set as 100 / 2 for equal splits. If equal splits are not required, we can assign the percentages as we wish.
- So at
50%
the color should beorange
and then the final color would bebrown
. The position of the final color is always assumed to be at 100%.
Thus the gradient's specification should read as follows:
background-image: linear-gradient(to right, red, orange 50%, brown).
If we form the gradients using the above mentioned method and apply them to the element, we can get the required effect.
.red-orange-brown {
background-image: linear-gradient(to right, red, orange 50%, brown);
color: transparent;
-webkit-background-clip: text;
background-clip: text;
}
.green-yellowgreen-yellow-gold {
background-image: linear-gradient(to right, green, yellowgreen 33%, yellow 66%, gold);
color: transparent;
-webkit-background-clip: text;
background-clip: text;
}
<span class="red-orange-brown">Red to Orange to Brown</span>
<br>
<span class="green-yellowgreen-yellow-gold">Green to Yellow-green to Yellow to Gold</span>