1. :nth-child(n)
栗子:
规定属于其父元素的第二个子元素的每个 p 的背景色:
<!DOCTYPE html><html><head><style> p:nth-child(2){background:#ff0000;}</style></head><body><h1>这是题目</h1><p>第一个段落。</p><p>第二个段落。</p><p>第三个段落。</p><p>第四个段落。</p><p><b>正文:</b>Internet Explorer 不反对 :nth-child() 选择器。</p></body></html>
成果
p:nth-child(2)
n示意父元素的第n个元素,即便p的上一个元素不为p元素,也要从父元素的第一个开始计数。
小结:
n 从1开始
n 能够是数字、关键词或公式。
CSS3 :nth-child() 选择器