1

What I want to do is something like:

#div_id > .some_class 
{
}

I don't want to change the class everywhere. I only want to change the class if it in that particular div.

Is ther some other way to do that same thing?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
GC_
  • 1,673
  • 6
  • 23
  • 39

4 Answers4

1

Your question already contains the child combinator CSS selector and will target the elements with class .some_class that are children of the element with id div_id, so if you have only one <div> with an id of div_id then it will only target the child elements with the class some_class. So it should work as already expected, except in IE6 of course which does not support that selector natively.

If you want to select grandchildren, use the descendant combinator.

  • Child combinator body > p
  • Descendant combinator body p
andyb
  • 43,435
  • 12
  • 121
  • 150
1

You've already stumbled upon the answer yourself:

#div_id > .class {
    /* CSS magic */
}

This selects .class if it is the direct descendant of #div_id. For all descendants regardless of depth, use the selector #div_id .class instead.

See also this JSFiddle.

You
  • 22,800
  • 3
  • 51
  • 64
  • You know, honestly, I don't know why people need to say "*direct* child". The word "direct" is pretty much implied. – BoltClock Jul 27 '11 at 21:20
  • @BoltClock: True. Replaced "child" with "descendant", which is what I really meant anyway. – You Jul 27 '11 at 21:45
0

You essentially have the answer there. If you want to modify all classes with in a div then the selector would be div#id .this_class. If it's just one instance of the class inside the div (say you have a div called 'test' with three divs with a class of 'test_class' then you could either use the :nth-child() selector or the :first-of-type selector.

ayyp
  • 6,590
  • 4
  • 33
  • 47
0

Your code looks fine to me. Note that the > operator will only affect the children of the DIV not any lower decendants (i.e. grandchildren). Remove the > to affect everything inside the DIV with the class .some_class.

russellfeeed
  • 617
  • 8
  • 10