首页 > 解决方案 > 我可以对具有 start 属性值的有序列表进行样式编号吗?

问题描述

这是问题所在:

我有一个具有如下属性的ol li有序列表:start

    .custom {
      margin: 0;
      padding: 0;
      list-style-type: none;
    }
    
    .custom li {
      counter-increment: step-counter;
      margin-bottom: 10px;
    }
    
    .custom li::before {
      content: counter(step-counter);
      margin-right: 5px;
      font-size: 80%;
      background-color: rgb(0,200,200);
      color: white;
      font-weight: bold;
      padding: 3px 8px;
      border-radius: 3px;
    }
    <ol start="6" class="custom">
      <li>This is the sixth item</li>
      <li>This is the seventh item</li>
      <li>This is the eighth item</li>
      <li>This is the ninth item</li>
      <li>This is the tenth item</li>
    </ol>

我在浏览器上得到以下输出:

列出 5 个项目; 第一个有数字 1,但应该有数字 6

是否可以使用属性中list-style的值而不是序列化有序列表上的编号?但是,没有 JavaScript 可以用于此。start1

标签: htmlcsshtml-listssemantic-markup

解决方案


您可以使用您设置的 CSS 变量来模拟这一点,而不是start使用它来重置计数器。出于语义目的,您还可以保留start属性。

.custom {
  margin: 0;
  padding: 0;
  list-style-type: none;
  counter-reset: step-counter calc(var(--start) - 1);
}

.custom li {
  counter-increment: step-counter;
  margin-bottom: 10px;
}

.custom li::before {
  content: counter(step-counter);
  margin-right: 5px;
  font-size: 80%;
  background-color: rgb(0, 200, 200);
  color: white;
  font-weight: bold;
  padding: 3px 8px;
  border-radius: 3px;
}
<ol style="--start:6" start="6" class="custom">
  <li>This is the sixth item</li>
  <li>This is the seventh item</li>
  <li>This is the eighth item</li>
  <li>This is the ninth item</li>
  <li>This is the tenth item</li>
</ol>


推荐阅读