Animating the appearance of text in CSS

Animating the appearance of text in CSS

In this tutorial we will create a simple CSS animation of the appearance of the text. Let’s break our text into separate phrases and scroll them one after another, like slides. This technique can often be seen in the header of a website or on a banner.

HTML code

Let’s create a parent container for text boxes. All text blocks will be absolutely the same in form, but different in content.

    

Приходи в школу

    

Учись вождению

    

Получай права

    

Наслаждайся

Animation of the appearance of text in CSS.

CSS code

In order for our container to be placed in the center of the screen, apply to the tag body flex and style the container itself.


body {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 25vh;
}

.container {
    text-align: center;
    height: 60px;
    background: yellow;
    border: 3px solid black;
    padding: 0 50px; /* отступы по краям */
}

Animation of the appearance of text in CSS.

Next, let’s style the block with the text.


.text {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 40px;
    line-height: 60px; /* равен высоте контейнера */
    text-transform: uppercase;
}

Animation of the appearance of text in CSS.

We want the block with the text to change in turn in the yellow container, and hide everything outside the container. To do this, add one important property to the container, overflow: hidden.


.container {
    ...
    overflow: hidden;
}

Now only the first block with the text remains visible.

Animation of the appearance of text in CSS.

CSS Animation

Create 7 frames keyframes from 0% to 100% in approximately 16% steps. We get such a number as a result of dividing 100/6. Where did these 7 frames come from? We have four test lines to run back and forth. Since the first line will appear in the first frame by default, the total number of frames is calculated using the formula 8-1 = 7. As we move up, we will increase in importance margin-top by 60 pixels, and when moving down, shrink it by 60 pixels.


@keyframes slide {
    0% {
        margin-top: 0;
    }
    16% {
        margin-top: -60px;
    }
    33% {
        margin-top: -120px;
    }
    50% {
        margin-top: -180px;
    }
    66% {
        margin-top: -120px;
    }
    82% {
        margin-top: -60px;
    }
    100% {
        margin-top: 0;
    }
}

Let’s give an arbitrary name to the animation slide, we will indicate the duration of the animation and set to infinity infinite… Let’s animate the first block with text, which will drag the other three blocks. When the first block goes up outside the yellow container, it will hide, thanks to the property overflow: hidden… The first line will be replaced by another, and so on in a circle.


.text:first-child {
    animation: slide 12s infinite;
}

Take a look at an example at CodePen

Previous article Next article

Copying of materials is allowed only with the indication of the author (Mikhail Rusakov) and an indexed direct link to the site (http://myrusakov.ru)!

Add to my friends VK: http://vk.com/myrusakov.
If you want to rate me and my work, then write it in my group: http://vk.com/rusakovmy.

If you do not want to miss new materials on the site,
then you can subscribe to updates: Subscribe to updates

If you still have any questions, or you have a desire to comment on this article, then you can leave your comment at the bottom of the page.

Recommend this article to your friends:

If you liked the site, then post a link to it (on your site, on the forum, in contact):

  1. Button:

    It looks like this: How to create your website

  2. Text link:

    It looks like this: How to create your website

  3. BB-code of the link for forums (for example, you can put it in the signature):

Related Posts

Property Management in Dubai: Effective Rental Strategies and Choosing a Management Company

“Property Management in Dubai: Effective Rental Strategies and Choosing a Management Company” In Dubai, one of the most dynamically developing regions in the world, the real estate…

In Poland, an 18-year-old Ukrainian ran away from the police and died in an accident, – media

The guy crashed into a roadside pole at high speed. In Poland, an 18-year-old Ukrainian ran away from the police and died in an accident / illustrative…

NATO saw no signs that the Russian Federation was planning an attack on one of the Alliance countries

Bauer recalled that according to Article 3 of the NATO treaty, every country must be able to defend itself. Rob Bauer commented on concerns that Russia is…

The Russian Federation has modernized the Kh-101 missile, doubling its warhead, analysts

The installation of an additional warhead in addition to the conventional high-explosive fragmentation one occurred due to a reduction in the size of the fuel tank. The…

Four people killed by storm in European holiday destinations

The deaths come amid warnings of high winds and rain thanks to Storm Nelson. Rescuers discovered bodies in two separate incidents / photo ua.depositphotos.com Four people, including…

Egg baba: a centuries-old recipe of 24 yolks for Catholic Easter

They like to put it in the Easter basket in Poland. However, many countries have their own variations of “bab”. The woman’s original recipe is associated with…

Leave a Reply

Your email address will not be published. Required fields are marked *