Discover the best way to apply identical @keyframes and @property to multiple div elements!

The CSS counter presented in this example (https://codepen.io/bgbos/pen/pobZvwJ) consists of 4 divs, each with a different initial value. However, at the end of the animation, only one value appears instead of each div's respective initial value.

Here is the original code where only one value is working correctly, utilizing CSS and HTML:

@property --num {
    syntax: "<integer>";
    initial-value: 984;
    inherits: false;

}

#twitter {
    animation: counter 3.9s infinite alternate ease-in-out;
    animation-iteration-count:1;
    counter-reset: num var(--num);
  
}
#twitter::before {
    content: counter(num);
}

@keyframes counter {
    from {
        --num: 0;
    }
    to {
        --num: 100;
    }
}



@property --num {
    syntax: "<integer>";
    initial-value: 402;
    inherits: false;

}

#instagram {
    animation: counter 3.9s infinite alternate ease-in-out;
    animation-iteration-count:1;
    counter-reset: num var(--num);

}
#instagram::before {
    content: counter(num);
}


@property --num {
    syntax: "<integer>";
    initial-value: 254;
    inherits: false;

}

#facebook {
    animation: counter 3.9s infinite alternate ease-in-out;
    animation-iteration-count:1;
    counter-reset: num var(--num);

}
#facebook::before {
    content: counter(num);
}



@property --num {
    syntax: "<integer>";
    initial-value: 610;
    inherits: false;

}

#youtube {
    animation: counter 3.9s infinite alternate ease-in-out;
    animation-iteration-count:1;
    counter-reset: num var(--num);

}
#youtube::before {
    content: counter(num);
}
<div id="twitter" ></div>
<div id="instagram" ></div>
<div id="facebook" ></div>
<div id="youtube" ></div>

I attempted to make edits on https://codepen.io/CarterLi/pen/NWNJvPE, but encountered some challenges as I am still a beginner. Any help provided will be greatly appreciated.

Answer №1

To simplify your code and ensure the value is inside the element, you can use the following approach:

#twitter,
#instagram,
#facebook,
#youtube{
  animation: counter 3.9s  ease-in-out;
  counter-reset: num var(--num);
}

#twitter::before,
#instagram::before,
#facebook::before,
#youtube::before{
  content: counter(num);
}

#twitter { --num:984; }
#instagram { --num:402; }
#facebook { --num:254; }
#youtube { --num:610; }

@property --num {
  syntax: "<integer>";
  initial-value: 0;
  inherits: false;
}

@keyframes counter {
  from {
    --num: 0;
  }
}
<div id="twitter" ></div>
<div id="instagram" ></div>
<div id="facebook" ></div>
<div id="youtube" ></div>

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

How come my gifs appear tiny even though I've adjusted their width to 32% each?

I am trying to display three gifs in a row, each taking up one-third of the width of the page. However, when I preview the page, the gifs appear tiny. I have set the divs to 32% each and the gifs should fill 100% of their respective div. Here is the code s ...

Changing the scroll ball's height using CSS

Can the scrollbar height be adjusted in this section? I've tried using overflow-y:auto, but it seems to start from the header and extend all the way to the bottom. When I scroll, the header moves along with it. Header needs fixing Scrollbar height a ...

Utilizing a Custom Validator to Compare Two Values in a Dynamic FormArray in Angular 7

Within the "additionalForm" group, there is a formArray named "validations" that dynamically binds values to the validtionsField array. The validtionsField array contains three objects with two values that need to be compared: Min-length and Max-Length. F ...

Can a React single-page site be exported to HTML format?

I am currently working on a one-page web application using React and materialize-css. My goal is to export it as static HTML and CSS, allowing for easy editing of the HTML for prototyping purposes. Is there a way to export at least a snapshot of the curren ...

Automate the process of launching the <select> picker through programming

In an Android WebView, I need a way to programmatically trigger the opening of the select element picker provided below. <select id="select1" multiple="multiple"> <option value="MDO1" selected="selected">MDO 1</option> <option val ...

How do I disable scrolling while the animation is running in CSS?

Is there a way to disable scrolling while animating in CSS without using overflow:hidden on the body tag? ...

Verify if a personalized angular directive is able to display or conceal an element

I have a custom directive that toggles the visibility of an element based on the value of another service. I attempted to write a test to verify if the directive functions as intended. Initially, I used the ":visible" selector in my test, but it consistent ...

Silky animations in kinetic.js (html5 canvas)

I am seeking a better grasp on kinetic.js animation. Recently, I came across a tutorial at . After experimenting with the code provided, I managed to create an animation that positions my rectangle at x coordinate 100. However, I am struggling to achieve s ...

Selecting multiple items from a grid using the Ionic framework

I am looking to create a grid of category images in Ionic framework, where users can select multiple categories and send their values to the controller. However, I'm unsure about how to make this happen with Ionic framework. Below is the view I curre ...

Add or remove a class when a button is clicked

I am trying to achieve the functionality where upon clicking a button, one class is removed from a div and another is added. However, I have been unsuccessful so far in making it work. <div class="hiddennav displaynone"> <ul> <?php wp ...

Incorrect positioning of AnyChart within a reusable component (Angular version 9.1.3, Bootstrap 4.4.1, Anychart version 8.7.1) causing display issues on the DOM

I have created a test dashboard featuring two Bootstrap cards, each containing an Anychart column chart. The primary objective is to experiment with reusable components. For those interested, here is the code link on Stackblitz Upon running the code, my ...

After clicking in the Firefox browser, the Window.open function is unexpectedly showing [object Window]

I have added an anchor link that, when clicked, opens a new window with the specified link. Below is the code I used for this purpose: <a href="javascript:window.open('https://www.remax.fi/fi/kiinteistonvalitys/tietosuojaseloste/', &apos ...

What is a reliable method to retrieve the text from the current LI if all LI elements in the list share the

I'm encountering an issue with retrieving the text from LI elements because I have 10 list items and they all have the same class name. When I attempt to fetch the text using the class name or id, I only get the text of the last item. Here is my code ...

Avoid having the browser automatically move focused elements that are navigated to using the tab key

When moving through form elements or anchors using the tab key (and shift + tab), the browser automatically scrolls to the focused element. If the element is not visible due to being part of overflow content with a hidden overflow setting, the container&ap ...

What is the best way to resize a Bootstrap 4 form to perfectly fit the screen width in mobile view?

I need help with a Bootstrap 4 form that I want to adjust to fit the viewport of mobile users. Currently, mobile users see this: https://i.sstatic.net/JjRa8.png The form inputs do not fill the width of the screen, and I want them to take up the full wid ...

Is there a way for me to access the response from the PHP file I'm calling with Ajax?

I am just starting to explore ajax and jquery, and I recently found a code online that I'm tweaking for my own use. However, I am struggling with how to handle responses from PHP in this context. The current setup involves ajax POSTing to a php page ...

Interacting with HTML5 canvas using object events

How can I assign an event handler to an element created on the HTML5 Canvas without having to manually track specific coordinates? ...

Trouble with the mobile layout - styling with CSS

Having an issue with the mobile view CSS. Visit: Looking to shift the navigation button to the right and widen the logo without changing its height. Any assistance would be appreciated. Thank you, ...

Is there a way to repurpose a function to work with both ids and classes?

My current code is affecting all elements instead of just the intended one. I've experimented with classes and ids, ruling out those as potential issues. I'm hoping for my JavaScript to target only the selected element, not all of them. Check ou ...

Saving data to a file using PHP

I've been working on a tracking pixel project, aiming to log the IP address of the requester along with some other information to an HTML file. However, I've encountered an issue where the HTML file doesn't get created and even if I manually ...