为啥 nth-of-type/nth-child 不适用于嵌套元素?

Posted

技术标签:

【中文标题】为啥 nth-of-type/nth-child 不适用于嵌套元素?【英文标题】: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>

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

【问题讨论】:

【参考方案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>

【讨论】:

以上是关于为啥 nth-of-type/nth-child 不适用于嵌套元素?的主要内容,如果未能解决你的问题,请参考以下文章

7.nth-of-type | nth-child?CSS

结构伪类选择器的误解(nth-of-type,nth-child等等结构伪类选择器应用在孙子辈元素上出现错误)

你应该同步运行方法吗?为啥或者为啥不?

为啥使用 glTranslatef?为啥不直接更改渲染坐标?

为啥 DataGridView 上的 DoubleBuffered 属性默认为 false,为啥它受到保护?

为啥需要softmax函数?为啥不简单归一化?