14

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
jdross
  • 1,188
  • 2
  • 11
  • 30

2 Answers2

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 
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
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

CamelCamelCamel
  • 5,200
  • 8
  • 61
  • 93