Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting direct AND first child in CSS

Tags:

html

css

I have an html setup like this:

<div class = "myClass">

     <div>
         <a>Content</a>
         <p><a>Content</a></p>  
     </div>


     <p><a>Content to CHANGE!</a></p>
     <p>Content</p>

</div>

I simply want to add 10px margin-top to the one labeled "Content to Change". This <p> is a direct child of class="myClass" I beleive and it's the FIRST one that is a <p>;

however this CSS style isn't working:

.myClass p:nth-child(1) {
    margin-top: 10px;
}

OR

.myClass > p:nth-child(1) {
    margin-top: 10px;
}

Anyone see why?

like image 274
TheLettuceMaster Avatar asked Aug 01 '13 16:08

TheLettuceMaster


1 Answers

Because p is not the first child of .myClass. The <div> is. Use:

.myClass p:first-of-type

You may also want to use

.myClass > p:first-of-type

to select the child explicitly.

like image 159
Explosion Pills Avatar answered Oct 31 '22 08:10

Explosion Pills