More counters in CSS

删除回忆录丶 提交于 2019-12-11 08:06:56

问题


I'm having a problem with CSS counters.

I set up two counters: one indexes headings and other indexes images. However, only one works correctly, the other one (for images) shows only number 1 in all image descriptions. You can see the example here

body {
  counter-reset: figcounter;
  counter-reset: head2counter;
}
.fig:before {
  counter-increment: figcounter;
  content: "Fig. " counter(figcounter)": ";
  font-weight: bold;
}
.figreset {
  counter-reset: figcounter;
}
.head2:before {
  counter-increment: head2counter;
  content: counter(head2counter)" ";
}
.head2reset {
  counter-reset: head2counter;
}
<h1>Article title</h1>
<h2 class="head2">Services</h2>
<img src="http://www.google.com/about/company/images/company-products.png" width="200" />
<span class="fig">Google services</span>
<h2 class="head2">E-mail clients</h2>
<h2 class="head2">Others</h2>
<img src="http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png" width="200" />
<span class="fig">Google logo</span>
<br />
<img src="http://thenextweb.com/wp-content/blogs.dir/1/files/2013/02/google_chrome.png" width="200" />
<span class="fig">Chrome</span>

Do you know how to fix it? If there is only one counter, it works correctly. I want to index images independently on headings.


回答1:


Change from:

body {
      counter-reset: figcounter;
      counter-reset: head2counter;
}

To:

body {
    counter-reset: figcounter head2counter;
}

Why?

Because the counter-reset and counter-increment can be overridden. So if you have to use counter-reset and counter-increment for more than 1 element counter variable, you need to put them on the same declaration for counter-reset and counter-increment, with a space separating them. In this case you only need to put the counter-reset property


body {
  counter-reset: figcounter head2counter;
}
.fig:before {
  counter-increment: figcounter;
  content: "Fig. " counter(figcounter)": ";
  font-weight: bold;
}
.figreset {
  counter-reset: figcounter;
}
.head2:before {
  counter-increment: head2counter;
  content: counter(head2counter)" ";
}
.head2reset {
  counter-reset: head2counter;
}
<h1>Article title</h1>
<h2 class="head2">Services</h2>
<img src="http://www.google.com/about/company/images/company-products.png" width="200" />
<span class="fig">Google services</span>
<h2 class="head2">E-mail clients</h2>
<h2 class="head2">Others</h2>
<img src="http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png" width="200" />
<span class="fig">Google logo</span>
<br />
<img src="http://thenextweb.com/wp-content/blogs.dir/1/files/2013/02/google_chrome.png" width="200" />
<span class="fig">Chrome</span>



回答2:


Hm strange, your fiddle works, if you change your css to:

.fig {
  counter-increment: figcounter;
}
.fig:before {
  content: "Fig. " counter(figcounter) ": ";
  font-weight: bold;
}

Look at the fiddle: https://jsfiddle.net/jddkucs7/2/

But sorry, I have no idea why your fiddle doesn't work. May someone has any suggestions. I would be interested in an explanation too.

Ciao Ralf




回答3:


your counter for the figcounter is not correctly keeping the count because it resets the counter after every span. what you want is to have a parent that needs to keep the count.

So in your example , if you enclose the section where you want the counter not to reset inside a div element , then it will work.

Refer the MDN reference for more understanding and if my explanation was as clear as mud.

here is the snippet,

body {
  counter-reset: figcounter;
  counter-reset: head2counter;
}
.fig:before {
  counter-increment: figcounter;
  content: "Fig. " counter(figcounter)": ";
  font-weight: bold;
}
.figreset {
  counter-reset: figcounter;
}
.head2:before {
  counter-increment: head2counter;
  content: counter(head2counter)" ";
}
.head2reset {
  counter-reset: head2counter;
}
<body>

  <h1>Article title</h1>


  <h2 class="head2 figreset">Services</h2>


  <img src="http://www.google.com/about/company/images/company-products.png" width="200" /> <span class="fig">Google services</span>


  <h2 class="head2 figreset">E-mail clients</h2>


  <h2 class="head2 figreset">Others</h2>


  <img src="http://4.bp.blogspot.com/-JOqxgp-ZWe0/U3BtyEQlEiI/AAAAAAAAOfg/Doq6Q2MwIKA/s1600/google-logo-874x288.png" width="200" /> <span class="fig">Google logo</span>

  <br />

  <img src="http://thenextweb.com/wp-content/blogs.dir/1/files/2013/02/google_chrome.png" width="200" /> <span class="fig">Chrome</span>

</body>

Hope this helps.



来源:https://stackoverflow.com/questions/29126881/more-counters-in-css

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!