Search code examples
cssanimationtransition

Adding Transition to text on load


I am trying to add transition to text on my site such that when the page is loaded, the opacity of "Hello" changes from 0 to 1 in the first second and the opacity of the text "Name" changes from 0 to 1 in the next second. I tried to use transition but it would work only with hover not with load. I tried keyframes and animation but that does work only in the designated time and the page loads with a default opacity of 1. The requirement is this: Hello (appears in the first second with a transition) Name (appears in the next second with transition) Initially both having opacity 0. Please help.


Solution

  • You can use forwards animation.

    .hello,
    .name {
      opacity: 0;
      animation: fadeIn 1s linear forwards;
    }
    
    .name {
      animation-delay: 1s;
    }
    
    @keyframes fadeIn {
      to {
        opacity: 1;
      }
    }
    <div class="hello">Hello</div>
    <div class="name">there</div>