6

Our production environment is using an adaptation technology that is incompatible with multiple css classes. This means it's easy to forget and use two classes when styling HTML, and have it break once it gets to production.

I would like to use CSS as a way to highlight when someone forgets and accidentally applies two classes to an element.

Something like this is my intent, although of course this is invalid. It should highlight any element that has two classes applied to it:

.*.* { /* not valid (I wish) */
  outline:2px dotted red;
  }

I understand that this would work if I knew the classes, the problem is I want to flag ANY two classes:

.classA.classB { /* not good enough */
  outline:2px dotted red;
  }

I understand I could do this with JS and a bookmarklet, and maybe that is the only solution. If it is possible with just CSS that would be better as it would automatically flag things for all developers and QA.

3
  • I dont see that CSS has that type of functionality. The easiest way would be using jquery looking for any elements whose class attribute contains a space Commented Jul 27, 2011 at 16:07
  • I think JS is the only solution; Why you do not use JS? Can you say?
    – amiry jd
    Commented Jul 27, 2011 at 16:09
  • CSS is easier to add and remove cleanly. JS would also work though.
    – SimplGy
    Commented Jul 27, 2011 at 16:11

3 Answers 3

6

I just found an acceptable answer:

[class*=" "] {
  outline:2px dotted red;
  }

This highlights anything with a space in the class attribute. It gets some false positives, because sometimes spaces in a class attribute happen legitimately as a result of legible server side code, but I prefer the false positives to false negatives.

Any better ideas?

1
  • 1
    Note also (probably unlikely, and hence acceptable) this would also flag class="foo foo" which really only has one class applied.
    – Phrogz
    Commented Jul 27, 2011 at 16:21
4

To remove most false positives (e.g. space-padded attribute values), you can use this selector:

[class*=" "]:not([class^=" "]):not([class$=" "]) {
    outline: 2px dotted red;
}

This still does not filter out repeated classes in values such as class="foo foo" as pointed out by Phrogz in your own answer, but it's better than nothing, and I think that's much less likely to occur than class attributes with whitespace padding.

1

This is not possible using CSS alone.

And, if I may say so, your production environment is silly. CSS without multiple classes is like a tag cloud with only one tag allowed per item. It defeats some of the purpose. Fix your production environment to not abuse CSS in this manner, instead of limiting your authors from properly, semantically describing the content.

1
  • I don't disagree. We are using a Japanese content adaptation system to get our site to function on Keitai mobile devices. There are not a lot of options.
    – SimplGy
    Commented Jul 27, 2011 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.