The importance of timing in @keyframes animations

I was experimenting with a fun project that involved creating a timer using just HTML and CSS.

But I'm curious, why do I need to add a delay to my 100s animation in order to synchronize it properly with the 10s animation?

.second::before {
  animation: time 100s linear 4.5s infinite;
  /* Why is there a need for delay to sync the animations properly? */
  content: "0";
}
.second::after {
  animation: time 10s linear infinite;
  content: "0";
}
@keyframes time {
  10% {
    content: "1"
  }
  20% {
    content: "2"
  }
  30% {
    content: "3"
  }
  40% {
    content: "4"
  }
  50% {
    content: "5"
  }
  60% {
    content: "6"
  }
  70% {
    content: "7"
  }
  80% {
    content: "8"
  }
  90% {
    content: "9"
  }
}
 <span class="second"></span>

My initial idea was simple: count from 0 to 9 over 100 seconds (one digit every ten seconds), and from 0 to 9 over 10 seconds (one digit every second).

However, without the delay, the 100s animation shows the first digit after about five seconds, then changes digits every 10 seconds. Why does this happen?

You can view an example on CodePen at the following link: oldTimer

Answer №1

In order to ensure that your animation runs smoothly, it is essential to set the keyframes at precise intervals rather than relying on default values.

By adjusting the steps of the animation to occur exactly where you intend them to, you can avoid discrepancies in timing. For example, setting one value for 10% and another for 20% may result in a change happening at the midpoint, which can lead to synchronization issues.

To rectify this, it is recommended to define the keyframes with accuracy in mind:

.second { font-size: 80px; }

.second::before {
animation: time 100s linear infinite;
content: "0";
}
.second::after {
animation: time 10s linear infinite;
content: "0";
}
@keyframes time {
0%, 9.99% {
content: "0";
background-color: blue;
}
10%, 19.99% {
content: "1";
background-color: green;
}
20%, 29.99% {
content: "2";
background-color: tomato;
}
30%, 39.99% {
content: "3"
}
40%, 49.99% {
content: "4"
}
50%, 59.99% {
content: "5"
}
60%, 69.99% {
content: "6"
}
70%, 79.99% {
content: "7"
}
80%, 89.99% {
content: "8"
}
90%, 100% {
content: "9"
}
}
<span class="second"></span>

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

Can a Dashcode Webkit Web app be transformed into traditional HTML and CSS?

I have developed a blog using Dashcode, incorporating HTML, CSS, and Javascript to pull data from an xml file. It's pretty simple... My perspective on this is: 1. JavaScript should be compatible with all browsers (with some variations). 2. I may need ...

What is the best way to minimize the number of files being referenced for compilation?

Is there a way to reference less files in my project without including their styling in the final CSS output? @import(reference) filename.less When I try this, it seems to include the styles from filename.less in the final compiled CSS. Is there a better ...

Protecting against overflow for text inside a container that changes when hovered over

There is text displayed on top of an image within a div container that functions as a link. <a href="https://link.com" style="color:white"> <div class="thumbnail"> <img src="image.jpg" clas ...

What is the best way to avoid overflow of a fixed element when styling with CSS?

I need help ensuring that the inner div, colored red, stays hidden within its parent div, which is blue. I attempted to use overflow: hidden; in CSS but it did not have the desired effect. I am looking for a solution using solely CSS since JavaScript cause ...

Guide to positioning SVG elements behind an image responsively using Tailwind CSS:

I need help figuring out how to position SVG's like in the example image found here. The goal is for the SVG's to maintain their positions even when the screen is resized. You can check out a sample link of my attempt before trying it out. I exp ...

Tips for controlling the placement of divs on a webpage

I need to align 3 divs in a specific way on my webpage. One should be on the left side taking up 25% of the window, one on the right side also taking up 25%, and the last one in the center taking up the remaining space. I'm struggling with how to achi ...

A single pixel border surrounding an odd number of divs

I've been struggling with a persistent issue and I'm determined to find a solution. My goal is to achieve the following design using css: https://gyazo.com/c8ae39ebc4795027ba7c1067a08d3420 Currently, I have an uneven number of divs styled as fo ...

:hover does not activate the animation forwards

Why won't the animation: forwards work on :hover? The animation itself is functioning properly, but the command "forwards" isn't. The Issue: Here's the HTML code snippet: <img src="images/notebook_desk.svg" class="notebook-cover"> ...

Creating a dynamic table layout with Bootstrap

Can someone help me figure out how to make this table responsive using Twitter Bootstrap 4? Here's the JS Fiddle Link for reference. <div class="some-table-section"> <div class="container"> <table class="table some-table"> ...

What is the best way to align the images on the right side of this Bootstrap Footer design?

Is there a way to reduce the gap between the "linkedin" images in my Bootstrap footer template? I've tried replacing the default images on the right side with linkedin icons, but I can't seem to adjust the padding/margin properly. Here's the ...

Mobile Mode Issue with Bootstrap 4 Navbar Example

Currently, I am exploring the Bootstrap material and trying to understand how to integrate their content with a jinja2 file that I am preparing for a django project. Upon attempting to copy and paste their example code (specifically this example http://get ...

Capturing the action phase in Liferay to change the cursor to 'waiting' mode

I'm currently working on a large Liferay project and have encountered a specific issue: Whenever something in the system is loading or processing, I need to change the cursor to a waiting GIF. While this is simple when using Ajax, there are many inst ...

Header with a logo and two navigation options

I am facing a challenge where I need to incorporate two navigation bars and a logo on the left side. The desired layout can be seen in the image below. I am looking for a solution that is both efficient and responsive, ensuring that when the browser size ...

Tips for resizing a flexible circle and a definite rectangle within a stationary holder

My parent container .secondary-nav-wrap has the following CSS: .secondary-nav-wrap { margin: 30px 0; position: relative; } Within it, there is an ordered list (ol) with varying list items (li), and an absolutely positioned div .import-26-as with ...

CSS/HTML: A guide to creating an element that switches to absolute positioning when scrolled past

Just starting out with CSS and HTML here, trying to figure out how to make an element become absolutely positioned once you scroll past it on the page. Take a look at this example for reference: (no need for an account to test). As you scroll down, the b ...

The wonders of CSS flexbox and the power of justify-content

I am working on a layout where I need three flex items (rows) in a flex container, and my requirement is to justify them as space-between... The first row will consist of cloud tags, the second will have a price, and the third will contain a Read more link ...

Starting on the Legacy 1.2.0.RC4 TideSDK: Where to Begin?

I just acquired the legacy version 1.2.0.RC4 from the official website. Now that I have it downloaded, what are the next steps? How can I begin using it? ...

Do mobile CSS/HTML have distinct rules to follow?

I am having trouble displaying a background image in CSS. Additionally, I am unable to set a background color for a div. #mlogo { background-image: url(/mobileimages/2015LOGOFB.jpg); background-size: 100%; background-position: top; backgro ...

Attach to dynamically generated elements by event listeners that bind on mouseover or hover

I am looking to implement a hover effect for dynamically generated items in JavaScript. Specifically, when I hover over an icon element, I want a unique text to appear related to that icon: for (var i = 0; i < items.length; i++) { $('.items&ap ...

Align the text characters vertically within a word or sentence

I currently have a logo that consists of lines and text. I would like to create a similar version using CSS for my website. https://i.sstatic.net/vxS8Z.png My goal is to have the letters "T - S o N" appear in one line with the letter "o" positioned verti ...