csscss-selectors

Comma after last selector


Given this code

a,
h1
{
  background: lightblue;
}

I would like to rewrite it such that each selector ends with a comma, even the last. This will make it easy to add or reorder selectors. However if I add a comma after the last the style will not be applied. This code seems to work

a,
h1,
_ {
  background: lightblue;
}

but could this cause unforseen issues?


Solution

  • As you're fully aware, _ is a completely valid type selector, so there's no reason it should be breaking your CSS rule by itself.

    The only unforeseen issue that could come up with this is that, conversely, your CSS rule would apply any _ element that happens to be added to the document(s) you're styling (for whatever reason), just as it would any other element with any other matching selector. If the specification decides to introduce an _ element, then the meaning of your selector with respect to the markup completely changes. Of course, since we're talking about HTML, it's vanishingly unlikely, but it's still a possibility; in the unlikely, unfortunate event that it does happen, you will have to change that stub selector to... something else.

    I won't repeat what others have stated in the comments on how sensible such a coding practice might or might not be.