CSS - 选择除第一个之外的所有子项

标签 css

我有以下内容:

<div class="chapter"> 
     <p>chapter 1, paragraph 1</p>
     <p>chapter 1, paragraph 2</p>
     <div class="section">
          <p>chapter 1, section 1, paragraph 1</p>
          <p>chapter 1, section 1, paragraph 2</p>
     </div>
</div>
<div class="chapter"> 
     <p>chapter 2, paragraph 1</p>
     <div class="section">
          <p>chapter 2, section 1, paragraph 1</p>
          <p>chapter 2, section 1, paragraph 2</p>
     </div>
     <div class="section">
          <p>chapter 2, section 2, paragraph 1</p>
          <p>chapter 2, section 2, paragraph 2</p>
     </div>
</div>

使用CSS,是否可以选择所有元素<p> ,除了第一个,对于每个元素 .chapter.section ?例如,我想选择 <p>chapter 1, paragraph 2</p> , <p>chapter 1, section 1, paragraph 2</p> , <p>chapter 2, section 1, paragraph 2</p> , <p>chapter 2, section 2, paragraph 2</p> ,等等。

谢谢。

最佳答案

您应该将 :first-of-type:not 结合使用

.chapter > p:not(:first-of-type),
.section > p:not(:first-of-type){
  color:#ccc
}
<div class="chapter"> 
     <p>chapter 1, paragraph 1</p>
     <p>chapter 1, paragraph 2</p>
     <div class="section">
          <p>chapter 1, section 1, paragraph 1</p>
          <p>chapter 1, section 1, paragraph 2</p>
     </div>
</div>
<div class="chapter"> 
     <p>chapter 2, paragraph 1</p>
     <div class="section">
          <p>chapter 2, section 1, paragraph 1</p>
          <p>chapter 2, section 1, paragraph 2</p>
          <p>chapter 2, section 1, paragraph 3</p>
          <p>chapter 2, section 1, paragraph 4</p>
     </div>
     <div class="section">
          <p>chapter 2, section 2, paragraph 1</p>
          <p>chapter 2, section 2, paragraph 2</p>
     </div>
</div>

关于CSS - 选择除第一个之外的所有子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27083415/

相关文章:

html - 表单标签自动从 HTML 中移除

javascript - 更改具有不同宽度的元素目标

html - 可选择的输入字段和文本区域,但在 Firefox 中没有其他内容可选择?

javascript - Prestashop 产品图像调整大小

java - 将带有 css 的 Jsp 转换为 pdf

表格单元格中的 HTML/css 文本不缩进

html - 两个带有空白 :nowrap elements inside a flex container 的 50% div

javascript - 导航栏,响应式导航有问题

javascript - 在动态生成的元素中覆盖 css

python - 在 Django 中链接静态文件目录