简体   繁体   English

多个:nth-​​child语句

[英]multiple :nth-child statements

I have a grid of posts, and I am trying to give each title between h2 tags a different color (green, red, blue - over and over again that pattern). 我有一阵子的帖子,我试图给h2标签之间的每个标题赋予不同的颜色(绿色,红色,蓝色-重复该图案)。

the html (simplified)is like this: 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>

I have tried several approaches and the closest I have gotten to targeting that anchor and change its color is with this: 我尝试了几种方法,最接近目标锚并更改其颜色的方法是这样的:

.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*/

} }

But the only one that works is the red one, if I use them all at the same time it applies the last color to all the titles. 但是唯一起作用的是红色,如果我同时使用它们,它将最后一种颜色应用于所有标题。

I tried this CSS: Can't get multiple :nth-child selectors to work but didn't work, anyone can point me to the right direction? 我尝试了以下CSS:无法使多个:nth-​​child选择器起作用但没有起作用,任何人都可以指出我正确的方向吗?

try using (-n+1) 尝试使用(-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*/}

Assuming this would be your continuation of HTML (which your question doesn't have) then you can achieve this with nth-of-type instead of nth-child 假设这是您的HTML延续(您的问题没有),那么您可以使用nth-of-type而不是nth-child

Pay attention that instead of nth-of-type(3n+1) , for reaching items 1,4,7 and so on, due to your code having a sibling as first div with content you can't use the 1st item, so the counting changes then you should count 3n+4 , meaning, it will count from 4 to 7 to 10, and so on. 请注意,到达项1,4,7等时,不是nth-of-type(3n+1) ,因为您的代码具有同级内容作为第一div ,因此您不能使用第一个项,因此计数变化,那么您应该计数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> 

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

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