Tips for saving the latest counter value in CSS and showcasing it as content in HTML

Here is an example of how my CSS code is structured:

.page-number:after { counter-increment: page; }

If we were to display 6 pages, it would look something like this:

.page-number:after { 
   content: 'Page ' counter(page) ' of';
}

I am trying to figure out a way to store the final count (in this case, 6) and have it displayed after each item. Do you think there is a solution for this?

ul {
  list-style: none;
  counter-reset: page;
  padding: 0;
}
.page-number {
  counter-increment: page;
}
.page-number:before {
  content: 'Page ' counter(page) ' of'
}
<p>I would like to utilize counters to print '6' after every item. For instance, page 1 of 6 </p>
<ul>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
</ul>

The .page-number element has a fixed position, ensuring it is displayed on all printed pages.

Update:

@media print {
body { margin: 0; }
thead { counter-reset: pages; }
.page-number:before { counter-increment: pages; content: " "; }
.page-number:after { counter-increment: page; }
.page-number:after { content: "Page " counter(page) " of " counter(pages); }
}

Visit this link for a live example.

Answer №1

Maybe you're looking for something along these lines:

body {
  counter-reset: page;
}

.page-number:after { 
  counter-increment: page;
  content: counter(page);
  visibility: hidden;
}

.page-number:last-child:after { 
  visibility: visible;
}
<div id="wrapper">
  <div class="page-number">test</div>
  <div class="page-number">test</div>
  <div class="page-number">test</div>
  <div class="page-number">test</div>
  <div class="page-number">test</div>
  <div class="page-number">test</div>
</div>

To see the counter increment, you need to display it. That's why I adjusted the visibility.

Answer №2

To display your HTML in the following way: 1. Example 2. Example 3. Example

you can achieve this by:

<div class="example">
  <h3>List Item</h3>
  <h3>List Item</h3>
  <h3>List Item</h3>
</div>
.example {
  counter-reset: count;
}
.example h3:before {
  counter-increment: count;
  content: count". ";
}

Answer №3

Let's explore a creative way to achieve this using only CSS:

1) Insert a single element following the list of pages to hold the total number of pages.

2) Utilize the CSS element() function, which is currently part of the Images Module Level 4 draft specification and has been supported by firefox since version 4, to seamlessly duplicate its content as a background for other elements.

Quoting from MDN:

The CSS function element() defines a value that originates from any HTML element. This image dynamically updates, reflecting changes made to the underlying HTML element automatically in the CSS properties utilizing its output.

Interactive code snippet (Best viewed in Firefox)

ul {
  list-style: none;
  counter-reset: page;
}
.page-number {
  counter-increment: page;
  padding-right: 0.3em;
  float: left;
  clear: left;
}
.page-number:before {
  content: 'Page ' counter(page) ' of'
}
.page-number ~ .page-number {
  background:-moz-element(#counterTotal) no-repeat right center;
  padding-right: 0.8em;
}
.copy:before {
  content: counter(page);
}
<ul>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
  <li class="page-number"></li>
</ul>
<span id="counterTotal" class="copy"></span>


Note: I'm presenting this approach as a demonstration of how theoretically achieving this task solely with CSS is viable (albeit requiring an extra element). For practical purposes, employing a javascript solution would be advisable.

Answer №4

Give this a shot

.page-number:after
    {
       counter-increment: page;
       content: counter(page) "/" counter(pages);
    }

Answer №5

Recently, I had to develop something similar and came up with a brute-force solution.

Utilizing SASS:

.page {
  counter-increment: page;

  @for $i from 1 through 100 {
    &:nth-last-child(#{$i})::after,
    &:nth-last-child(#{$i}) ~ &::after {
      content: "Page " counter(page) " of #{$i}.";
    } 
  }
}

The CSS output is quite extensive due to the nature of the code:

.page {
  counter-increment: page;
}
.page:nth-last-child(1)::after, .page:nth-last-child(1) ~ .page::after {
  content: "Page " counter(page) " of 1.";
}
.page:nth-last-child(2)::after, .page:nth-last-child(2) ~ .page::after {
  content: "Page " counter(page) " of 2.";
}
.page:nth-last-child(3)::after, .page:nth-last-child(3) ~ .page::after {
  content: "Page " counter(page) " of 3.";
}
/* and so on… */

Answer №6

You have the ability to utilize "CSS Counters" in your design.

Here is an example:

body {
    counter-reset: section;
}

h2::before {
    counter-increment: section;
    content: "Section " counter(section) ": ";
}
<!DOCTYPE html>
<html>
<head>

</head>
<body>

<h1>Using CSS Counters:</h1>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h2>JavaScript Tutorial</h2>

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>

</body>
</html>
Source: https://www.w3schools.com/css/css_counters.asp

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

There is an issue with the navigation keys not functioning correctly within the cellrenderer input boxes in ag grid

Struggling with an autocomplete input box within the cell renderer component in an ag grid cell. When attempting to use the left/right navigation keys, the input does not move inside the box and instead exits the cell abruptly. Similarly, navigating down ...

jQuery plugin stops functioning properly following the use of the jQuery display block method

In my project, I am exploring the use of divs as tabs with jQuery. Within these divs, I also want to incorporate another jQuery plugin. Currently, I have manually created these div tabs using jQuery and set the default style for second and subsequent divs ...

Why are the items I have inserted inside my <div> tag not appearing on the screen?

What could be simpler than this: {this.state.dash ? <div className='dashWrapper slide'> HELLO </div> : null} I've tried everything, but nothing seems to show up inside the div. ...

The div inside an iframe is not displaying the desired background color as intended from the parent page

The code snippet below is included in the <head> section of the main page: <script type="text/javascript"> $(document).ready(function () { $('#innerframe').load(function () { $(this).contents().find($(".TitleB ...

How to send variables to a function when a value changes in TypeScript and Angular

As someone who is new to Angular and HTML, I am seeking assistance with the following code snippet: <mat-form-field> <mat-select (valueChange)="changeStatus(list.name, card.name)"> <mat-option *ngFor="let i of lists"> {{i.name}} ...

I am encountering the error message "The requested resource does not contain the Access-Control-Allow-Origin header."

After going through all the possible answers and attempting to add header("Access-Control-Allow-Origin : *"); to the beginning of my asp page, I still couldn't resolve the issue. jQuery(document).ready(function(){ $.post('htt ...

Ways to adjust the number of columns in a div using Bootstrap for varying screen sizes of larger devices

https://i.sstatic.net/1nHlm.pngI am new to coding and exploring html, css, and bootstrap. Is it possible to assign varying numbers of columns to a div based on different resolution ranges for larger devices? Specifically looking at the "lg" range which co ...

What is the best way to position a box in the center using CSS?

I have constructed a container using div and included 4 additional div elements inside: HTML: <div className='main__container'> <div className='item'>Image</div> <div className='item'>N ...

Tips for applying/styling "All Items Center" and "Space Between Items" in CSS and ReactJS

justify-content: space-evenly; seems to be working correctly, but the desired output is not achieved I am aiming for something like this: https://i.stack.imgur.com/iQoWK.png However, this is what I currently have: https://i.stack.imgur.com/1PxGR.png ...

The bold horizontal line in HTML tables is a bit unusual and is caused by the border-collapse property

Edit 1: The strange issue appears to be related to the border-collapse property in CSS, as it can be resolved by using border-spacing: 0px. However, the question remains, why does border-collapse result in this behavior? It seems to be connected to scaling ...

What is the process for updating the background color of the header in ngx datatable?

I am looking to change the background color of the table header to blue. This is the HTML code I have: <ngx-datatable class="material" [rows]="rows" [columnMode]="'force'" [headerHeight]="50" [footerHe ...

The Perfect Scrollbar feature is not functioning properly in conjunction with the accordion menu

I am having some trouble with implementing the Perfect Scrollbar alongside an accordion slider menu. The scrollbar is not working as expected, and you can view the fiddle here. On initial page load, the scrollbar only appears for the first sub-menu aft ...

Is it possible to determine the height of a div after it has been rendered using the .resize method?

In my current structure, <div id="A"> <div id="A1"> <div id="B1"></div> <div id="B2"></div> </div> <div id="A2"></div> </div> A2 and B2 contain tables, while B1 has 4 checkboxes. I&a ...

Is it possible to craft poetic lines with indents in HTML using blockquotes, pre, or dd tags?

I am struggling to format a few lines of text with indentations using HTML. Here is an example I found: HERE Below is my attempt: <p><code>"Knowing is not enough. We</code></p> <p><blockquote><code>must apply. Wi ...

Fade in an image using Javascript when a specific value is reached

Here's the select option I'm working with: <div class="okreci_select"> <select onchange="changeImage(this)" id="selectid"> <option value="samsung">Samsung</option> <option value="apple">App ...

How can HTML text be highlighted even when it's behind a transparent div?

In my React application, I am facing an issue with a transparent div that serves as a dropzone for draggable elements. This div covers text and also contains children elements that may have their own text content. <Dropzone> <SomeChild> . ...

What is the best way to turn off autocorrect in a textarea on IE11 without turning off spellcheck?

In my experience, disabling the spellcheck attribute seems to solve the auto-correct issue, but it also eliminates the underlining of misspelled words. <textarea id="TextArea1" spellcheck="false"></textarea> I prefer to keep spellcheck enabl ...

Trouble with integrating HTML5 canvas from an external JavaScript file

Having trouble with storing canvas js in an external file. If the javascript responsible for drawing on the canvas is included in the html header, then the rectangle is displayed correctly. Here is the working html (javascript in html header): <!DOCT ...

Determine if a paragraph contains a specified value using jQuery

I need to only select the checkboxes that do not have a value in the paragraph. Some mistake seems to be causing all the checkboxes to be marked when I click the button (Value > 0). Can you spot where I went wrong? $("input[id*='btnValue']" ...

The dilemma between Nuxt Js global CSS and Local CSS

Currently, I am in the process of developing a Nuxt application utilizing an admin template. While I am well-versed in Vue.js, I am finding the aspect of loading assets within Nuxt.js to be a bit perplexing. I am in the process of converting the admin temp ...