Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbering an ordered list <OL> like an array, with brackets around the numbers

Tags:

css

I have this list:

<ol start="0">
  <li>Tokyo Skytree</li>
  <li>Canton Tower</li>
  <li>CN Tower</li>
  <li>Ostankino Tower</li>
  <li>Oriental Pearl Tower</li>
</ol>

Which shows something like this:

1. Tokyo Skytree
2. Canton Tower
3. CN Tower
4. Ostankino Tower
5. Oriental Pearl Tower

Now what I want is to have something like this:

[1] Tokyo Skytree
[2] Canton Tower
[3] CN Tower
[4] Ostankino Tower
[5] Oriental Pearl Tower

Ideally I want a 0-based array like:

[0] = Tokyo Skytree
[1] = Canton Tower
[2] = CN Tower
[3] = Ostankino Tower
[4] = Oriental Pearl Tower

I've read about CSS Numbering Style and CSS Lists W3C Draft but I cannot figure out a solution that works.

like image 728
AlexStack Avatar asked Mar 30 '13 07:03

AlexStack


People also ask

How can you make a numbered list ol?

An ordered list is marked with the numbers by default. You can create an ordered list using the <ol></ol> tag and, define the list items using <li></li>. type="1"− This creates a numbered list starting from 1. type="A"− This creates a list numbered with uppercase letters starting from A.

What is an ordered list ol >?

The OL element defines an ordered list. The element contains one or more LI elements that define the actual items of the list. Unlike with an unordered list (UL), the items of an ordered list have a definite sequence. Items in an ordered list are numbered by the browser.

What is the default numbering style for OL tag?

By default, browsers number ordered list items with a sequence of Arabic numerals. Besides being able to start the sequence at some number other than 1, you can use the type attribute with the <ol> tag to change the numbering style itself.

Is Ol a numbered list?

<ol>: The Ordered List element. The <ol> HTML element represents an ordered list of items — typically rendered as a numbered list.


2 Answers

You can do something like this:

ol {
  counter-reset: o-counter -1;
  list-style-type: none;
}
ol li:before {
  content: '[' counter(o-counter) '] = ';
  counter-increment: o-counter;
}

http://jsfiddle.net/userdude/Fb3ab/2/

Keep in mind the o-counter (or whatever you call it) needs to be on the parent UL/OL for it to increment. And, IE7 does not support this either.

More information:

  • http://www.w3.org/TR/CSS21/generate.html#counters
  • http://css-infos.net/property/content
like image 185
Jared Farrish Avatar answered Oct 19 '22 00:10

Jared Farrish


You can use the counter property is CSS.

<ol>
  <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  <li>Aliquam tincidunt mauris eu risus.</li>
  <li>Vestibulum auctor dapibus neque.</li>
  <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  <li>Aliquam tincidunt mauris eu risus.</li>
  <li>Vestibulum auctor dapibus neque.</li>
</ol>  


ol { list-style: none; 
counter-reset:array;}
ol li:before {
counter-increment:array;
content:"[" counter(array) "] ";
}

http://codepen.io/Nunotmp/pen/duhAb

like image 23
Juan Rangel Avatar answered Oct 19 '22 01:10

Juan Rangel