tags:

views:

31

answers:

3

Is it possible to apply style to all class X only if it's of class A, B or C without code duplication?

I understand grouping:

A, B, C {
   ...
}

But I want something like:

X, (A, B, C) {
   ...
}

How can I achieve this without duplicating code?

+2  A: 

Couldn't you do this?

.X.A, .X.B, .X.C {
 /* rules */
}

That will match elements with both class X and A, or X and B, or X and C. Or did I misunderstand your question?

Marc W
I was hoping for a shorter solution built into CSS. I know about this one, but is that the only way?
LiraNuna
Yes. And this is built into CSS. =P
Marc W
If you really need to mark something with that many classes, why don't you make a new class or that represents this union of classes?
James Deville
Because those classes are a given - I'm using ExtJS and it has way too much predefined classes.
LiraNuna
Ahh yes. I've used ExtJS extensively. There really is no way around dealing with the hugely absurd number of classes it uses. Trust me, I've tried. Just give in... =P
Marc W
It's a really nice library - just styling it is rather... extensive as the library itself...
LiraNuna
Yes definitely. I'd say I spent about the same amount of time customizing all the styles as I did actually writing the Javascript to make the widgets. You get used to all the extensive CSS. Lots of trial-and-error in there.
Marc W
A: 

Sorry, I wanted to comment on Marc W's answer, but I guess I don't have enough reputation points to do so >=O

Please note that using multiple classes in a selector, like

.X.A, .X.B, .X.C {
 /* rules */
}

Isn't supported by IE6.

Kevin C.
Good point. When I was doing Ext work a while back, we pretty much had to drop IE6 support in our product. Actually, that was a great bonus to using Ext in the first place. =)
Marc W
@Kevin C.: Well, we should technically be murdering IE6 off whenever we get the chance anyway...
Nathan Kleyn
A: 

I'm modifying BluePrintCSS to accommodate our needs. Not quite the same as what you're doing, but there is a need to constantly apply styles to bunches of classes or IDs at once just as you are doing.

I've found that it's quicker to built a 'generator' in Javascript and then use that to spit out all the repetitive CSS declarations via nested for loops and such.

The advantage is that when the framework is updated, I can make some tweaks to my script and quickly update my 'over-ride' styles.

DA