Andy
Andy

Reputation: 49

Replacing text in CSS

I'm trying to make an animation loop in CSS that will change a class-defined h1 text into something else, 4 times.

Basically, I want a certain text to change from "hard-working" to "tired" after 1s, then to "coffee-addicted" and then to "fast-paced", and loop indefinitely. I've come up with code, but it doesn't work. I'm new to animating, so I could use an expert mind!

I've tried two approaches but neither work :(

Number 1:

.animate{
display: inline;
margin:0;
padding: 0;
border: 0;
-webkit-animation-name: animate;
-webkit-animation-duration: 2s;
animation-name: animate;
animation-duration: 2s;
animation-delay: 1s;
animation-iteration-count: infinite;
}


@keyframes animate{
0% {content: 'hard-working';}
25%{content: 'tired';}
50%{content: 'coffee-addicted';}
75%{content:'fast-paced';}
100%{content: 'hard-working';}
}

Number 2:

.animate{
display: inline;
margin:0;
padding: 0;
border: 0;
-webkit-animation-name: animate;
-webkit-animation-duration: 2s;
animation-name: animate;
animation-duration: 2s;
animation-delay: 1s;
animation-iteration-count: infinite;
}



@keyframes animate{
0% {content: 'hard-working';}
25%{visibility: hidden; .animate:after{content:'tired'};}
50%{visibility: hidden .animate:after{content:'coffee-addicted'};}
75%{visibility: hidden; .animate:after{content:'fast-paced'};}
100%{visibility: hidden; .animate:after{content: 'hard-working'};}
}

JsFiddle 1

JsFiddle 2

Thanks in advance!

Edit: if visualization helps: https://i.sstatic.net/8MW65.jpg

Upvotes: 1

Views: 2117

Answers (1)

Itay Ganor
Itay Ganor

Reputation: 4205

apply the animation on a :before pseudo element:

   .animate {
       display: inline;
       margin: 0;
       padding: 0;
       border: 0;
   }
   
   .animate:before {
       content: 'hard-working';
       -webkit-animation-name: animate;
       -webkit-animation-duration: 2s;
       animation-name: animate;
       animation-duration: 2s;
       animation-delay: 1s;
       animation-iteration-count: infinite;
   }
   
   @keyframes animate {
       0% {
           content: 'hard-working';
       }
       25% {
           content: 'tired';
       }
       50% {
           content: 'coffee-addicted';
       }
       75% {
           content: 'fast-paced';
       }
       100% {
           content: 'hard-working';
       }
   }
<div class="animate"></div>

JSFiddle

Upvotes: 4

Related Questions