Imagine having 4 spans arranged in a block on your webpage. These spans are filled with content from a knockout viewmodel, but sometimes they may be empty. How can we display them in a visually appealing, comma-separated format while considering their possible emptiness?
Below are two versions I attempted using HTML and CSS:
VERSION 1
This version displays commas even for empty spans.
.comma:not(:last-child):after {
content: ", ";
}
<span class="comma">A</span>
<span class="comma">B</span>
<span class="comma"></span>
<span class="comma">D</span>
VERSION 2
In this version, a final comma is displayed if the last span is empty.
.comma:not(:empty):not(:last-child):after {
content: ", ";
}
<span class="comma">A</span>
<span class="comma">B</span>
<span class="comma">C</span>
<span class="comma"></span>
Is there a way to modify the code to always render correctly, regardless of where the empty spans are located? It's essential to ensure compatibility with modern browsers like IE9+.