0

Is there a way to apply a style to elements that contain multiple classes?

Example


<div class="foo"     > foo     </div>
<div class="foo bar" > foo bar </div>
<div class="bar"     > bar     </div>

I only want to modify the styling of the element that contains class "foo bar". The XPath information would be something like //div[contains(@class,"foo") and contains(@class,"bar")]. Any ideas for pure CSS, w/o having to create a unique class?

2 Answers 2

5

Like this:

.foo { background: red; }
.bar { background: blue; }
.foo.bar { background: purple; }
Sign up to request clarification or add additional context in comments.

6 Comments

If only it would KNOW that red and blue make purple - it could do this automagically! ;)
Probably not worth mentioning, but just in case, IE6 has some issues with this. @BoltClock explains it well here: stackoverflow.com/questions/3772290/…
In IE6: e.c1.c2...cn = e.cn where e is an element and ci is a class with position i.
@thirtydot: If I had seen that question I probably wouldn't have wasted my time asking :) --- and I did search SO and google :) -- could you check on attribute selectors in IE6
@vol7ron: IE6 simply does not support attribute selectors, like those in your answer.
|
1

Solutions


  1.  .foo.bar { ... }
    
  2.  [class="foo bar"] { ... }             // explicit class name
    
  3.  [class~="foo"][class~="bar"] { ... }  // inclusive class name
    

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.