Run various CSS animations repeatedly

I recently created a captivating text fade-in, fade-out animation consisting of four lines. Each line elegantly appears one after the other, creating a mesmerizing effect. However, there's now a request to have these animations run in an infinite loop. As someone relatively new to CSS animations, I find myself unsure of how to achieve this continuous loop seamlessly. It seems like I may have approached the setup incorrectly from the start. How can I reconstruct the animation so that all four lines continue to cycle endlessly?

Any suggestions or hints would be greatly appreciated!

PS: For those who prefer to dive into the code, I've included the snippet below and a link to the Fiddle here: https://codepen.io/SchweizerSchoggi/pen/xxKXyyv

PS2: I noticed a similar question posted by another user five years ago, but unfortunately, it went unanswered. This prompted me to pose my query today on this platform, where I was fortunate enough to receive helpful guidance.

body {
  font-size: 62.5%;
  font-family: Arial;
}

.animation-box {
  width: 75%;
  height: 30rem;
  background-color: darkblue;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}

@keyframes fadeInOut {
  0% {
    opacity: 0;
  }
  
  45% {
    opacity: 1;
  }

  100% {
    opacity: 0%;
  }
}

.animation-box h1 {
  position: absolute;
  left: 5%;
  top: 0;
  font-size: 4em;
  color: white;
  font-weight: 400;
}

.first-line,
.second-line,
.third-line,
.fourth-line {
  font-size: 3em;
  position: absolute;
  left: 5%;
  top: 20%;
  opacity: 0;
  color: rgba(200,200,200,0.9);
}

.first-line {  
  animation-name: fadeInOut;
  animation-duration: 5s;  
}

.second-line {  
  animation-name: fadeInOut;
  animation-delay: 5s;
  animation-duration: 5s;
}

.third-line {
  animation-name: fadeInOut;
  animation-delay: 10s;
  animation-duration: 5s;  
}

.fourth-line {  
  animation-name: fadeInOut;
  animation-delay: 15s;
  animation-duration: 5s;
}
<section class="animation-box">
<h1>Fading the lines</h1>
       
<div class="first-line">This is line 1</div>
<div class="second-line">here comes line 2</div>
<div class="third-line">3 is the perfect number</div>
<div class="fourth-line">the final one is 4</div>

</section>

Answer №1

Explanation of the process

1: To determine the animation delay, divide the animation duration by the number of elements.

2: Customizing the keyframe animation is essential based on your specific needs. It's important to have a sense of timing for each element and the gap between them.

3: Include

animation-iteration-count: infinite;
in each individual element.

body {
  font-size: 62.5%;
  font-family: Arial;
}

.animation-box {
  width: 75%;
  height: 30rem;
  background-color: darkblue;
  margin: 0 auto;
  overflow: hidden;
  position: relative;
}

@keyframes fadeInOut {
  0% {
    opacity: 0;
  }

  2% {
    opacity: 0;
  }

  5% {
    opacity: 1;
  }

  17% {
    opacity: 1;
  }

  19% {
    opacity: 1;
  }

  24% {
    opacity: 0;
  }

  80% {
    opacity: 0;
  }

  100% {
    opacity: 0;
  }
}
.animation-box h1 {
  position: absolute;
  left: 5%;
  top: 0;
  font-size: 4em;
  color: white;
  font-weight: 400;
}

.first-line,
.second-line,
.third-line,
.fourth-line {
  font-size: 3em;
  position: absolute;
  left: 5%;
  top: 20%;
  opacity: 0;
  color: rgba(200,200,200,0.9);
  animation-name: fadeInOut;
  animation-iteration-count: infinite;
}

.first-line {  
  animation-duration: 12s;
}

.second-line {  
  animation-delay: 3s;
  animation-duration: 12s;
}

.third-line {
  animation-delay: 6s;
  animation-duration: 12s; 
}

.fourth-line {  
  animation-delay: 9s;
  animation-duration: 12s;
}
<section class="animation-box">
    <h1>Fading the lines</h1>
   
    <div class="first-line">This is line 1</div>
    <div class="second-line">here comes line 2</div>
    <div class="third-line">3 is the perfect number</div>
    <div class="fourth-line">the final one is 4</div>
  </section>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to set the z-index for elements within CSS containers that are utilized for container queries?

I am thrilled about the introduction of container queries in CSS. It's great to see this feature now available across all browsers. However, I have encountered an issue when working with multiple consecutive containers and placing an absolutely positi ...

How to prevent the animation from triggering when changing the theme

Currently, I am developing a game that is similar to the concept of . In my game, I have implemented both dark and light themes along with animations that are triggered when the API sends a response (such as flipping a div and changing the background col ...

Troubleshooting CSS Problems: Issues with background repeating and footer placement

I am currently in the process of transitioning my website to a CSS layout, and so far it's looking good. However, I am facing two specific issues with the page layout that I need help with: 1) The background image of the left-most column is not repea ...

Ways to conceal components upon clicking a different element

Struggling to make this jQuery function properly. I have a form with all fields contained in a div.form-group. The subscribe button is identified by the id subscribe. I'm aiming to hide the form fields when clicked, but my JavaScript doesn't see ...

Show a grid layout with adjustable sizing and elements centered in the middle

When dealing with a layout like the one below, how can we ensure that elements are centered within the wrap container? The margins around the elements should be flexible but at least 14px. .wrap{ display: grid; grid-template-columns: repeat(auto-fi ...

How can you prevent a draggable element from surpassing the bottom of the screen?

I'm dealing with an element that I want to make draggable only along the Y-axis. It needs to be able to go past the top of the screen, but I need to restrict it from going past the bottom of the screen. I recently came across the containment feature i ...

What is the best way to create a dynamic textbox or label within a specific div class?

I need assistance with my dynamic asp.net page where I am generating labels and text boxes in a form. Is there a way to ensure that these elements are generated within a specific div class? Below are the relevant code snippets: for (int i = 0; i < cou ...

What could be causing Typed.js to not function properly in my situation?

Having an issue with typed.js not functioning properly. Here is the code snippet: <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="js/jquery-1.11.3.min.js"></script> <!-- Include all compiled plugins ...

Methods for incorporating rtl functionality into Angular Chosen

I am currently using Angular with Chosen (source) and I am attempting to implement right-to-left (RTL) support. Here is my demo, but despite referencing resources, I am encountering issues. The main problem arises when the body element has a dir="rtl" att ...

Need to update various form fields at once with jquery?

There is a form with fields for firstname, lastname, email, country, along with edit icon and submit/cancel buttons. When the user clicks on the edit icon in the top right corner, all values will be displayed in textboxes and the country will be shown in ...

Differences in SVG rendering between Chrome and Firefox

I am eager to create a diagram on my website using SVG. To ensure the diagram is displayed in full screen, I am utilizing availHeight and availWidth to determine the client screen's height and width, then adjust the scaling accordingly. My current sc ...

Using HTML5, the <section> element can inherit styling from the

This is my first time building a website using HTML5 and I've come across a small problem. Here is the code snippet: <header></header> <section> <header></header> <article></article> <footer></foot ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...

Creating a continuous loop animation with CSS hover state

This piece of code creates an interesting effect when the text is hovered over. The slight shakiness adds a cool touch to it. However, this effect only occurs when the mouse is moved slowly; if the mouse remains stationary, the hover style takes precedence ...

Tips for creating a dynamic header background using custom CSS for my website design

As I embark on creating my very first WordPress website using the Blossom Pin Pro theme, I'm encountering an issue where the background image of the header disappears when adjusting the window width. Click here to visit the site If you have a code s ...

Utilize duplicate named CSS grid rows as a reference

Summary; This method currently works, but it requires adding multiple nth-child selectors for each new person and their answer. I am seeking a solution that can dynamically handle any number of rows using an even and odd selector to simplify the process. ...

Toggling designs with a simple click

I'm currently working on a ReactJS project and I'm trying to figure out how to change the color of a button when it's clicked. I've heard about the Ripple API, but I find it quite complex to use. Can anyone offer some advice on how I ca ...

Developing components through JavaScript

I am attempting to use JavaScript to create an element in my HTML using createElement, but there seems to be an issue in my code. HTML <button class="test_button">I am a test</button> <div class="test"></div> ...

Aligning columns next to one another using flexbox in a centered manner

I am attempting to create a layout with 3 columns, but I want any extra columns beyond a multiple of 3 to be centered instead of aligned left. While I have made progress, the issue I'm facing is that the additional columns are not centered relative t ...

PHP/CSS: Backgrounds for DIVs have been implemented, but they appear to be transparent

Update: Check out the link for some old photos here: I have a PHP file where I am creating divs with images as backgrounds using the code below (within a while-loop): echo ' <div class="pic" style="background:url('.$directory.'/'.$ ...