2016-06-01 2 views
0

У меня есть сетка сообщений, и я пытаюсь дать каждому заголовку между тегами h2 другой цвет (зеленый, красный, синий - снова и снова).multiple: nth-child statements

HTML, (упрощенный), как это:

<div class="fusion-posts-container"> 
     <div> 
     <div>      
      <div> 
       <ul> 
       <li> 
        <div> 
        <img> 
         <div> 
          <div> 
          <a></a> 
          <div></div> 
          <a></a> 
          <h4><a></a></h4> 
          <div> 
           <a></a> 
          </div> 
          </div> 
         </div> 
        </div> 
        </li> 
       </ul> 
      </div> 
     <div class="fusion-post-content-wrapper"> 
      <div class="fusion-post-content post-content"> 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 

Я попробовал несколько подходов, и ближе я получил к ориентации, что якорь и изменить его цвет с этим:

.fusion-posts-container div:nth-child(3n+3) a{ 
color: #b7e352 !important;/*red*/ 
} 

.fusion-posts-container div:nth-child(3n+1) a{ 
color: #fb5322 !important;/*green*/ 
} 
.fusion-posts-container div:nth-child(3n+2) a{ 
color: #1592b0 !important;/*blue*/ 

}

Но только тот, который работает, является красным, если я использую их все одновременно, он применяет последний цвет ко всем названиям.

Я пробовал это CSS: Can't get multiple :nth-child selectors to work, но не работал, любой может указать мне в правильном направлении?

+0

показывают конец HTML – dippas

ответ

0

Предполагая, что это будет ваше продолжение HTML (что ваш вопрос не имеет), то вы можете добиться этого с nth-of-type вместо nth-child

Обратите внимание, что вместо nth-of-type(3n+1), для достижения позиций 1,4,7 и т. Д. Из-за того, что ваш код имеет родной брат, как первый div с контентом, вы не можете использовать 1-й элемент, поэтому счёт меняется, тогда вы должны считать 3n+4, то есть он будет считаться от 4 до 7 до 10 и т. д.

.fusion-posts-container div:nth-of-type(3n+4) a { 
 
    color: blue 
 
} 
 
.fusion-posts-container div:nth-of-type(3n+3) a { 
 
    color: red 
 
} 
 
.fusion-posts-container div:nth-of-type(3n+2) a { 
 
    color: green 
 
}
<div class="fusion-posts-container"> 
 
    <div> 
 
    <div> 
 
     <div> 
 
     <ul> 
 
      <li> 
 
      <div> 
 
       <img> 
 
       <div> 
 
       <div> 
 
        <a></a> 
 
        <div></div> 
 
        <a></a> 
 
        <h4><a></a></h4> 
 
        <div> 
 
        <a></a> 
 
        </div> 
 
       </div> 
 
       </div> 
 
      </div> 
 
      </li> 
 
     </ul> 
 
     </div> 
 
     <div class="fusion-post-content-wrapper"> 
 
     <div class="fusion-post-content post-content"> 
 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 
 
     </div> 
 
     </div> 
 
     <div class="fusion-post-content-wrapper"> 
 
     <div class="fusion-post-content post-content"> 
 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 
 
     </div> 
 
     </div> 
 
     <div class="fusion-post-content-wrapper"> 
 
     <div class="fusion-post-content post-content"> 
 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 
 
     </div> 
 
     </div> 
 
     <div class="fusion-post-content-wrapper"> 
 
     <div class="fusion-post-content post-content"> 
 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 
 
     </div> 
 
     </div> 
 
     <div class="fusion-post-content-wrapper"> 
 
     <div class="fusion-post-content post-content"> 
 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 
 
     </div> 
 
     </div> 
 
     <div class="fusion-post-content-wrapper"> 
 
     <div class="fusion-post-content post-content"> 
 
      <h2 class="entry-title"><a>THIS TITLE</a></h2> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

0

попробуйте использовать (-n + 1)

.fusion-posts-container div:nth-child(-n + 1) a{color: #b7e352 !important;/*red*/} 

.fusion-posts-container div:nth-child(-n + 2) a{color: #fb5322 !important;/*green*/} 

.fusion-posts-container div:nth-child(-n + 3) a{color: #1592b0 !important;/*blue*/}