селектор n-го типа не работает должным образом - PullRequest
0 голосов
/ 30 сентября 2018

Я хочу выбрать только первое h2 и первое p из #section_5, поэтому я использовал этот CSS:

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

Но он выбирает все остальные дочерние элементы #section_5, которыене удовлетворяет селектор: 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-->

Не могли бы вы пролить свет на это, или я делаю это неправильно?

1 Ответ

0 голосов
/ 30 сентября 2018

Вам необходимо рассмотреть селектор > , чтобы избежать выбора вложенных элементов.Ваш селектор выберет все div, которые nth-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-->
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...