Make OL list start from number different than 1 using CSS August 31, 2006

Posted by Predrag in : Web Design , trackback

If you need to create an OL list that starts from number different than 1, here is an elegant solution. This is very useful if you need to display a listing on more pages (eg. there are 100 search results, and you want to display 10 results per page). In this case, if you are using OL list, it will start from number 1 on every page, and that is not a good solution because it will look like this:


Page 1

  1. Division Bell
  2. Atom Hearth Mother
  3. Relics
  4. Dark Side of the Moon
  5. Wish You Were Here



Page 2

  1. The Wall
  2. More
  3. Piper at the gates of Dawn
  4. Final Cut
  5. Meddle

In order not to use deprecated < ol start=”6″ > parameter in my second list, we’ve searched the Web for an adequate solution which complies with W3C standards - and we found it.

You need to write the following in your CSS:

	OL#page_one { counter-reset: item }
	OL#page_two { counter-reset: item 5 }
	LI { display: block }
	LI:before {
		content: counter(item) ". ";
		counter-increment: item;
		display:block;
		}

And, this is how your lists should be defined:

<ol id="page_one">
	<li>Division Bell</li>
	<li>Atom Hearth Mother</li>
	<li>Relics</li>
	<li>Dark Side of the Moon</li>
	<li>Wish You Were Here</li>
</ol>

<ol id="page_two">
	<li>The Wall</li>
	<li>More</li>
	<li>Piper at the gates of Dawn</li>
	<li>Final Cut</li>
	<li>Meddle</li>
</ol>

These fieldsets represent separate pages, of course. Now, our lists look as we wanted them to look in the first place:


Page 1

  1. Division Bell
  2. Atom Hearth Mother
  3. Relics
  4. Dark Side of the Moon
  5. Wish You Were Here



Page 2

  1. The Wall
  2. More
  3. Piper at the gates of Dawn
  4. Final Cut
  5. Meddle

Let’s explain what happened here: if you set this CSS rule: OL {counter-reset: item 2;} your list will start from 3 (value is set to 0 by default, and list by default starts from 1). From some reason, you need to set LI:display:block; if you don’t do this, numbers won’t appear at all.

All you need to do now is to dynamically change the value in your counter-reset rule. You can do it in PHP, ASP, CF, etc…

li:before rule is used for defining the appearance of the number in your OL list. If you set display:block inside of li:before, you can create some great looking and also 100% clean CSS stuff.

Now, there is a twist in the tale - even though this rule is specified in W3C CSS2 standard (with even more great solutions, that will make XHTML coder’s live easier), IE 6 completely ignores these rules! However, in the Amazon Search created in Ruby example, created by my colleague, you can see how IE6 ignores the rule, but search results appear correctly, and the whole appearance looks acceptable. However, if you use Firefox, you’ll see our OL list in its full glory.

Comments»

1. Enlaza listas numéricas con CSS - aNieto2K - August 31, 2006

[…] Muy buen artículo para conseguir enlazar esas listas numéricas que por separado nunca nos cuadran los números. Ahora, aunque sea para Firefox únicamente (IE lo ignora) podremos hacer que nos números cuadren. […]

2. meneame.net - September 1, 2006

Enlaza listas numeradas usando CSS…

En este artículo [en inglés] nos explican cómo enlazar las listas numéricas con CSS que por separado nunca nos cuadran los números. Si necesitas crear una lista numerada que empiece por un número diferente de 1, aqui tienes una solución elegante…

3. Richard - March 23, 2007

Making ’start’ from the ol tag and ‘value’ from the LI tag deprecated parameters was a mistake.

The number that a list starts at is meaning and not style. Putting it in the style sheet is going to create a lot of bloated style sheets. We have loads of ordered lists that span many pages. I don’t want to create separate styles for all of them.

I’m not following the W3C on this one. It’s just compliance for compliance sake.

4. Predrag - March 23, 2007

Richard, I must say I agree with you on this one. They shouldn’t be deprecated, but since they are, I was verry happy to see that there is a fine solution.
Not awesome though, but it works.

5. Anthony - August 27, 2007

This is much easier:

makes this list item number 30.
makes this list item number 40.
makes this list item number 41.

6. Paul Bodea - Atelier 26 - December 4, 2007

Well that is a good solution. I am glad I found this

7. Hai Nguyen - July 1, 2008

This is not working on IE7? How can I make tweak this to make it work for IE7?