tags:

views:

311

answers:

2

I want to make "grammatically correct" lists using CSS. This is what I have so far:

The <li> tags are displayed horizontally with commas after them.

li { display: inline; list-style-type: none; }

li:after { content: ", "; }

That works, but I want the "last-child" to have a period instead of a comma. And, if possible, I'd like to put "and" before the "last-child" as well. The lists I'm styling are generated dynamically, so I can't just give the "last-children" a class. You can't combine pseudo-elements, but that's basically the effect I want to achieve.

li:last-child li:before { content: "and "; }

li:last-child li:after { content: "."; }

How do I make this work?

+3  A: 

This works :) (I hope multi-browser, Firefox likes it)

<html>
 <head>
  <style type="text/css">
   li { display: inline; list-style-type: none; }
   li:after { content: ", "; }
   li:last-child:before { content: "and "; }
   li:last-child:after { content: "."; }
  </style>
 </head>
 <body>
  <ul>
   <li>One</li>
   <li>Two</li>
   <li>Three</li>
  </ul>
 </body>
</html>
Zyphrax
That was fast! Thanks a lot!
Derek
@Derek, please select your or my answer as THE answer (by clicking left of the answer rating number). This makes it easier for other users with the same problem to find a solution.
Zyphrax
+1  A: 

You can combine pseudo-elements! Sorry guys, I figured this one out myself shortly after posting the question. Maybe it's less commonly used because of compatibility issues.

li:last-child:before { content: "and "; }

li:last-child:after { content: "."; }

This works swimmingly. CSS is kind of amazing.

Derek
So amazing that you can even use it for stuff it's not even intended for...
Guffa