Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read the applied CSS-counter value?

Tags:

javascript

css

Say you have a CSS 2.1 counter like

ol {
  counter-reset: section;
  list-style-type: none;
}
li:before {
  counter-increment: section;
  content: counters(section, ".") " ";
}


<ol>
  <li>itemA</li>          <!-- 1     -->
  <li>itemB               <!-- 2     -->
    <ol>
      <li>itemC</li>      <!-- 2.1   -->
      <li id="foo">itemD</li>      <!-- 2.2   -->

(see https://developer.mozilla.org/en/CSS_Counters "nesting counters")

Is there a way to read/get the :before.content ("2.2" in this case) for <li id="foo"> in JavaScript?

Edit: In my case a Mozilla-only solution would suffice. But there really seems to be no way to access this information. At least I didn't find any at https://developer.mozilla.org/en/CSS_Counters ff.

like image 382
VolkerK Avatar asked Feb 10 '09 12:02

VolkerK


People also ask

What is CSS counter-increment?

The counter-increment property increases or decreases the value of one or more CSS counters. The counter-increment property is usually used together with the counter-reset property and the content property. Default value: none.

How do you show a counter in HTML?

Steps to create HTML counter Step 1: Create the simple structure using HTML <div> tags. Step 2: Add CSS to make the counter more attractive. Step 3: To add the small icons (like suitcase, coffee-cup, Smylie, user icon, book and more) with the counter, use the bootstrap cdn link and code.

Why are CSS counters important?

CSS counters let you adjust the appearance of content based on its location in a document. For example, you can use counters to automatically number the headings in a webpage, or to change the numbering on ordered lists.


1 Answers

None that I can think of, no. :before pseudo-elements are not part of the DOM so there is no way to address their content.

You could make a function that scanned the stylesheet's DOM for the :before rules and worked out which rules the browser had applied where, but it would be incredibly messy.

like image 126
bobince Avatar answered Sep 27 '22 21:09

bobince