【问题标题】:Why doesn't nth-of-type/nth-child work on nested elements?为什么 nth-of-type/nth-child 不适用于嵌套元素?
【发布时间】:2012-11-21 14:22:40
【问题描述】:

我正在尝试更改 div 中奇数 div 的样式。出于某种原因,nth-of-type(odd) 在另一个 div 中时会影响我的所有 div。这是我的常规 div 和奇数 div 的代码:

.video-entry-summary {
  width: 214px;
  height: 210px;
  margin-left: 10px;
  float: left;
  position: relative;
  overflow: hidden;
  border: 1px solid black;
}

.video-entry-summary:nth-of-type(odd) {
  width: 214px;
  height: 210px;
  margin-left: 0px;
  float: left;
  position: relative;
  overflow: hidden;
  border: 1px solid black;
  background: #ccc;
}
<div id="post-501" class="post-501 post type-post status-publish format-standard hentry category-moto-dz-films tag-news-sub-2">
  <div class="video-entry-summary">
    video 1
  </div>
</div>

<div id="post-240" class="post-240 post type-post status-publish format-standard hentry category-videos">
  <div class="video-entry-summary">
    video 2
  </div>
</div>

<div id="post-232" class="post-232 post type-post status-publish format-standard hentry category-videos">
  <div class="video-entry-summary">
    video 3
  </div>
</div>

<div id="post-223" class="post-223 post type-post status-publish format-standard hentry category-videos">
  <div class="video-entry-summary">
    video 4
  </div>
</div>

由于某种原因,nth-of-type 在包裹在我的 div 中时不起作用,但在它们未包裹在任何 div 中时确实起作用。

未包裹在 div 中时的工作版本:

.video-entry-summary {
  width: 214px;
  height: 210px;
  margin-left: 10px;
  float: left;
  position: relative;
  overflow: hidden;
  border: 1px solid black;
}

.video-entry-summary:nth-of-type(odd) {
  width: 214px;
  height: 210px;
  margin-left: 0px;
  float: left;
  position: relative;
  overflow: hidden;
  border: 1px solid black;
  background: #ccc;
}
<div class="video-entry-summary">
  video 1
</div>

<div class="video-entry-summary">
  video 2
</div>

<div class="video-entry-summary">
  video 3
</div>

<div class="video-entry-summary">
  video 4
</div>

​如何让初始代码和上面的一样工作?

【问题讨论】:

    标签: html css css-selectors pseudo-class


    【解决方案1】:

    :nth-of-type() 与:nth-child() 相似,因为它们都必须来自同一个父级。如果您需要那些包装器divs,请在这些包装器上使用:nth-of-type():

    div.post:nth-of-type(odd) .video-entry-summary {
        width:214px; 
        height:210px; 
        margin-left:0px;
        float:left;
        position:relative; 
        overflow:hidden; 
        border:1px solid black; 
        background:#ccc; 
    }
    

    如果所有兄弟姐妹都是.post,请改用:nth-child(),以防止与what :nth-of-type() really means混淆:

    .post:nth-child(odd) .video-entry-summary {
        width:214px; 
        height:210px; 
        margin-left:0px;
        float:left;
        position:relative; 
        overflow:hidden; 
        border:1px solid black; 
        background:#ccc; 
    }
    

    .video-entry-summary {
      width: 214px;
      height: 210px;
      margin-left: 10px;
      float: left;
      position: relative;
      overflow: hidden;
      border: 1px solid black;
    }
    
    .post:nth-child(odd) .video-entry-summary {
      width: 214px;
      height: 210px;
      margin-left: 0px;
      float: left;
      position: relative;
      overflow: hidden;
      border: 1px solid black;
      background: #ccc;
    }
    <div id="post-501" class="post-501 post type-post status-publish format-standard hentry category-moto-dz-films tag-news-sub-2">
      <div class="video-entry-summary">
        video 1
      </div>
    </div>
    
    <div id="post-240" class="post-240 post type-post status-publish format-standard hentry category-videos">
      <div class="video-entry-summary">
        video 2
      </div>
    </div>
    
    <div id="post-232" class="post-232 post type-post status-publish format-standard hentry category-videos">
      <div class="video-entry-summary">
        video 3
      </div>
    </div>
    
    <div id="post-223" class="post-223 post type-post status-publish format-standard hentry category-videos">
      <div class="video-entry-summary">
        video 4
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2013-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多