如何使用CSS自动添加节号(1.2,3.4.1)?

时间:2012-04-26 19:42:25

标签: html css formatting

如何使用CSS自动添加节号(1.2,3.4.1)?

我目前有

h1, h2, h3, h4 {
  font-weight: normal;
}
h1 { font-size: 140%; }
h2 { font-size: 120%; color:#049;}
h3 { font-size: 110%; color:#06C;}
h4 { font-size: 105%; color:#09C;}

如何修改它们以便根据嵌套级别和节标题的出现顺序自动构建1.3,2.4.5等节编号?

...

<h2>heading21</h2>
...

<h3>heading31</h3>
...

<h2>heading22</h2>

应该显示

  1. heading21

    1.1 heading31

  2. heading22

  3. 或类似的东西。

2 个答案:

答案 0 :(得分:14)

嘿,现在你可以使用 CSS反增量属性

就像这样 的的CSS

   body {counter-reset:section;}
    h1 {counter-reset:subsection;}
    h1:before
    {
    counter-increment:section;
    content:"Section " counter(section) ". ";
        font-weight:bold;
    }
    h2:before
    {
    counter-increment:subsection;
    content:counter(section) "." counter(subsection) " ";
    }

<强> HTML **

<p><b>Note:</b> IE8 supports these properties only if a !DOCTYPE is specified.</p>
<h1>HTML tutorials</h1>
<h2>HTML Tutorial</h2>
<h2>XHTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h1>Scripting tutorials</h1>
<h2>JavaScript</h2>
<h2>VBScript</h2>
<h1>Heading </h1>
<h2>One </h2>
<h2>Two</h2>

**

现场演示http://jsfiddle.net/PfcX2/1/

更多信息请点击此处https://developer.mozilla.org/en-US/docs/Web/CSS/counter-increment

答案 1 :(得分:0)

要回答以下问题:“我只有一​​个h2,如何抑制前导1.,请在样式选择器之前更改h2:before,以便h2必须遵循h1:

h1:before > h2:before {
   // your h2 attributes go here
}