1

I want to create a list in html that has reversed ordered, non-consecutive years instead of numbers. It should look like this:

2009 Item 7.
2007 Item 6.
2006 Item 5.
2004 Item 4.
2003 Item 3.
2002 Item 2.
2000 Item 1.

I have code that works for reverse ordering number lists:

ol {
    margin-left: 0;
    padding-left: 1em;
    text-indent: -1em;
}

<ol>
    <li value="5">Item 5.</li>
    <li value="4">Item 4.</li>
    <li value="3">Item 3.</li>
    <li value="2">Item 2.</li>
    <li value="1">Item 1.</li>
</ol>

This gives me:

5. Item 5.
4. Item 4.
3. Item 3.
2. Item 2.
1. Item 1.  

If I simply add in a year:

<ol>
    <li value="2002">Item 5.</li>
</ol>

The '2002' is moves inside the area for the item, instead of being set out to the left. Is there an easy way around this?

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Steve
  • 5,727
  • 10
  • 32
  • 30
  • Not sure why my html code didn't show up there - also the list is reversed on my web page, but not here.
    1. Item 5.
    2. Item 4.
    3. Item 3.
    4. Item 2.
    5. Item 1.
    – Steve Dec 13 '09 at 21:56
  • 1
    @Steve: You need to use markdown: http://stackoverflow.com/editing-help – OMG Ponies Dec 13 '09 at 21:59
  • The value attribute of the li tag is deprecated http://www.w3schools.com/TAGS/att_li_value.asp - not valid in HTML 4.01 Strict / XHTML 1.0 Strict. You may want to take a different approach depending on the current and future portability/compatibility needs of your HTML markup. – micahwittman Dec 13 '09 at 22:11
  • The value attribute has been un-deprecated in HTML5 – Thomas J Bradley Dec 15 '09 at 01:08

4 Answers4

4

Steve, it seems that a definition list would better suit your needs. Technically, though the year is a number, it is not functioning as the numbering of your list (inferred from you not wanting the period after the year). You can style the dl to look similar to the ol and have greater control over the year in addition to the list item.

dt {width: 3em; clear: both;}
dd, dt {float: left;}

<dl>
    <dt>2007</dt> 
    <dd>Item 6</dd>
    <dt>2006</dt>
    <dd>Item 5</dd>
    <dt>2004</dt>
    <dd>Item 4</dd>
    <dt>2003</dt>
    <dd>Item 3</dd>
    <dt>2002</dt>
    <dd>Item 2</dd>
    <dt>2000</dt>
    <dd>Item 1</dd>
</dl>
Luke
  • 597
  • 6
  • 11
  • Thanks Luke. That works fine when using isolated code. When I combine it with the rest of my page, however, the 'float: left' command seems to mess up the content immediately below (it overlaps the definition list, whereas it did not before). Also, the years are not outside of the regular margins of the text. I tried using various implementations of 'margin-left' and 'padding-left', but it doesn't seem to move very far, no matter what values I use. I'd like the items ("
    Item 5
    " etc.) to be flush left with the rest of my text and the years to be further left, as a numeric list is.
    – Steve Dec 14 '09 at 23:51
  • The simple solution would be to just float the dts left and give them a negative left margin. margin-left: -3em; Since the years are always going to be 4 characters long it should work properly. – Thomas J Bradley Dec 15 '09 at 01:11
  • Thanks so much Luke and Thomas, that's an elegant and perfect solution. – Steve Dec 15 '09 at 02:15
1

It seems to me that you're applying the style to the wrong element. If you change the css to this:

li {
margin-left: 0;
padding-left: 1em;
text-indent: -1em; }

It fixes your problem straight away. Note that this may or may not be the result you were looking for, but for what I understood

johnnyArt
  • 4,327
  • 5
  • 29
  • 28
  • Thanks, that works perfectly too, but still leaves an unwanted period after the year (e.g., "2007."). Is there any way to remove that? – Steve Dec 13 '09 at 22:32
  • Not that I'm aware of. I've done some searching about it but since the "value" attribute of lists is deprecated you shouldn't really be using it. Why not just include the value inside the list element instead of specifying it as an attribute? – johnnyArt Dec 13 '09 at 22:43
  • 1
    The value attribute WAS deprecated. It's "de"-deprecated in html5 :) – Tor Valamo Dec 13 '09 at 23:31
  • Oh, I had no idea. But checking with w3 I see it's shown as deprecated. Source Thanks for letting us now anyway! – johnnyArt Dec 13 '09 at 23:42
  • Oh shuffles no html allowed on comments? *blushes and hides* – johnnyArt Dec 13 '09 at 23:42
  • @johnnyArt: **w3scools != w3**. Regardless, that page is just regurgitating the HTML4 spec, where `value` *is* deprecated. What Tor was saying is that it's no longer deprecated in HTML5, the new spec. So unless you care about being 100% validated against the HTML4 spec (which is kinda meaningless) then don't worry about it. – DisgruntledGoat Dec 14 '09 at 01:16
1

On an ordered list, value attribute on the <li>, which is fairly self-explanatory.

<ol>
    <li value="2007">Item 1</li>
    <li value="2005">Item 2</li>
    <li value="2004">Item 3</li>
</ol>

I'm not sure of your exact issue with numbers moving - I tried Firefox, Chrome and Opera and they all played ball. If you're seeing issues in Internet Explorer, make sure you have a doctype so you don't go into quirks mode.

I did see a small issue in Chrome, where the first number was cut off at the edge of the screen. However, this is easily fixed by changing the left-padding (or margin) to anything over the default 40px, if the same thing happens for you.

Interestingly, HTML 5 defines a reversed attribute that would be closer to what you want, but that isn't well-supported yet. Also note that while the value attribute is deprecated in HTML4, it is not deprecated in HTML5, so use it to your heart's content!

DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290
  • Thanks for that. I was having issues in Camino, but the suggestions in the posts below fixed that. I just need to get rid of the damn period after the years now. – Steve Dec 13 '09 at 22:35
  • This answer may help you: http://stackoverflow.com/questions/1632005/ordered-list-html-lower-alpha-with-right-parentheses/1636635#1636635 – DisgruntledGoat Dec 13 '09 at 23:17
0

This works for me

<ol type="1">
    <li value="2007">item 1</li>
    <li value="2005">item 2</li>
    <li value="2004">item 3</li>
</ol>
Tor Valamo
  • 33,261
  • 11
  • 73
  • 81
  • That works great, thanks, but it leaves a period after the year (e.g., "2002."). Is there any way to get rid of that? – Steve Dec 13 '09 at 22:28
  • No, there's no way to do that. – Tor Valamo Dec 13 '09 at 22:35
  • Don't spend too long trying to find a way to do that, because it's not possible, unless you make each number into an image and use list-style-type on each element to set it. It is up to the browser how to display ordered list items. – Tor Valamo Dec 13 '09 at 22:44