HTML / CSS autonumber headings?
Asked Answered
O

7

18

Is there a way (ideally easy) to make headings and sections autonumber in HTML/CSS? Perhaps a JS library?

Or is this something that is hard to do in 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.

Ollie answered 11/2, 2009 at 3:45 Comment(0)
C
3

2016 update. Please see Stephen's answer below for a proper method in CSS. My answer made sense in 2009 when the browsers and libraries were different. 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.


See this other question if you're wondering how to do it in CSS, the answer might be what you are looking for. But titel has a good proposition too, depending how your HTML document is made.

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. Support of this CSS feature is limited.

It would of course be easy to use something like jQuery to do the increment also. 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.

Cannibal answered 11/2, 2009 at 4:5 Comment(4)
jQuery it will have to be - the CSS counters idea looks nice but won't work since our IT group only supports IE7 and only plans to upgrade to IE8. FF, Safari, Opera, etc are frowned up (but users can install them).Ollie
Note that this approach will change the header. Using css will leave the header name without the number.Nadaha
Instant downvote for any javascript used to generate HTML upon onload. This is why many modern web pages lag once they load.Hydrous
@TomášZato: you are absolutely right, I've updated the answer to reflect the new reality.Cannibal
N
44

Definitely possible using css counters - just make sure you watch out for browser compatibility...:

This will make h2 get 1., 2., h3 gets 1.1, 2.1, 2.2 etc...

<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.

Also note that using css will not change the heading (e.g. selected text will give you the heading without the numbers). This may or may not be desirable. lpfavreau's (accepted) answer will give you the jquery code to modify the heading text.

See MDN: Using CSS counters for details.

3rd Party Edit

I created an example with the css above

Nadaha answered 11/2, 2009 at 4:6 Comment(5)
I sure hope the OP doesn't like his code working on more than two browsers.Embracery
"Two browsers" is a little harsh. Not supporting any version of IE, though is pretty much a show-stopper unless you have a very tightly-controlled target audience.Playwriting
Has anybody heard of graceful degradation? In my site people with proper browsers will get some nice numbers. IE will only have headings. It's better than nobody having numbers at all, and I am not going to stuff around getting IE to work (presumably it will in IE8)Nadaha
[comment edit/clarification] "not going to stuff around getting IE to work" + for something so trivial as this. It's an enhancement, not a necessity I believe. (I'm all for getting IE to work for important stuff)Nadaha
Just a quick reminder for myself to check against this commentPetrology
C
3

2016 update. Please see Stephen's answer below for a proper method in CSS. My answer made sense in 2009 when the browsers and libraries were different. 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.


See this other question if you're wondering how to do it in CSS, the answer might be what you are looking for. But titel has a good proposition too, depending how your HTML document is made.

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. Support of this CSS feature is limited.

It would of course be easy to use something like jQuery to do the increment also. 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.

Cannibal answered 11/2, 2009 at 4:5 Comment(4)
jQuery it will have to be - the CSS counters idea looks nice but won't work since our IT group only supports IE7 and only plans to upgrade to IE8. FF, Safari, Opera, etc are frowned up (but users can install them).Ollie
Note that this approach will change the header. Using css will leave the header name without the number.Nadaha
Instant downvote for any javascript used to generate HTML upon onload. This is why many modern web pages lag once they load.Hydrous
@TomášZato: you are absolutely right, I've updated the answer to reflect the new reality.Cannibal
R
1

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
    II. Nested two
  2. Section
  3. Section
  4. Section
Randirandie answered 11/2, 2009 at 4:3 Comment(0)
L
0

It is possible to implement auto numbering using HTML itself using ordered lists, and nesting them if necessary. Below there is a link to a live example of this, example I found after a fast search on Google.

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:

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

Latreshia answered 11/2, 2009 at 3:45 Comment(0)
W
0

Could possibly be done either serverside or with 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.

Wapiti answered 11/2, 2009 at 3:49 Comment(2)
It's not impossible with CSS. CSS does this by default with lists. Why would other elements be any different?Cioban
Because it's not semantically correct to mark up an entire, large document as an ordered list. Plus, in large documents you'd probably want the sections to be called 1, 1.1, 1.2, 1.3 etc. which you cannot achieve with OLs.Wapiti
C
0

Lists do it, why not other elements? http://www.w3.org/TR/CSS2/generate.html#scope

Cioban answered 11/2, 2009 at 4:10 Comment(0)
F
-1
<ol>
  <li>Heading 1</li>
  <li>Heading 2</li>
  <li>Heading 3</li>
</ol>
Farriery answered 11/2, 2009 at 4:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.