How can you customize the numbers in an ordered list?
Asked Answered
L

17

137

How can I left-align the numbers in an ordered list?

1.  an item
// skip some items for brevity 
9.  another item
10. notice the 1 is under the 9, and the item contents also line up

Change the character after the number in an ordered list?

1) an item

Also is there a CSS solution to change from numbers to alphabetic/roman lists instead of using the type attribute on the ol element.

I am mostly interested in answers that work on Firefox 3.

Lovato answered 14/8, 2008 at 10:42 Comment(1)
a very elegant example with nesting! --> developer.mozilla.org/en-US/docs/Web/CSS/CSS_Counter_Styles/…Shirashirah
L
121

This is the solution I have working in Firefox 3, Opera and Google Chrome. The list still displays in IE7 (but without the close bracket and left align numbers):

ol {
  counter-reset: item;
  margin-left: 0;
  padding-left: 0;
}
li {
  display: block;
  margin-bottom: .5em;
  margin-left: 2em;
}
li::before {
  display: inline-block;
  content: counter(item) ") ";
  counter-increment: item;
  width: 2em;
  margin-left: -2em;
}
<ol>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight</li>
  <li>Nine<br>Items</li>
  <li>Ten<br>Items</li>
</ol>

EDIT: Included multiple line fix by strager

Also is there a CSS solution to change from numbers to alphabetic/roman lists instead of using the type attribute on the ol element.

Refer to list-style-type CSS property. Or when using counters the second argument accepts a list-style-type value. For example the following will use upper roman:

li::before {
  content: counter(item, upper-roman) ") ";
  counter-increment: item;
/* ... */
Lovato answered 14/8, 2008 at 10:42 Comment(2)
Needed to add the numbers back in, a global style was removing them (who knows why you would use a ol and remove the numbers rather than a ul??). Very clear answer +1 for full explanation and code, can easily be modified to do anything.Hudnut
I suggest a cleaner CSS margin/padding solution, that works better with lists having wide numbering: ol {counter-reset: item; padding-left: 0;} li {display: block; margin-bottom: .5em;} li:before {display: inline-block; content: counter(item) ") "; counter-increment: item; padding-right: 0.8em;}.Elector
T
52

HTML5: Use the value attribute (no CSS needed)

Modern browsers will interpret the value attribute and will display it as you expect. See MDN documentation.

<ol>
  <li value="3">This is item three.</li>
  <li value="50">This is item fifty.</li>
  <li value="100">This is item one hundred.</li>
</ol>

Also have a look at the <ol> article on MDN, especially the documentation for the start and attribute.

Tyrant answered 6/2, 2020 at 17:40 Comment(2)
but is there any way to style, say, the font-family of the numbers?Ozone
This needs more upvotes, perfect solution, thank you!Kippie
S
31

The CSS for styling lists is here, but is basically:

li {
    list-style-type: decimal;
    list-style-position: inside;
}

However, the specific layout you're after can probably only be achieved by delving into the innards of the layout with something like this (note that I haven't actually tried it):

ol { counter-reset: item }
li { display: block }
li:before { content: counter(item) ") "; counter-increment: item }
Shiite answered 14/8, 2008 at 11:1 Comment(1)
This gets the numbers lined up, but the text content isn't.Lovato
C
16

You can also specify your own numbers in the HTML - e.g. if the numbers are being provided by a database:

ol {
  list-style: none;
}

ol>li:before {
  content: attr(seq) ". ";
}
<ol>
  <li seq="1">Item one</li>
  <li seq="20">Item twenty</li>
  <li seq="300">Item three hundred</li>
</ol>

The seq attribute is made visible using a method similar to that given in other answers. But instead of using content: counter(foo), we use content: attr(seq).

Demo in CodePen with more styling

Cordon answered 5/8, 2015 at 4:53 Comment(4)
You can simplify this if you just use the value attribute on <li>. e.g. <li value="20">. Then you don't need any pseudo elements. DemoCountercurrent
@Countercurrent While that is valid (and a good solution), it's limited to numerical values as the list is ordinal. As such, you can't do something like value="4A", as it won't work. Additionally, the value attribute can work with the type attribute, but value still must be a number (as it works within an ordered set).Endocranium
Thanks - this worked for me when I needed Flying Saucer to show a list in reverse order (reversed attribute is html5 only, as is using the value of the li). Instead of using your seq I set a value and used attr(value) instead of attr(seq)Spondee
Is seq a standard attribute defined anywhere? If not, wouldn't it be better to use data-seq?Oribel
G
8

Stole a lot of this from other answers, but this is working in FF3 for me. It has upper-roman, uniform indenting, a close bracket.

ol {
  counter-reset: item;
  margin-left: 0;
  padding-left: 0;
}
li {
  margin-bottom: .5em;
}
li:before {
  display: inline-block;
  content: counter(item, upper-roman) ")";
  counter-increment: item;
  width: 3em;
}
<ol>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight</li>
  <li>Nine</li>
  <li>Ten</li>
</ol>
Gorgonian answered 28/1, 2009 at 15:54 Comment(0)
S
6

I suggest playing with the :before attribute and seeing what you can achieve with it. It will mean your code really is limited to nice new browsers, and excludes the (annoyingly large) section of the market still using rubbish old browsers,

Something like the following, which forces a fixed with on the items. Yes, I know it's less elegant to have to choose the width yourself, but using CSS for your layout is like undercover police work: however good your motives, it always gets messy.

li:before {
  content: counter(item) ") ";
  counter-increment: item;
  display: marker;
  width: 2em;
}

But you're going to have to experiment to find the exact solution.

Shiite answered 18/8, 2008 at 14:51 Comment(1)
you'll need a counter-reset: item; before that block.Breach
T
5

The numbers line up better if you add leading-zeroes to the numbers, by setting list-style-type to:

ol { list-style-type: decimal-leading-zero; }
Trigonous answered 24/8, 2008 at 10:30 Comment(0)
A
4

Borrowed and improved Marcus Downing's answer. Tested and works in Firefox 3 and Opera 9. Supports multiple lines, too.

ol {
    counter-reset: item;
    margin-left: 0;
    padding-left: 0;
}

li {
    display: block;
    margin-left: 3.5em;          /* Change with margin-left on li:before.  Must be -li:before::margin-left + li:before::padding-right.  (Causes indention for other lines.) */
}

li:before {
    content: counter(item) ")";  /* Change 'item' to 'item, upper-roman' or 'item, lower-roman' for upper- and lower-case roman, respectively. */
    counter-increment: item;
    display: inline-block;
    text-align: right;
    width: 3em;                  /* Must be the maximum width of your list's numbers, including the ')', for compatability (in case you use a fixed-width font, for example).  Will have to beef up if using roman. */
    padding-right: 0.5em;
    margin-left: -3.5em;         /* See li comments. */
}
Angelenaangeleno answered 28/1, 2009 at 20:1 Comment(5)
Also you want text-align: left; not right. And the last line should be margin-left: -3.5em;Lovato
@grom, Thanks for the em correction. Also, Opera renders lists right-aligned by default, so I mimicked this behavior.Angelenaangeleno
@grom, The Firefox issue is ... Firefox puts the li:before pseudoelement on its own line without the float, even if it is display: inline-block.Angelenaangeleno
@strager, Well I am using 3.0.4 on Linux and 3.0.3 on Windows, and it works for me without the float: left; rule.Lovato
@grom, Hmm, interesting. I just tested myself, and it works without the float. Maybe I mistyped.Angelenaangeleno
C
2

Nope... just use a DL:

dl { overflow:hidden; }
dt {
 float:left;
 clear: left;
 width:4em; /* adjust the width; make sure the total of both is 100% */
 text-align: right
}
dd {
 float:left;
 width:50%; /* adjust the width; make sure the total of both is 100% */
 margin: 0 0.5em;
}
Cis answered 22/11, 2011 at 23:45 Comment(0)
F
1

There is the Type attribute which allows you to change the numbering style, however, you cannot change the full stop after the number/letter.

<ol type="a">
    <li>Turn left on Maple Street</li>
    <li>Turn right on Clover Court</li>
</ol>
Fasano answered 14/8, 2008 at 11:3 Comment(5)
The markup in this answer needs to be fixed. Use lowercase and enclose attribute values in "quotes".Duhl
And close your elements: <li>...</li>.Duhl
@Duhl — You are aware that the markup presented is proper, 100% valid HTML, yes? Don't downvote people for not using XHTML unless XHTML is requested.Biblio
I didn't downvote you. And yes, that's true re. HTML. I was just being a standardsy picky person.Duhl
My apologies for posting invalid html. I copied the code from a website and didn't think to correct it. I feel ashamed now :(Fasano
P
1

The docs say regarding list-style-position: outside

CSS1 did not specify the precise location of the marker box and for reasons of compatibility, CSS2 remains equally ambiguous. For more precise control of marker boxes, please use markers.

Further up that page is the stuff about markers.

One example is:

       LI:before { 
           display: marker;
           content: "(" counter(counter) ")";
           counter-increment: counter;
           width: 6em;
           text-align: center;
       }
Perichondrium answered 28/1, 2009 at 6:53 Comment(1)
All the examples (see w3.org/TR/CSS2/generate.html#q11) for markers do not work for me.Lovato
M
0

Vhndaree posted an interesting implementation of this problem on a duplicate question, which goes a step further than any of the existing answers in that it implements a custom character before the incrementing numbers:

 
 .custom {
     list-style-type: none;
 }
 .custom li {
     counter-increment: step-counter;
 }
 .custom li::before {
     content: '(' counter(step-counter) ')';
     margin-right: 5px;
 }
 
 
 
 <ol class="custom">
    <li>First</li>
    <li>Second</li>
    <li>Third</li>
    <li>Fourth</li>
    <li>Fifth</li>
    <li>Sixth</li>
    <li>Seventh</li>
    <li>Eighth</li>
    <li>Ninth</li>
    <li>Tenth</li>
 </ol>
 
 
Millstream answered 14/8, 2008 at 10:42 Comment(0)
D
0

Quick and dirt alternative solution. You can use a tabulation character along with preformatted text. Here's a possibility:

<style type="text/css">
ol {
    list-style-position: inside;
}
li:first-letter {
    white-space: pre;
}
</style>

and your html:

<ol>
<li>    an item</li>
<li>    another item</li>
...
</ol>

Note that the space between the li tag and the beggining of the text is a tabulation character (what you get when you press the tab key inside notepad).

If you need to support older browsers, you can do this instead:

<style type="text/css">
ol {
    list-style-position: inside;
}
</style>

<ol>
    <li><pre>   </pre>an item</li>
    <li><pre>   </pre>another item</li>
    ...
</ol>
Dysphemia answered 1/2, 2009 at 17:2 Comment(0)
E
0

I will give here the kind of answer i usually don't like to read, but i think that as there are other answers telling you how to achive what you want, it could be nice to rethink if what you are trying to achive is really a good idea.

First, you should think if it is a good idea to show the items in a non-standard way, with a separator charater diferent than the provided.

I don't know the reasons for that, but let's suppose you have good reasons.

The ways propossed here to achive that consist in add content to your markup, mainly trough the CSS :before pseudoclass. This content is really modifing your DOM structure, adding those items to it.

When you use standard "ol" numeration, you will have a rendered content in which the "li" text is selectable, but the number preceding it is not selectable. That is, the standard numbering system seems to be more "decoration" than real content. If you add content for numbers using for example those ":before" methods, this content will be selectable, and dued to this, performing undesired vopy/paste issues, or accesibility issues with screen readers that will read this "new" content in addition to the standard numeration system.

Perhaps another approach could be to style the numbers using images, although this alternative will bring its own problems (numbers not shown when images are disabled, text size for number not changing, ...).

Anyway, the reason for this answer is not just to propose this "images" alternative, but to make people think in the consequences of trying to change the standard numeration system for ordered lists.

Eisteddfod answered 8/2, 2014 at 17:10 Comment(0)
W
0

This code makes numbering style same as headers of li content.

<style>
    h4 {font-size: 18px}
    ol.list-h4 {counter-reset: item; padding-left:27px}
    ol.list-h4 > li {display: block}
    ol.list-h4 > li::before {display: block; position:absolute;  left:16px;  top:auto; content: counter(item)"."; counter-increment: item; font-size: 18px}
    ol.list-h4 > li > h4 {padding-top:3px}
</style>

<ol class="list-h4">
    <li>
        <h4>...</h4>
        <p>...</p> 
    </li>
    <li>...</li>
</ol>
Watchdog answered 27/3, 2017 at 14:52 Comment(0)
S
-1

I have it. Try the following:

<html>
<head>
<style type='text/css'>

    ol { counter-reset: item; }

    li { display: block; }

    li:before { content: counter(item) ")"; counter-increment: item; 
        display: inline-block; width: 50px; }

</style>
</head>
<body>
<ol>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
    <li>Something</li>
</ol>
</body>

The catch is that this definitely won't work on older or less compliant browsers: display: inline-block is a very new property.

Shiite answered 28/1, 2009 at 14:49 Comment(0)
T
-1

The other answers are better from a conceptual point of view. However, you can just left-pad the numbers with the appropriate number of '&ensp;' to make them line up.

* Note: I did not at first recognize that a numbered list was being used. I thought the list was being explicitly generated.

Tourcoing answered 28/11, 2011 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.