【问题标题】:Using nth-of-type to change the style of child elements使用 nth-of-type 改变子元素的样式
【发布时间】:2018-03-12 20:21:45
【问题描述】:

.indexNewsWidget .col-xs-6 .row-of-4-text-window h3 {
    background: pink;
}

.indexNewsWidget .col-xs-6 .row-of-4-text-window h3:nth-of-type(3), .indexNewsWidget .col-xs-6 .row-of-4-text-window h3:nth-of-type(4) {
    background: blue !important;
}
<div class="indexNewsWidget indexNewsWidgetLocal">
<div class="row">

<div class="col-sm-3 col-xs-6">
<a>
  <div class="responsiveImage"></div>
  </a>
<div class="row-of-4-text-window">
  <h3>
    <a>text</a>
  </h3>
<p class="hidden-xs">more text...</p>
    </div>
</div>
  
  <div class="col-sm-3 col-xs-6">
<a>
  <div class="responsiveImage"></div>
  </a>
<div class="row-of-4-text-window">
  <h3>
    <a>text</a>
  </h3>
<p class="hidden-xs">more text...</p>
    </div>
</div>
  
  <div class="col-sm-3 col-xs-6">
<a>
  <div class="responsiveImage"></div>
  </a>
<div class="row-of-4-text-window">
  <h3>
    <a>text</a>
  </h3>
<p class="hidden-xs">more text...</p>
    </div>
</div>
  
  <div class="col-sm-3 col-xs-6">
<a>
  <div class="responsiveImage"></div>
  </a>
<div class="row-of-4-text-window">
  <h3>
    <a>text</a>
  </h3>
<p class="hidden-xs">more text...</p>
    </div>
</div>

</div>
</div>

我正在尝试使最后两个文本具有蓝色背景。不知道我在这里做错了什么,但 nth-of-type(3) (和 4)和 nth-child(3) (和 4)似乎不能解决问题。

我在这里做错了什么?有人可以解释为什么我的解决方案不起作用吗?我很确定我以前用过很多次

塔

【问题讨论】:

  • 您正在将nth-child(3) 或其他任何内容应用于h3,而 tehre 只是每个 div 中的其中一个。您需要应用nth 来计算超父元素然后 样式h3

标签: html css


【解决方案1】:

我想这就是你要找的,

.indexNewsWidget .col-xs-6:nth-of-type(3) .row-of-4-text-window h3, 
.indexNewsWidget .col-xs-6:nth-of-type(4) .row-of-4-text-window h3 {
    background: blue !important;
}

您的代码不起作用,因为您指向的是 .row-of-4-text-window 内的第三和第四个 &lt;h3&gt; 而不是第三和第四个 .col-xs-6 内的 &lt;h3&gt;。您可以在 HTML 中添加更多 &lt;h3&gt; 以查看差异。

希望这会有所帮助。

【讨论】:

  • 很高兴我能提供帮助。您可以选择正确的答案,以帮助其他用户。谢谢。
  • 当然 - 只是在等待剩下的 6 分钟到期 :) 全部完成
猜你喜欢
  • 1970-01-01
  • 2012-11-21
  • 2012-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多