15

Lets say I have the following HTML

<div id="div1"> .... <span class="innercontents">...</span> .... </div> 

Can I select just the child of the parent ID?

Could I do something like

#div1 span { ... } 

Thanks for any help.

Sorry for any confusion. I should have been more clear. In the above example I would like to just select the tags that fall under that specific

4
  • 2
    What should the selector match? The <div> or the <span>? (If the <div>, then what you're asking for isn't possible.) Commented Sep 30, 2011 at 20:43
  • 1
    #div1 > .innercontents Commented Sep 30, 2011 at 20:43
  • 1
    "Can I select just the child classes of the parent ID?" => that sentence is very confusing. Commented Sep 30, 2011 at 20:43
  • #div1 > .innercontents stackoverflow.com/questions/1182189/… Commented Sep 30, 2011 at 20:45

2 Answers 2

26
#div1 > .innercontents /* child selector */ 

The above will select these ids from the following HTML: c and d

<div id="div1"> <div id="a"> <span id="b" class="innercontents"></span> </div> <span id="c" class="innercontents"></span> <span id="d" class="innercontents"></span> </div> 

if you want all descendents selected such as b, c, and d from the above HTML then use

#div1 .innercontents 
Sign up to request clarification or add additional context in comments.

Comments

3

Yes. #div1 > .innercontents. This is the immediate descendent selector, or child selector.

This is the best reference for CSS selectors: http://www.w3.org/TR/css3-selectors/#selectors

2 Comments

#div1 .innercontents is a descendant selector, not a child selector.
#div1 > .innercontents then, for immediate descendents.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.