Creating a custom CSS counter-style reminiscent of the toLocaleString function

Is there a way to customize the list counter style to display commas in the thousands place, similar to number.toLocaleString()? Or perhaps even adjust based on locale to show either 1,234 or 1.234?

I have checked the Predefined counter styles but did not find anything that fits this requirement.

The goal is to incorporate CSS animations to create an animated counter like the one demonstrated here: https://codepen.io/wp-asdf123/pen/XWLVPyy. It would be ideal for the output to include commas (or be locale-specific).

It appears that using @counter-style within the

content: counter(num /*, counter-style? */ );
might be the solution, but I'm not entirely clear on how to implement it.

Any assistance on this matter would be highly appreciated. Thank you.

Answer №1

It is almost necessary to use JavaScript in order to achieve this goal. MDN lists all supported descriptors, but none of them allow for achieving this directly or indirectly, especially when only using commas without considering locale.

One potential solution could be to use a fixed set of symbols, but the number of symbols required would likely be too large.

An alternative workaround using pure CSS could involve using 2 (or more) counters to control each 3-digit segment withpad: 3 "0". However, this method has limitations as it will only work if the counter increment is fixed and if the initial count is 0 or a positive number divisible by 10. Below is an example using just 2 counters, where the counter increases by 100 for each list item. This setup only supports numbers from 0 to 9,999.

@counter-style hundres-paded {
  system: extends decimal;
  pad: 3 "0";
}

.item:first-child {
  counter-reset: hundreds 0;
}

.item:not(:first-child) {
  counter-increment: hundreds 100;
}

.item:nth-child(10n+11) {
  counter-reset: hundreds 000;
  counter-increment: thounsands 1;
}

.item::before {
  content: counter(hundreds);
}

.item:nth-child(n+11)::before {
  content: counter(thounsands) "," counter(hundreds, hundres-paded);
}
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></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

Using Angular material to display a list of items inside a <mat-cell> element for searching

Can I use *ngFor inside a <mat-cell> in Angular? I want to add a new column in my Material table and keep it hidden, using it only for filtering purposes... My current table setup looks like this: <ng-container matColumnDef="email"> < ...

Adjusting Font Size of Menu BarORModifying

I'm currently using WordPress to build my website. If you'd like to take a look, here is the link: I'm trying to make the font size smaller to accommodate more menu bars on the site. Any suggestions on how I can achieve this would be great ...

What could be the reason behind the additional gap at the bottom on mobile devices?

I've encountered an issue with my responsive website on the iPhone 11/11 pro where the background is visible at the bottom of the page. After researching, it seems like the CSS height property might be causing the problem. It was suggested to use VH i ...

Guide for positioning buttons in front of an image using HTML and CSS

I need assistance placing buttons in specific positions above an image carousel using HTML and CSS code. Despite researching various resources, I am unable to resolve this issue. This is the HTML implementation: <!-- Image where buttons should be placed ...

How can I alter the background color using Jquery or CSS3?

UPDATE Update: After following Zach Saucier's instructions, I added the specified codes: JS this.onclick = function() { this.style.background = "red"; this.innerHTML = "Bought"; } So the button displays as: <a onclick="javascript:func(this)" ...

Exciting Findings of Grid in Bootstrap 4 Alpha-6

I recently switched to using the latest alpha4 6 version and decided to see what has changed. It seems that I can now use containers, rows, and columns just like in version 3. So, I set up a very basic layout - content > row > col (using col as a c ...

Any tips on how to personalize the loading animation for single pages using CSS and GIFs?

presentLoading(message) { this.loading = this.loadingCtrl.create({ dismissOnPageChange: false, spinner:'hide', content:`<img class="load" src="assets/dual.gif" />`, }); this.loading.present(); } Hello there! I am working with the ...

Is it possible to reference the same PHP file in multiple locations?

Currently, I am working on a web development project where I have created a header.html file to store the common header for all my webpages. This header file is located in the parent directory. Within my header.html file, I have included references to bot ...

Introducing an alternative solution for handling tasks linked to the completion of CSS3 animations

In order to incorporate smooth CSS3 animations into my website, I have implemented the animate.css library created by Dan Eden. One particular scenario involves a loading screen that features a fade-out animation. It is crucial for this animation to comple ...

Responsive CSS Fluid Grid Design Encounters a Tablet Break Point Failure

During the development of a responsive website in Dreamweaver CS6 using the Fluid Grid system, all breakpoints were initially functioning correctly. The site had distinct layouts for desktop, tablet, and mobile devices, each with different resolutions that ...

Setting the second tab as the primary active tab

I am currently working on a script that is well-known, and everything is functioning perfectly. However, I want to change it so that when the page is first opened, it displays the second tab instead of the first one (the first tab being a mail compose tab ...

I'm curious if it's possible to create a scrolling marquee on an SVG path with the use of HTML and CSS?

I am interested in creating a unique effect similar to what is showcased on this website or in the main menu of Colin McRae Rally 2.0. My goal is to generate a path and animate text along it, with the letters wrapping around to the start as they reach the ...

Switch the scroll direction in the middle of the page and then switch it back

Yesterday, while browsing online, I stumbled upon this website and was amazed by the unique scroll direction change from vertical to horizontal mid-page. I'm curious about how they managed to achieve that effect. Does anyone have insight into the pro ...

What could be the reason for the css problems being caused by my flash banner?

My flash banner ad is being displayed on a website as an advertisement at the bottom of the page. Whenever the banner ad is shown, the scroll bar disappears. The following CSS code appears when the flash banner is active: body {margin: 0; padding: 0; over ...

developing a responsive website that can effortlessly adjust to any screen size, whether big or small

As I develop a website on my 13-inch MacBook, I encountered an issue with the background image being too large for my screen. Is there a way to maintain the aspect ratio while designing so that it looks consistent when scaled up or down? I tried using the ...

Place the element at the bottom of the row above

I'm utilizing Angular to retrieve a list of items from the server and exhibit them on the page using: <div class="col-sm-6 col-md-4" ng-repeat="activity in activities"> <img ng-src="[[ act.icon ]]" height="100" alt=""> <h3>[ ...

Segment the progress bar into sections

Struggling with an Angular app, and still new to HTML and CSS, I am attempting to create a unique progress bar. I aim to split each section of the progress bar into two subsections with distinct background colors shown in this image: https://i.sstatic.net/ ...

How to target CSS when a DIV does not have a particular sibling

I am currently dealing with HTML markup that is being generated by a third-party application. I am in the process of styling it to fit my branding, but I have encountered a challenge that I cannot figure out. Most of the time, the HTML structure consists o ...

The size of table td is less than 100%

I'm having trouble getting my td to fill 100% of my table. I have tried using the following code: .table-scroll tbody { display: block; overflow-y: scroll; height: calc(100% - 130px); } Even with the display block property, the td element is s ...

"Looking to add some flair to your website with a

I am trying to figure out how to place a 30x30px no-repeat background image in the top left corner of a 200px by 200px div, positioned 120px from the left and 50px from the top. However, I also want to ensure that the text inside the div is displayed on to ...