Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS children Selector

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

like image 213
jdross Avatar asked Sep 30 '11 20:09

jdross


People also ask

How do I select a child in CSS?

The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.

How do I select all 3 children in CSS?

You start with -n , plus the positive number of elements you want to select. For example, li:nth-child(-n+3) will select the first 3 li elements.

How do you target only a child in CSS?

The :only-child CSS pseudo-class represents an element without any siblings. This is the same as :first-child:last-child or :nth-child(1):nth-last-child(1) , but with a lower specificity.

What does the child selector do?

The child selector selects all elements that are the children of a specified element.


2 Answers

#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 
like image 99
John Hartsock Avatar answered Oct 02 '22 18:10

John Hartsock


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

like image 34
CamelCamelCamel Avatar answered Oct 02 '22 18:10

CamelCamelCamel