Click here to Skip to main content
13,343,731 members (63,367 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 21 Feb 2012

Separating HTML list items by comma using CSS

, 23 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Separating HTML list items by comma using CSS

In the HTML, we are displaying list of elements using the UL/LI structure. In some cases, we want to show a list of elements as an inline list separated by comma (,). We want to put list similar to the following structure:

<ul class="csv">

In the programming languages, it is easy to create comma separated lists. After each item should be placed comma and the last comma at the end should be removed using some trim() function. However, in pure CSS, there is no trim function.


CSS code for creating comma separated lists is shown in the following code:

ul.csv { list-style: none; margin: 0; padding: 0; }
ul.csv li { display: inline; }
ul.csv li:after { content: ","; }
ul.csv li:last-child:after { content: ""; }

The first two lines put LI elements inline. The third line puts comma after each LI element. However, here is a problem because we will have one additional comma character at the end of the list, and we do not have trim() function.
Therefore, we are adding the fourth line that puts blank content after last LI element in the list.
This way you have true comma separated list implemented in pure CSS.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jovan Popovic
Program Manager Microsoft
Serbia Serbia
Graduated from Faculty of Electrical Engineering, Department of Computer Techniques and Informatics, University of Belgrade, Serbia.
Currently working in Microsoft as Program Manager on SQL Server product.
Member of JQuery community - created few popular plugins (four popular JQuery DataTables add-ins and loadJSON template engine).
Interests: Web and databases, Software engineering process(estimation and standardization), mobile and business intelligence platforms.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 1074221517-Apr-14 6:07
memberMember 1074221517-Apr-14 6:07 
GeneralReason for my vote of 5 Nice one! Pin
Ernesto Oltra29-Feb-12 10:10
memberErnesto Oltra29-Feb-12 10:10 
GeneralReason for my vote of 5 Nice one Pin
nikhi _singh24-Feb-12 19:24
membernikhi _singh24-Feb-12 19:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 23 Feb 2012
Article Copyright 2012 by Jovan Popovic
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid