html - 为什么第 nth-of-type/nth-child 不适用于嵌套元素?

标签 html css css-selectors pseudo-class

我正在尝试更改 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>

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

最佳答案

:nth-of-type():nth-child() 相似,因为它们必须都来自同一父级。如果您需要那些包装器 div,请在这些包装器上使用 :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>

关于html - 为什么第 nth-of-type/nth-child 不适用于嵌套元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59678782/

相关文章:

html - 为什么 firefox 将 align ="center"更改为 align ="middle"?

javascript - 单击时的 div 重定向到另一个 HTML 页面并调用 javascript 以显示特定的 div

CSS 空白 : nowrap; broke p tag

css - css 中的兄弟选择器

html - 导航和内容之间的差距?

html - 在 div 悬停时更改其他 div css

html - 如何在响应式表格内设置文本 - 单元格和表格的纵横比应为 1 :1

javascript - 为什么博主在页面加载之前显示齿轮图像?

html - 在没有 !important 的情况下从 CSS 文件覆盖内联 CSS

html - 如何防止 Cufón 替换某个类下的元素中的文本?