views:

175

answers:

1

I have a style rule I want to apply to a tag when it has TWO styles. Is there any way to perform this without javascript? In other words..

<li class='left ui-class-selector'>

I want to select only if the li has both 'left' and 'ui-class-selector' classes applied.

+4  A: 

You mean two classes? "Chain" the selectors (no spaces between them):

.class1.class2 {
    /* style here */
}

This selects all elements with class1 that also have class2.

In your case:

li.left.ui-class-selector {

}

Official documentation : CSS2 class selectors.


As akamike points out a problem with this method in Internet Explorer 6 you might want to read this: Use double classes in IE6 CSS?

Felix Kling
Please note that IE6 does not like these, as it does not read the chain of classes. It will only look at the last class in the chain.
akamike
@Felix: it isn't :) All major websites have dropped, are dropping or are planning to drop in the near future support for IE6.. Do the same!
Andreas Bonini
@Andreas Bonini: Yeah I know. To be honest I already don't care about IE6 for a long time. (I deleted my previous comment because I found the other question that deals with this problem.)
Felix Kling
Yeah, you can't use them at all in IE6, only take longer routes to style your elements. For example, styling each class separately and using appropriate CSS specificity to override as best as you can.
akamike
Thank you very much. This solved what I was trying to do. I'm not really concerned with IE6 support in this specific case, so this works well.
Stacey