tags:

views:

31

answers:

2

I'm trying to style all html with the class called sample

<div class="sample">
</div>

.sample{

}

But I'm running into a situation where the class sample could be nested. In this case I want to style only the inner one. How can I tell the css to apply the style only to the inner div and not to the outer sample? Can this be done?

<div class="sample">
   <div class="sample">
   </div>
</div>
+2  A: 

Use the descendant selector for your ruleset:

.sample .sample

If you want to maintain the style on your outer div, then I'd suggest giving it another class, like

.something .sample

Using the same class for all the elements on a page is highly discouraged.

Yi Jiang
+1  A: 

You could use Child Selectors:

div>.sample {}

or:

div.sample>.sample {}
STW
NB the child selector ">" is not supported by IE6. Ref http://kimblim.dk/css-tests/selectors/
Jonathan Day
workarounds, for "the nearly dead one": http://craftycodeblog.com/2010/05/19/emulating-css-child-selectors-in-ie6/
STW