如何使用CSS计数器在每个子元素之前添加索引号?

huangapple go评论58阅读模式
英文:

How to use CSS counter to prepend the index number before every children?

问题

我有一个如下的DOM结构:

<div>
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
</div>

<div>
<div>Tiger</div>
<div>Monkey</div>
<div>Fox</div>
</div>

如何使用CSS计数器使其呈现为:

1.A
2.B
3.C
4.D

1.Tiger
2.Monkey
3.Fox

我尝试了以下代码:

div > div:before {
  content: counter("div > div");
}

但它失败了。

英文:

I have a DOM like this:

&lt;div&gt;
&lt;div&gt;A&lt;/div&gt;
&lt;div&gt;B&lt;/div&gt;
&lt;div&gt;C&lt;/div&gt;
&lt;div&gt;D&lt;/div&gt;
&lt;/div&gt;


&lt;div&gt;
&lt;div&gt;Tiger&lt;/div&gt;
&lt;div&gt;Monkey&lt;/div&gt;
&lt;div&gt;Fox&lt;/div&gt;
&lt;/div&gt;

How do I use CSS counter to let it rendered as:

1.A
2.B
3.C
4.D

1.Tiger
2.Monkey
3.Fox

I have tried this:

div &gt; div:before {
  content: counter (&quot;div &gt; div&quot;);
}

But it fails.

Code snippet:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

div&gt;div:before {
  content: counter(&quot;div &gt; div&quot;)
}

;

<!-- language: lang-html -->

&lt;div&gt;
&lt;div&gt;A&lt;/div&gt;
&lt;div&gt;B&lt;/div&gt;
&lt;div&gt;C&lt;/div&gt;
&lt;div&gt;D&lt;/div&gt;
&lt;/div&gt;


&lt;div&gt;
&lt;div&gt;Tiger&lt;/div&gt;
&lt;div&gt;Monkey&lt;/div&gt;
&lt;div&gt;Fox&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 2

你对counter("div>div")的使用是不正确的。如果给容器添加一个类,那么就不需要div>div。

.container div::before {
  counter-increment: section; /* 将section计数器的值递增1 */
  content: counter(section) ". "; /* 在每个嵌套div的内容前显示计数器和一个点 */
}

.container > :first-child {
  counter-reset: section;
}
<div class="container">
  <div>A</div>
  <div>B</div>
  <div>C</div>
  <div>D</div>
</div>

<div class="container">
  <div>Tiger</div>
  <div>Monkey</div>
  <div>Fox</div>
</div>
英文:

Your use of counter("div>div") was incorrect

If you give the container a class, then you do not need the div>div

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container div::before {
  counter-increment: section; /* Increment the value of section counter by 1 */
  content: counter(section) &quot;. &quot;; /* Display the counter and a dot before the content of each nested div */
}

.container &gt; :first-child {
  counter-reset: section;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div&gt;A&lt;/div&gt;
  &lt;div&gt;B&lt;/div&gt;
  &lt;div&gt;C&lt;/div&gt;
  &lt;div&gt;D&lt;/div&gt;
&lt;/div&gt;


&lt;div class=&quot;container&quot;&gt;
  &lt;div&gt;Tiger&lt;/div&gt;
  &lt;div&gt;Monkey&lt;/div&gt;
  &lt;div&gt;Fox&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

    .container{
       counter-reset: my-counter;
    }

    div > div::before {
       counter-increment: my-counter;
       content: counter(my-counter) ". ";
    }
英文:

Give container divs a class to reset it between lists and initialize counter in css.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.container{
   counter-reset: my-counter;
}

div &gt; div::before {
   counter-increment: my-counter;
   content: counter(my-counter) &quot;. &quot;;
}

<!-- language: lang-html -->

&lt;div class=&quot;container&quot;&gt;
    &lt;div&gt;A&lt;/div&gt;
    &lt;div&gt;B&lt;/div&gt;
    &lt;div&gt;C&lt;/div&gt;
    &lt;div&gt;D&lt;/div&gt;
&lt;/div&gt;
    
    
&lt;div  class=&quot;container&quot;&gt;
    &lt;div&gt;Tiger&lt;/div&gt;
    &lt;div&gt;Monkey&lt;/div&gt;
    &lt;div&gt;Fox&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 16:27:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定