views:

49

answers:

4

I have the following

<p class="main yellow">Hello World</p>

I would like to write a css element that refers to only elements with main and yellow. Is there a way to do this?

Eg. the following doesn't work, but would be what I'm after

.main + .yellow { color:green }
+1  A: 

You just need to specify them as

.main.yellow { color: green; }

No space between the two classes.

dj2
+1  A: 

does this work for you?

.main.yellow{
  color:green;
}
scunliffe
+3  A: 

This should grab it:

.main.yellow { color:yellow; }

Though you may get differing results in different browsers. I use QuirksMode to get an idea of what will/won't work cross browser.

PeterJCLaw
I think the second gets elements with the class "yellow", that are nested (any depth) within elements with the "main" class
scunliffe
The first is correct, but fails in IE6.
bobince
is the order respected?
BlueFish
yeah - you're right - I misread the question, and I've removed that line
PeterJCLaw
@BlueFish: I believe the order *should* be respected, and as scunlife says the latter class will grab children of the former for space separated classes.
PeterJCLaw
A: 

As others have already said, what you want is:

.main.yellow { color:green; }

However, let me quickly explain why your first attempt didn't work. The + keyword refers to a following element, i.e. the element after.

Your example would have matched the following HTML...

<p class="main">Hello</p>
<p class="yellow">World</p>

...and styled the second paragraph (.yellow) green. So ".main + .yellow" means "select a .yellow that is immediately after a .main".

Jeremy Visser