I canceled the logic by setting a comma as the contents of the before pseudo-element
Example: http://codepen.io/anon/pen/NPdbbw
.comma:not(:first-child) { margin-left: -.3em; } .comma:empty { display: none; } .comma:not(:first-child):before { content: ", "; }
If the first .comma element may also be empty, then here is a more complex approach
http://codepen.io/anon/pen/BypQRd
.comma:not(:first-child) { margin-left: -.3em; } .comma:first-child:empty ~ .comma:not(:empty) { margin-left: 0; } .comma:first-child:empty ~ .comma:not(:empty) ~ .comma:not(:empty) { margin-left: -.3em; } .comma:empty { display: none; } .comma:not(:first-child):before { content: ", "; } .comma:not(:first-child):before { content: ", "; } .comma:empty + .comma:not(:empty):before { content : ""; } .comma:not(:empty) ~ .comma:empty + .comma:not(:empty):before { content : ", "; }
The last example was successfully tested against all possible combinations of 4 elements.
Additional Information :empty pseudo-class available on MDN
fcalderan
source share