Select element based on multiple classes

Ciel picture Ciel · Mar 31, 2010 · Viewed 250.1k times · Source

I have a style rule I want to apply to a tag when it has two classes. Is there any way to perform this without JavaScript? In other words:

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

I want to apply my style rule only if the li has both .left and .ui-class-selector classes applied.

Answer

Felix Kling picture Felix Kling · Mar 31, 2010

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?