Moving color text html

Creating Animated Gradient Texts (pure HTML and CSS!)

To add the gradients, we’ll set the color to transparent , the background-image to linear-gradient(. ) with our favorite colors, and background-clip to text to only render the gradient within the boundaries of the text. 👇🏻

.heading-line  /* . */ /* set the color of the text to transparent */ color: transparent; /* we need to clip the gradient background so that so only the text bit will have the gradient background (otherwise the entire rectangle the text is in would have it) */ -webkit-background-clip: text; background-clip: text; > .heading-line-first  background-image: linear-gradient(to left, #ed8936, #ed64a6); > .heading-line-second  background-image: linear-gradient(to left, #38b2ac, #0bc5ea); > .heading-line-third  background-image: linear-gradient(to left, #ecc94b, #48bb78); > 

heading saying animated. gradient. text. with gradient background and no animation

This is what you should end up with 👇🏻

Animating the gradient

In this section, we’re only going to animate the gradient background. To add some more spice to the animation, we’re also going to animate white background of each heading. That’s gonna be covered in the next section though!

We now have a solid foundation to implement the animation! Let’s think about how the animation should look from a visual standpoint. When the page loads, what colors should all the headings be? It should probably look like this, where the first heading has the gradient background and the rest have white backgrounds 👇🏻 heading saying animated. gradient. text. the first line has a gradient background and the rest has white backgrounds Then, after a little while, the first gradient should start fading away and the second one should start fading in. 👇🏻 heading saying animated. gradient. text. the first two lines has half gradient half white backgrounds and the third one has fully white background Finally, the first line should be white and the second should have the gradient. 👇🏻 heading saying animated. gradient. text. the first and the third lines have gradient background while the second line has white background The same applies to the third line. Once that’s finished, the first line’s gradient appears again and the cycle restarts. You can see how the gradients are fading in and out as we move the slider 👇🏻

  • one heading will have the gradient background (the rest will have white background)
  • two headings will be fading in/out

We can use the table below to know what exact behavior we want to model in CSS:

Part Heading 1 Heading 2 Heading 3
1 Gradient No gradient No gradient
2 Fading out Fading in No gradient
3 No gradient Gradient No gradient
4 No gradient Fading out Fading in
5 No gradient No gradient Gradient
6 Fading in No gradient Fading out

We have now done most of the hard work. Now it’s time to translate the table above into CSS.

The key thing we’ll be animating is the opacity of the gradient background.

When we want the gradient to be visible, we’ll set it to 1 and when we want it to be invisible, we’ll set it to 0 .

Before we can do that, however, we need to make sure that there is a white background underneath the gradient.

To do that, we’ll need to wrap the heading lines in span elements, add class heading-line-gradient to them and leverage the ::before pseudo-element.

  class="heading-line heading-line-first">  class="heading-line-gradient">Animated.   class="heading-line heading-line-second">  class="heading-line-gradient">Gradient.   class="heading-line heading-line-third">  class="heading-line-gradient">Text.   

The .heading-line-gradient will have the gradient background and .heading-line::before will have the white background.

/* move these style from .heading-line to .heading-line-gradient */ .heading-line-gradient  color: transparent; background-image: linear-gradient(to left, #ed8936, #ed64a6); -webkit-background-clip: text; background-clip: text; > /* move this from .heading-line-first */ .heading-line-first > .heading-line-gradient  background-image: linear-gradient(to left, #ed8936, #ed64a6); > /* move this from .heading-line-second */ .heading-line-second > .heading-line-gradient  background-image: linear-gradient(to left, #38b2ac, #0bc5ea); > /* move this from .heading-line-third */ .heading-line-third > .heading-line-gradient  background-image: linear-gradient(to left, #ecc94b, #48bb78); > /* white text underneath the gradient text */ .heading-line::before  line-height: 1; display: block; position: absolute; color: #fff; pointer-events: none; width: 100%; > .heading-line-first::before  content: "Animated."; > .heading-line-second::before  content: "Gradient."; > .heading-line-third::before  content: "Text."; > 

Finally, let’s focus on the animation! We’ll start with the first heading. We’ll be animating the opacity of the heading-line-gradient class.

To define the animation, we’ll use the @keyframes rule and we’ll use the animation property to apply it.

We’ll set the animation duration to 8s and set infinite as the number of iterations. 👇🏻

@keyframes first_heading_gradient  0%, /* 16.667 = 1/6 */ 16.667%  opacity: 1; > /* 33.333% = 2/6 */ 33.333%, /* 83.333% = 5/6 */ 83.333%  opacity: 0; > 100%  opacity: 1; > > .heading-line-first > .heading-line-gradient  /* . */ animation: first_heading_gradient 8s infinite; > 

We’ll do the same for the second and third headings. 👇🏻

@keyframes second_heading_gradient  0%, 16.667%  opacity: 0; > 33.333%, 50%  opacity: 1; > 66.667%, 100%  opacity: 0; > > @keyframes third_heading_gradient  0%, 50%  opacity: 0; > 66.667%, 83.333%  opacity: 1; > 100%  opacity: 0; > > .heading-line-second > .heading-line-gradient  /* . */ animation: second_heading_gradient 8s infinite; > .heading-line-third > .heading-line-gradient  /* . */ animation: third_heading_gradient 8s infinite; > 

This is what you should end up with 👇🏻

Animating white background

To make the animation stand out, we’ll have to animate the white background of the heading as well.

Take one more look the what we want to achieve 👇🏻

You can notice that the white background is sort of fading in when the gradient is fading out and vice versa.

The effect is subtle, but it gives the animation a little bit more spice.

We can’t, however, animate the white background for the whole duration of the gradient animation. That would look odd as the text would be almost invisible at one point.

Instead, we’ll only start fading the white background away when the gradient is halfway through its fade-in animation and vice versa.

Play around with this widget to see how the opacity of the gradient and white background should behave 👇🏻

Now that we have a better understanding of when to animate the white background, let’s create a table that we’ll later translate to CSS.

This table only deals with the first heading but the same logic applies to the second and third heading as well.

Now that we’re animating the white background as well, we’ll need to split the animation into 12 steps (instead of 6).

Part Gradient opacity White background opacity
1 1 0
2 1 (start to fade out) 0 (start to fade in)
3 0.5 (still fading out) 1 (already faded in)
4 0 (faded out) 1
5 0 1
6 0 1
7 0 1
8 0 1
9 0 1
10 0 (start fading in) 1
11 0.5 (halfway faded in) 1 (start fading out)
12 1 (faded in) 0 (faded out)

I know that this table seems to be a bit overwhelming and a bit tedious to put together

You can (and I highly encourage you to) create analogical tables for animating the second and third headings.

Anyways, we have done a lot of hard work, the last bit remaining is to express this table in CSS.

Let’s start with the first heading. 👇🏻

@keyframes first_heading_white_text  0%, /* 16.667 = 2/12 */ 16.667%  opacity: 0; > /* 25% = 3/12 */ 25%, /* 16.667 = 9/12 */ 91.667%  opacity: 1; > 100%  opacity: 0; > > .heading-line-first::before  /* . */ animation: first_heading_white_text 8s infinite; > 

Analogically, we want to animate the second and third headings’ white backgrounds. 👇🏻

@keyframes second_heading_white_text  0%, 25%  opacity: 1; > 33.333%, 50%  opacity: 0; > 58.333%, 100%  opacity: 1; > > @keyframes third_heading_white_text  0%, 58.333%  opacity: 1; > 66.667%, 83.333%  opacity: 0; > 91.667%, 100%  opacity: 1; > > .heading-line-second::before  /* . */ animation: second_heading_white_text 8s infinite; > .heading-line-third::before  /* . */ animation: third_heading_white_text 8s infinite; > 

Source code

You can find the source code for this tutorial on CodeSandbox.

If you liked this tutorial, you can also check out and subscribe to my YouTube channel and my blog!

Источник

Speedy CSS Tip! Animated Gradient Text

Jhey Tompkins

Create the markup for your text. Let’s use a header with the word «Speedy»:

h1 class="boujee-text">Hello!</h1>

Then, let’s give our body a darker background-color :

body  
display: grid;
place-items: center;
min-height: 100vh;
background: hsl(0 0% 20%);
>

Bump up the font-size on our text. Use clamp to make it responsive:

.boujee-text  
font-size: clamp(3rem, 25vmin, 8rem);
>

Create two custom properties for the colors we’re going to use. Apply a linear-gradient to the background using those colors and rotate it by 90 degrees:

.boujee-text 
--color-one: hsl(15 90% 55%);
--color-two: hsl(40 95% 55%);
font-size: clamp(3rem, 25vmin, 8rem);
background: linear-gradient(
,
var(--color-one),
var(--color-two),
var(--color-one)
) 0 0 / 100% 100%;
>

Create a custom property that you can use for the horizontal background size. Use it for background-size-x :

.boujee-text 
--bg-size: 400%;
--color-one: hsl(15 90% 55%);
--color-two: hsl(40 95% 55%);
font-size: clamp(3rem, 25vmin, 8rem);
background: linear-gradient(
,
var(--color-one),
var(--color-two),
var(--color-one)
) 0 0 / var(--bg-size) 100%;
>

To clip the background to the text set the color to transparent , and set background-clip: text :

.boujee-text 
--bg-size: 400%;
--color-one: hsl(15 90% 55%);
--color-two: hsl(40 95% 55%);
font-size: clamp(3rem, 25vmin, 8rem);
background: linear-gradient(
,
var(--color-one),
var(--color-two),
var(--color-one)
) 0 0 / var(--bg-size) 100%;
color: transparent;
-webkit-background-clip: text;
background-clip: text;
>

At this point, you could leave it there and experiment with the background-position and the gradient used in the background-image . Or, you could animate that gradient across your text. First, define a keyframe for the animation. This will use the —bg-size custom property you defined earlier. A good example of scoped custom properties making maintenance easier for you.

@keyframes move-bg  
to
background-position: var(--bg-size) 0;
>
>

Then apply the animation using animation :

.boujee-text 
--bg-size: 400%;
--color-one: hsl(15 90% 55%);
--color-two: hsl(40 95% 55%);
font-size: clamp(3rem, 25vmin, 8rem);
background: linear-gradient(
,
var(--color-one),
var(--color-two),
var(--color-one)
) 0 0 / var(--bg-size) 100%;
color: transparent;
background-clip: text;
-webkit-background-clip: text;
animation: move-bg 8s infinite linear;
>

To take this further, you can wrap your animation code in a media query to respect your user’s preferences for motion:

@media (prefers-reduced-motion: no-preference)  
.boujee-text
animation: move-bg 8s linear infinite;
>
@keyframes move-bg
to
background-position: var(--bg-size) 0;
>
>
>

Done! 🎉 You’ve created some animated gradient text with CSS using scoped custom properties and background-clip .

Here’s this tip in its speedy video form! ⚡️

Источник

Читайте также:  Beget install python 3
Оцените статью