How to style unordered lists in CSS as comma separated text

后端 未结 6 2057
时光说笑
时光说笑 2020-12-08 06:10

I’m looking for a way to style an unordered list in XHTML with CSS such that it is rendered inline and the list items are separated by commas.

For example, the follo

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 06:49

    It depends on browser implementation, but this should work. Though it relies on first-child, which may limit its use, but essentially puts the comma-space ", " before the list-item, rather than after. I'm not sure how padding/margins will affect this, but if you use `display: inline; with margins and padding set to zero, it should be okay.

    #taglist li:before {content: ", ";}
    #taglist first-child {content: ""; } /* empty string */
    

    Edited: to respond to corrections offered in comments by Jakob.

    The following works (demo page here: http://davidrhysthomas.co.uk/so/liststyles.html:

    #taglist    {width: 50%;
            margin: 1em auto;
            padding: 0;
            }
    
    li      {display: inline;
            margin: 0;
            padding: 0;
            }
    
    li:before   {content: ", ";
            }
    
    #taglist li:first-child:before
            {content: "";
            }
    

    Although the commas are strangely floating-in-the-middle-of-nowhere, and, honestly, I prefer the accepted answer anyway. But just so's I wasn't leaving a horribly broken answer lying around, I thought I should fix it.

    Thanks, Jakob.

提交回复
热议问题