Anne van Kesteren: Weblog 4.2

Weblog on W3C, WHATWG, HTML, CSS, DOM, XML, HTTP and more.

How to display UL elements as a flat comma-separated list

I told Milo I could come up with something that makes the list use an “&” at the end. Apparently the version I made many years ago did not have any whitespace between the LI elements which makes it a lot easier. This time I created one with whitespace although I did have to use some sort of "hack" (display:block;float:left). Unfortunately Opera does not support the CSS 3 selector :last-child so the example only works in Mozilla based browers on Windows. I am not sure about Safari. It does degrade gracefully in Opera though, it is not really a major problem.

To make the example work in IE as well I used _display:inline;_float:none.

Here is the example: How to display UL elements as a flat comma-separated list. Something that might be a problem in Opera is that it also does not support the + selector so you need to apply clear:both on the next-sibling element of UL yourself.

16th January 2005

Browse through posts: main.

Comments

  1. @Anne, last time I checked Opera supported '+' selector.

    Permalink · 2005-01-16 13:44:31 · PragmaInline

  2. Actually Moose effectively used it in his CSS Destroy two years ago.

    Permalink · 2005-01-16 14:00:44 · Frenzie

  3. Boy, Safari does something weird here: it separates every word with an ampersand, as if the :last-child wasn't in the selector. Or maybe it treats every LI as last-child ?

    Safari does support :last-child normally, and Opera has supported the + for quite a while :^)

    Permalink · 2005-01-16 14:53:30 · Philippe

  4. You shouldn't have used the ampersand though (use and instead), it is meant to group two words. Read these and see the difference:

    Permalink · 2005-01-16 15:38:59 · Robbert Broersma

  5. The fallback in Opera is quite acceptable, but seeing that & as the last separator in Firefox is indeed a nice touch.

    On my personal site I use display:inline-block; for the list elements, which means I don't have to worry about floats and clears and all that. Downside is of course that it doesn't work in Firefox :)

    Permalink · 2005-01-16 15:52:21 · Rijk

  6. Wow, for a moment there I was sure it wasn't possible... but you proved me wrong. Most impressive!

    Permalink · 2005-01-16 15:58:27 · Milo

  7. Permalink · 2005-01-16 18:14:46 · EZblog» Linkdump 16 januari

  8. It's nice, but too bad it's copied as a list when copy-pasting.

    Permalink · 2005-01-16 20:07:07 · chris eidhof

  9. It's a very cool trick for the semantically keen. I like it very much.

    Possibly the only depressing thing about this is that I've got a 2 & a half month old unfinished entry on my blog which works in the same way. I think there's a lesson in 'finishing stuff' in here something... Excellent example page though, Anne - lovely and concise.

    Although rather breaking class semantics, you can also make these lists appear inline within a block of text by making the previous block level element "inline" as well. It's maybe a bit hackish, but means you can get flowing lists within your text blocks.
    Technically, there's an argument within all this for the HTML spec allowing the use of some list-type within paragraphs...

    Permalink · 2005-01-16 21:37:57 · Ben Ward

  10. Works in KHTML/Konqueror 3.3.91.

    Permalink · 2005-01-17 18:11:18 · praseodymium

  11. Two points I want to note about this otherwise excellent example:

    1. The CSS doesn't validate, and it's not hard at all to make it validating. Kinda regrettable :)
    2. Perhaps next time you could add a styled <pre> section with the CSS you used on the page itself, so that we don't have to check the source. Would be great to see the CSS along with the result at the same time all nice and easily ^_^

    Other than that, good stuff, I like the use of an & for the last item, makes it far more interesting.

    Permalink · 2005-01-18 10:15:00 · Faruk Ates

  12. Since when does the validator support CSS 3? :last-child is not part of CSS 2.1. If you are talking about the two line Internet Explorer hack, I guess you are able to remove that yourself.

    Permalink · 2005-01-18 14:44:21 · Anne

  13. Instead of the underscore hack you can also use a conditional comment for IE, which I think is more graceful because it is a documented feature and it doesn't present validation issues ;)

    Permalink · 2005-01-18 16:57:23 · crisp

  14. I'm using this to make the menus on my website to be.

    Oh, please don't bother looking at that in IE :)

    Permalink · 2005-01-19 00:01:28 · The Wolf

  15. nice stuff, I have a bug report though: when the list is inside a smaller element and the elements need to wrap, the wrap happens before the comma, so you get commas at the start of your line of text instead of at the end.

    I changed the CSS around a bit, but there is still one extra comma at the end before the ampersand. any ideas?

    Permalink · 2005-01-19 03:07:46 · Geoff

  16. Perhaps using li{white-space:nowrap}? I haven't really tested it yet though.

    Permalink · 2005-01-19 10:01:54 · Anne

  17. Anne: The CSS Validator supports CSS3 just fine :)

    Permalink · 2005-01-19 16:03:43 · Faruk Ates

Comments are closed

Sorry, comments are closed at this time.