html - 如何使用CSS中的类对问题进行编号

标签 html css css-counter

我已经创建了一个问题列表,并计划在将来添加更多问题。我想使用 CSS 对问题进行编号 例如,每个问题都位于下面给出的部分中。

<section class = "question">The Question</section>

有什么方法可以使用“问题”类自动对它们进行编号吗?

最佳答案

是的,您可以使用 :before 伪元素和 CSS 计数器:

body {
  counter-reset: section;                   /* Set the section counter to 0 */
}
section.question::before {
  counter-increment: section;               /* Increment the section counter*/
  content:  counter(section) ": "; /* Display the counter */
}
<section class="question">The Question</section>
<section class="question">The Question</section>
<section class="question">The Question</section>
<section class="question">The Question</section>

您可以在 https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Counters 阅读有关 CSS 计数器的更多信息。和 http://www.w3.org/TR/CSS2/generate.html#generate.html#counters

关于html - 如何使用CSS中的类对问题进行编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010272/

相关文章:

javascript - 根据父 div 的 Canvas 数量调整 Canvas 大小

javascript - 在点击 td 后尝试获取 td 行文本

css - 使用 CSS 计数器

html - CSS 嵌套有序列表计数器操作

css - 使用 CSS 对 h1、h2、h3 进行编号,并带有文章标签

html - ngRepeat 通过将相同的属性分组而不是另一个库

javascript - AngularJS - 从表单验证/脏中排除字段

GTK 中的 CSS - 在单击按钮时更改背景颜色

html - CSS背景图片网址仅接受完整路径

jquery - 由于较低的表格拉伸(stretch)而固定标题宽度