css - 使用 CSS 仅选择元素的第二个子元素

标签 css css-selectors

我有这样的东西:

<div id="someID">
   <div class="text">
     -----other html tags
   </div>
   <div class="text">
      -----other html tags
   </div>
   <div class="text">
     -----other html tags
   </div>
</div>

还有一些文本 div 的 CSS。可以为第二个 div 设置不同的 CSS 与文本类吗?

最佳答案

您可以轻松地使用 nth-child 来完成:

#someID .text:nth-child(2) {
 background:red;   
}

演示:http://jsfiddle.net/P6FKf/

关于css - 使用 CSS 仅选择元素的第二个子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18471970/

相关文章:

css - 我可以合并 :nth-child() or :nth-of-type() with an arbitrary selector?

css - 是否有 CSS 父级选择器?

javascript - 如何让div以100%的宽度和高度填充其容器?

javascript - jQuery同位素无效渲染

javascript - 将不透明的图像绘制到 Canvas 上

html - 特定元素上的 CSS `:first-child`

javascript - 使用正则表达式通过属性名称和值查找 DOM 元素

CSS 规则排除父类

html - 如何使用 CSS 为 <p> 元素中的每一行自动编号

javascript - 在循环中重复单词