CSS“反增量”不递增

时间:2014-12-10 19:02:12

标签: html css css3

关注this tutorial,我正在尝试在<ol>中设置列表项计数器的样式。但是,我的计数器没有递增。

.prog-ol ol {
  counter-reset:li; 
  margin-left:0; 
  padding-left:0;
} 
.prog-ol li {
  position:relative; /* Create a positioning context */
  margin:0 0 6px 2em; /* Give each list item a left margin to make room for the numbers */
  padding:4px 8px; /* Add some spacing around the content */
  list-style:none; /* Disable the normal item numbering */
}
.prog-ol li:before {
  content:counter(li); /* Use the counter as content */
  counter-increment:li; /* Increment the counter by 1 */
  /* Position and style the number */
  position:absolute;
  top:-2px;
  left:-2em;
  -moz-box-sizing:border-box;
  -webkit-box-sizing:border-box;
  box-sizing:border-box;
  width:2em;
  /* Some space between the number and the content in browsers that support generated content but not positioning it (Camino 2 is one example) */
  margin-right:8px;
  padding:4px;
  border-top:2px solid #666;
  color:#fff;
  background:#666;
  font-weight:bold;
  font-family:"Helvetica Neue", Arial, sans-serif;
  text-align:center;
 }
 <ol class="prog-ol">
          <li>Foo</li>
          <li>Bar</li>
          <li>baz</li>
        </ol>

看起来这两行几乎应该照顾它:

content:counter(li); /* Use the counter as content */
counter-increment:li; /* Increment the counter by 1 */

为什么我的计数器不递增?

1 个答案:

答案 0 :(得分:2)

您在.prog-ol ol上定义了一个计数器,它是<ol>类成员元素的prog-ol后代。

从选择器中删除ol

.prog-ol {
  counter-reset:li; 
  margin-left:0; 
  padding-left:0;
} 
.prog-ol li {
  position:relative; /* Create a positioning context */
  margin:0 0 6px 2em; /* Give each list item a left margin to make room for the numbers */
  padding:4px 8px; /* Add some spacing around the content */
  list-style:none; /* Disable the normal item numbering */
}
.prog-ol li:before {
  content:counter(li); /* Use the counter as content */
  counter-increment:li; /* Increment the counter by 1 */
  /* Position and style the number */
  position:absolute;
  top:-2px;
  left:-2em;
  -moz-box-sizing:border-box;
  -webkit-box-sizing:border-box;
  box-sizing:border-box;
  width:2em;
  /* Some space between the number and the content in browsers that support generated content but not positioning it (Camino 2 is one example) */
  margin-right:8px;
  padding:4px;
  border-top:2px solid #666;
  color:#fff;
  background:#666;
  font-weight:bold;
  font-family:"Helvetica Neue", Arial, sans-serif;
  text-align:center;
 }
<ol class="prog-ol">
          <li>Foo</li>
          <li>Bar</li>
          <li>baz</li>
        </ol>

(就此而言,您应该从类名中删除ol。将类名绑定到特定元素并没有多大意义。如果需要,可以将它们与类型选择器结合使用)。