html - Css 计数器输出标记和伪元素之前的差异

标签 html css

我正在经历这个link要了解有关计数器的更多信息以及嵌套计数器的工作原理,

我有如下的 css 和 html

<style>
   ol {
      counter-reset: my-counter 0;
      list-style-type: none;
   }
   li::before {
     content: counters(my-counter, '.'); 
     counter-increment: my-counter;
   }
</style>

用html作为

<ol>
  <li> First
     <ol>
        <li> Eleven </li>
        <li> Twelve </li>
     </ol>
  </li>
  <li> Second
    <ol>
       <li> Twenty-one </li>
       <li> Twenty-two </li>
    </ol>
  </li>
</ol>

在这里,我得到了预期的内容,例如 11.1,但是将 before 更改为 marker 伪元素,即 li::marker 给出类似 00.0 的值。

虽然当我只使用这个 css 时,输出符合预期

   li::marker {content: counters(list-item, '.') ' ';}

我不明白为什么 before 和 marker 伪元素为此列表生成不同的输出。

最佳答案

此问题与 ::marker 中允许的属性有关。 content 是允许的,但不是 counter-increment 所以它可以工作但不会增加计数器。

如果您将增量移动到 li 它会起作用:

ol {
  counter-reset: my-counter 0;
  list-style-type: none;
}

li::marker {
  content: counters(my-counter, '.');
}

li {
  counter-increment: my-counter;
}
<ol>
  <li> First
    <ol>
      <li> Eleven </li>
      <li> Twelve </li>
    </ol>
  </li>
  <li> Second
    <ol>
      <li> Twenty-one </li>
      <li> Twenty-two </li>
    </ol>
  </li>
</ol>

有关允许属性的更多详细信息,请参阅规范:https://www.w3.org/TR/css-lists-3/#marker-properties

关于html - Css 计数器输出标记和伪元素之前的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67847310/

相关文章:

html - 如何下载单个网页以供离线查看?

javascript - 将文本框的值更改为其在可排序选项卡中的当前顺序

html - 当它旁边的 div float 时,div 的填充不起作用?

javascript - 如何为 apexcharts y 轴添加背景颜色?

javascript - 如何在 Javascript 中编写\9 IE8 hack

html - :root imported CSS not working in safari and IE 11

javascript - 自动将所有链接从一个基本 URL 重定向到另一个 URL(HTML、JS)

javascript - 程序无法正常运行

css - 如何在悬停时正确定位 SVG 符号路径?

html - 响应式中型屏幕(平板电脑)的 Bootstrap 问题