css - 与第 nth-child 造型

标签 css css-selectors

我想要完成的是,前两个部分是白色的,然后是两个灰色部分,然后继续该模式。

我制作了一个 jsfiddle 来展示我目前拥有的东西:jsfiddle

CSS:

section{
      float:left;
      width:40%;
      background:#ccc;
      padding:20px 25px;
      margin-bottom:2px
}
section:nth-child(even){
      margin-left:2px;
}
section:nth-child(1),
section:nth-child(2n+2){
      background:#fff;
}

HTML:

<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>
<section></section>

我是否需要使用另一个 :nth-child() 才能使其正常工作?或者使用 jquery 会更好吗?

PHP 被用来生成这些框,那么 php 方法是最好的方法吗?也许上课?

最佳答案

Nth-child 是你想要的,但是你要找的是这样的:

http://tinker.io/f5c5c

section:nth-child(4n+1),
section:nth-child(4n+2){
      /* styles here */

}

关于css - 与第 nth-child 造型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021322/

相关文章:

html - 帖子注释滑过 Tumblr 永久链接页面中的帖子

html - 在 Bootstrap 表中如何删除行之间的行?

javascript - 当我切换滑动侧菜单时,如何让我的整个 body 向右滑动

python - 无法在 .select() 方法中使用多个属性

css - 使用多/双/条件 CSS 选择器时的性能如何?例如.hello1.hellob

css - 如何使用一个命令选择所有标题?

php - 添加 ?ref 后弹出框未出现

android - CSS 位置 :fixed causes blurry images in Android Browsers

jquery - 如何告诉 jQuery 仅在一个特定网页上运行某个函数?

css - 如何仅使用 CSS 选择 n>4 个元素的第一个元素?