简体   繁体   English

类型选择器无法正常工作

[英]nth-of-type selector not working as expected

I only want to select the first h2 and first p from #section_5 , thus I used this CSS: 我只想从#section_5选择第一个h2和第一个p ,因此我使用了以下CSS:

#section_5 div:nth-of-type(1) h2{
    color:green;
}
#section_5 div:nth-of-type(1) p{
    color:blue;
}

But it selects all other children divs of #section_5 which doesn't seem to satisfy the selector: nth-of-type(1) 但是它会选择#section_5所有其他子div,这似乎不满足选择器的要求: nth-of-type(1)

 /*style the more features title*/ #section_5 div:nth-of-type(1) h2{ color:green; } /*style the more features paragraph*/ #section_5 div:nth-of-type(1) p{ color:blue; } 
 <div id="section_5"> <div class="row just_space1 title"> <div class="col-12 text-center"> <h2>The Limitation Of Design Is Never Ending With Our Features</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vero quod consequuntur quibusdam, enim expedita sed quia nesciunt incidunt accusamus necessitatibus modi adipisci officia libero accusantium esse hic, obcaecati, ullam, laboriosam!</p> </div> </div> <div> <div> <div> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> <div> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> </div> <!-- /Features column 1 --> <!--Image to style the section--> <div class="col-12 col-md-8"> <p> ssss </p> </div> <div> <div> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> <div> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> </div> </div> </div> <!--/Section 5--> 

Could you shed the light on this, or am I doing this wrong? 您能说明这一点,还是我做错了?

You need to consider the > selector to avoid selecting nested elements. 您需要考虑>选择器,以避免选择嵌套元素。 Your selector will select all the div that are nth-of-type(1) at any level not only the div that are child of #section5 . 您的选择会选择所有divnth-of-type(1)在任何级别的不只是div是儿童#section5

 /*style the more features title*/ #section_5 > div:nth-of-type(1) h2 { color: green; } /*style the more features paragraph*/ #section_5 > div:nth-of-type(1) p { color: blue; } 
 <div id="section_5"> <div class="row just_space1 title"> <div class="col-12 text-center"> <h2>The Limitation Of Design Is Never Ending With Our Features</h2> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Vero quod consequuntur quibusdam, enim expedita sed quia nesciunt incidunt accusamus necessitatibus modi adipisci officia libero accusantium esse hic, obcaecati, ullam, laboriosam!</p> </div> </div> <div> <div> <!-- this div is also nth-of-type(1) --> <div> <!-- this div is also nth-of-type(1) --> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> <div> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> </div> <div class="col-12 col-md-8"> <p> ssss </p> </div> <div> <div> <!-- this div is also nth-of-type(1) --> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> <div> <span>&#xe011;</span> <p>Danything</p> <h2>anything t</h2> <p>anything</p> </div> </div> </div> </div> <!--/Section 5--> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM