tags:

views:

152

answers:

4

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?

A: 

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
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
No, there's no way to do that.
Tor Valamo
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
+1  A: 

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
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
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
The value attribute WAS deprecated. It's "de"-deprecated in html5 :)
Tor Valamo
Oh, I had no idea. But checking with w3 I see it's shown as deprecated.<a href='http://www.w3schools.com/tags/tag_li.asp'>Source</a>Thanks for letting us now anyway!
johnnyArt
Oh shuffles no html allowed on comments?*blushes and hides*
johnnyArt
@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
+1  A: 

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
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
This answer may help you: http://stackoverflow.com/questions/1632005/ordered-list-html-lower-alpha-with-right-parentheses/1636635#1636635
DisgruntledGoat
+3  A: 

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
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 ("<dd>Item 5</dd>" etc.) to be flush left with the rest of my text and the years to be further left, as a numeric list is.
Steve
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
Thanks so much Luke and Thomas, that's an elegant and perfect solution.
Steve