How can one appropriately utilize the counter() function in conjunction with nested headings and corresponding counter-increment() and counter-reset() methods?

I am encountering an issue with counter-reset() when introducing a div between the initial declaration and the reset.

Initially, I had structured my code like this, with headings directly under the numberedHeadings class:

Now, there is a need to wrap each heading tag in an additional div container like so:

.numberedHeadings {
  counter-reset: heading1counter 0 heading2counter 0 heading3counter 0 heading4counter 0;
}

.numberedHeadings h1.chapterHeading {
  counter-reset: heading2counter 0 heading3counter 0 heading4counter 0;
  counter-increment: heading1counter;
  margin-top: 24px;
  margin-bottom: 12px;
}

.numberedHeadings h1.chapterHeading::before {
  display: inline-block;
  padding-right: 8px;
  content: counter(heading1counter) " ";
}


/* Similar adjustments repeated for h2, h3, and h4 */


/* Example for h2 */

.numberedHeadings h2.chapterHeading {
  counter-reset: heading3counter 0 heading4counter 0;
  counter-increment: heading2counter;
  margin-top: 24px;
  margin-bottom: 12px;
}

.numberedHeadings h2.chapterHeading::before {
  display: inline-block;
  padding-right: 8px;
  content: counter(heading1counter) "." counter(heading2counter) " ";
}
<div class="numberedHeadings">
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading one (should be 1)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h2 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading two (should be 1.1)
    </h2>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading three (should be 2)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h2 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading four (should be 2.1)
    </h2>
  </div>
  <div class="settingsControlGroup">
    <h2 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading five (should be 2.2)
    </h2>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading 6 (should be 3)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading 7 (should be 4)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading 8 (should be 5)
    </h1>
  </div>
</div>

However, upon doing this, I notice that while the counter-increment() is working as expected, the counter-reset() is not being applied.

Answer №1

Please note: The code snippet provided appears to function properly on Edge/Chrome (tested on Windows 10) and Safari (tested on iOS 16). However, it may not work correctly on Firefox (tested on Windows 10).

.....................................................................

Move the counter reset/increment operation up by one level.

The CSS :has selector can be utilized to identify which counter is being manipulated.

.numberedHeadings {
  counter-reset: heading1counter 0 heading2counter 0 heading3counter 0 heading4counter 0;
}

.numberedHeadings .settingsControlGroup:has(h1.chapterHeading) {
  counter-reset: heading2counter 0 heading3counter 0 heading4counter 0;
  counter-increment: heading1counter;
  margin-top: 24px;
  margin-bottom: 12px;
}

.numberedHeadings h1.chapterHeading::before {
  display: inline-block;
  padding-right: 8px;
  content: counter(heading1counter) " ";
}


/* Repeat similar adjustments for h2, h3, and h4 */


/* Example for h2 */

.numberedHeadings .settingsControlGroup:has(h2.chapterHeading) {
  counter-reset: heading3counter 0 heading4counter 0;
  counter-increment: heading2counter;
  margin-top: 24px;
  margin-bottom: 12px;
}

.numberedHeadings h2.chapterHeading::before {
  display: inline-block;
  padding-right: 8px;
  content: counter(heading1counter) "." counter(heading2counter) " ";
}
<div class="numberedHeadings">
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading one (expected output: 1)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h2 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading two (expected output: 1.1)
    </h2>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading three (expected output: 2)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h2 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading four (expected output: 2.1)
    </h2>
  </div>
  <div class="settingsControlGroup">
    <h2 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading five (expected output: 2.2)
    </h2>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading 6 (expected output: 3)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading 7 (expected output: 4)
    </h1>
  </div>
  <div class="settingsControlGroup">
    <h1 id="id-7adcf781-0be8-4536-8dd9-89d4553850cf" class="chapterHeading">
      Heading 8 (expected output: 5)
    </h1>
  </div>
</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

HTML/CSS Pricing structure

As a designer, I am attempting to craft a pricing table with a unique pricing arrangement. Despite my efforts with HTML and CSS, I am struggling to position the "12x" below the "R$" and have both appear next to the price of "219." Here is the pricing layo ...

Issues with Bootstrap Carousel Fade Transition and Button Controls inoperable

I am currently exploring the creation of a carousel on bootstrap that transitions smoothly between three images (specifically 3 'gorilla' jpgs). However, I am encountering some challenges while learning HTML. Challenge 1: The carousel is not tra ...

Troubleshooting React child problems in TypeScript

I am facing a coding issue and have provided all the necessary code for reference. Despite trying numerous solutions, I am still unable to resolve it. export class JobBuilderOptimise extends React.Component<JobBuilderOptimiseProps & JobBuilderOptim ...

How can I align a single button to the left side while positioning the rest of the elements to the right in Bootstrap 4?

I am attempting to align the B1 button on the left side and have the remaining elements positioned on the right side. Below is my code snippet using Bootstrap 4.1: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/b ...

Trouble with CSS3 Menu Layout and Gradient Display Issues

My task is to replicate this menu: I'm having trouble creating the gradient. Completed Why can't I see it in my code? http://jsfiddle.net/Vtr6d/ Here's what I currently have: CSS: .mainOptions{ float:left; margin:0 20px 0 20px; ...

What is the best way to retrieve a particular div element from a webpage?

I'm having trouble retrieving a specific div element (specifically with the attribute id="vung_doc") from a website. Instead of getting just that element, I seem to be getting almost every element on the page. Any ideas on what could be causing this i ...

What is the best way to define the active <li> tab using JQuery?

Initially, my HTML code includes a set of <li> elements that I want to dynamically add to the existing <ul>. <ul class="tabs" id="modal_addquestion_ul"> <li class="tab-link current" data-tab="multipleChoice">Multiple Choice< ...

Manipulating layout with html5 VIDEO controls

I'm having an issue with the HTML5 video element. When I don't use "Controls," it displays with the border-radius as I specified. https://i.stack.imgur.com/KMEeJ.png However, when I add controls (which I need), an ugly border appears and overri ...

Tips for aligning spin.js spinner in the center of a bootstrap 3 modal?

I'm attempting to showcase and center the spin.js spinner within a Bootstrap 3 modal. Despite successful implementation in IE and FF using the code below, I am encountering issues in Chrome, Safari Desktop, Chrome IOS, Safari IOS. The problem appears ...

What is the best way to create a timer using JavaScript?

I'm looking for a reverse timer to track session timeout. I found a code on codepen that works as a clockwise timer, but my attempts to make it counterclockwise have failed. Can someone please suggest a solution? I want the timeout to be either 1 hour ...

The table row's background image splitting into two halves

I am working on a table row class called logo, which has specific attributes. .logo { background: url("img/logo.png") no-repeat left top !important; } Currently, I have successfully placed an image at the top left of every row. However, my goal is ...

Troubleshooting a glitch with passing a variable to a PHP script using AJAX

Explanation of the page functionality: When the quiz php page loads, a user can create a score using a function in quiz.js. This score is then stored in a variable score within quiz.js Once the score is generated, the user must click a button to move on ...

Once the database fetches and displays 500 results, the HTML/CSS formatting begins to

On my local webserver, I have a page running off SQLite as its database. Since it is used locally and loads results quickly, displaying all of them on one page isn't a concern. However, I'm facing an issue where the formatting goes awry after 500 ...

Dynamic Cell Class Assignment in Ag Grid

My Div's dimensions can change based on user interaction, with the div containing an ag-grid component. Initially, the div/grid loads in a compressed size, so I've applied specific classes (like small font-size, height, padding, etc.) to eliminat ...

Having trouble getting the sass lighten functions to work properly with a variable

Currently incorporating SASS into my project, Below is the snippet of my variable code :root, [data-theme="default"] { --text-color: #383143; } $text-color: var(--text-color); Utilizing the variable with a lighten function as shown below, body { ...

How can I delete the global styles defined in _app.js for a particular component in Next.js?

While working with NextJs and TailwindCSS, I encountered an issue where all the extra styles in my globals.css file were causing some trouble. Although it is recommended to import it at the top level in the _app, I tried moving it down to the "layout" comp ...

Struggling to properly position HTML5 Slider result

Apologies for any confusion in how I'm presenting this question. What I am aiming for is to have the "output" display right next to the slider, rather than below it. To see the issue: visit alainbruno.nl/form.html As I am just beginning with HTML5, ...

"Clicking on a hash link will refresh the current page

I have a snippet of code embedded on external websites that loads HTML, CSS, and JavaScript using a <script> tag. Within the code is a JavaScript function that triggers when a specific link is clicked: <a href="#">?</a> If there are Ja ...

This webpage is optimized for all browsers, with the exception of the Android Browser that may display fonts with larger sizes

When developing my webpage, I made sure it was compatible with all major browsers and even optimized it for mobile browsers. However, I overlooked testing the page on Android browser (specifically Android 4.2) and encountered some issues. The viewport set ...

Issues with hidden overflow and height attributes not functioning correctly in Internet Explorer 9 when using the DOCTYPE HTML standards mode

I have a <div> with overflow:hidden and height:100% that adjusts the div contents to fit the window's height on Chrome and Safari. However, in IE9, the div does not respect the styles of overflow:hidden and height:100%, causing it to expand to ...