简体   繁体   English

HTML / CSS自动编号标题?

[英]HTML / CSS autonumber headings?

Is there a way (ideally easy) to make headings and sections autonumber in HTML/CSS? 有没有办法(理想上很容易)在HTML / CSS中制作标题和部分自动编号? Perhaps a JS library? 也许是一个JS库?

Or is this something that is hard to do in HTML? 或者这是HTML中难以做到的事情?

I'm looking at an application for a corporate wiki but we want to be able to use heading numbers like we always have in word processors. 我正在寻找一个企业wiki的应用程序,但我们希望能够像文字处理器一样使用标题编号。

Definitely possible using css counters - just make sure you watch out for browser compatibility...: 绝对可以使用css计数器 - 只需确保注意浏览器兼容性......:

This will make h2 get 1., 2., h3 gets 1.1, 2.1, 2.2 etc... 这将使h2得到1.,2.,h3得到1.1,2.1,2.2等......

<style>
   body{counter-reset: section}
   h2{counter-reset: sub-section}
   h3{counter-reset: composite}
   h4{counter-reset: detail}

   h2:before{
     counter-increment: section;
     content: counter(section) " ";
   }
   h3:before{
     counter-increment: sub-section;
     content: counter(section) "." counter(sub-section) " ";
   }
   h4:before{
     counter-increment: composite;
     content: counter(section) "." counter(sub-section) "." counter(composite) " ";
   }
   h5:before{
     counter-increment: detail;
     content: counter(section) "." counter(sub-section) "." counter(composite) "." counter(detail) " ";
   }
   </style>

As lpfavreau says, it's the same as another question I believe. 正如lpfavreau所说, 这与我相信的另一个问题是一样的

Also note that using css will not change the heading (eg selected text will give you the heading without the numbers). 另请注意,使用css 不会更改标题(例如,选定的文本将为您提供没有数字的标题)。 This may or may not be desirable. 这可能是也可能不是所希望的。 lpfavreau's (accepted) answer will give you the jquery code to modify the heading text. lpfavreau(已接受)的答案将为您提供jquery代码来修改标题文本。

See MDN: Using CSS counters for details. 有关详细信息,请参阅MDN:使用CSS计数器

3rd Party Edit 第三方编辑

I created an example with the css above 用上面的css创建了一个例子

2016 update. 2016年更新。 Please see Stephen's answer below for a proper method in CSS. 请参阅下面的Stephen答案 ,了解CSS中的正确方法。 My answer made sense in 2009 when the browsers and libraries were different. 我的答案在2009年浏览器和库不同时才有意义。 We are now living in a whole new world and the method presented here is outdated. 我们现在生活在一个全新的世界,这里介绍的方法已经过时了。 I'm leaving it for the poor souls that are still living in corporate microcosms made of IE7 and tears. 我将它留给那些仍然生活在由IE7制造的企业微观世界和泪水中的可怜的灵魂。


See this other question if you're wondering how to do it in CSS, the answer might be what you are looking for. 如果您想知道如何在CSS中执行此操作,请查看此其他问题 ,答案可能就是您要查找的内容。 But titel has a good proposition too, depending how your HTML document is made. 但是,根据HTML文档的制作方式,titel也有一个很好的主张。

It should be noted that, as Triptych said in another comment, this is of interest only if it's for an internal tool where you have control over which browsers are used and using CSS is worth it because modifying the HTML would be hard for example. 应该注意的是,正如Triptych在另一篇评论中所说的那样,只有在你可以控制使用哪些浏览器的内部工具时才会感兴趣,并且使用CSS是值得的,因为修改HTML会很困难。 Support of this CSS feature is limited. 支持此CSS功能是有限的。

It would of course be easy to use something like jQuery to do the increment also. 使用像jQuery这样的东西当然也很容易。 Something like this untested snippet: 像这个未经测试的片段:

$(document).ready(function() {
  $('h1').each(function(index) {
    $(this).html((index + 1) + '. ' + $(this).html());
  });
});

Don't forget to include the jquery.js file in your document of course. 当然,不要忘记在文档中包含jquery.js文件。

The simplest method would be Numbered Lists 最简单的方法是编号列表

<ol>
<li> Section
    <ol>
      <li>Nested one</li>
      <li>Nested two</li>
    </ol>
</li>
<li>Section</li>
<li>Section</li>
<li>Section</li>
<ol>

will be something like: 将是这样的:

  1. Section 部分
    I. Nested one I.嵌套的
    II. II。 Nested two 嵌套两个
  2. Section 部分
  3. Section 部分
  4. Section 部分

Could possibly be done either serverside or with JavaScript. 可以在服务器端或使用JavaScript完成。 Don't know about any premade scripts that does it though. 不知道有任何预制脚本可以做到这一点。

Impossible to do with HTML/CSS, at least - unless you manually add all numbers into your headings. 不可能至少使用HTML / CSS - 除非你手动将所有数字添加到标题中。

It is possible to implement auto numbering using HTML itself using ordered lists, and nesting them if necessary. 可以使用有序列表使用HTML本身实现自动编号,并在必要时嵌套它们。 Below there is a link to a live example of this, example I found after a fast search on Google. 下面是一个实际示例的链接,例如我在Google上快速搜索后发现的示例。

http://archive.corewebprogramming.com/Chapter2/Nested-Ordered-Lists.html http://archive.corewebprogramming.com/Chapter2/Nested-Ordered-Lists.html

There is also an possibility to use Unordered Lists and CSS as shown in this example: 还可以使用无序列表和CSS,如下例所示:

http://print.wordpress.com/2006/02/22/css-beautifully-numbered-lists/ http://print.wordpress.com/2006/02/22/css-beautifully-numbered-lists/

Lists do it, why not other elements? 列表这样做,为什么不是其他元素? http://www.w3.org/TR/CSS2/generate.html#scope http://www.w3.org/TR/CSS2/generate.html#scope

<ol>
  <li>Heading 1</li>
  <li>Heading 2</li>
  <li>Heading 3</li>
</ol>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM