1:同级别的第几个 :first-child 选中同级别中的第一个 :last-child 选中同级别中的最后一个 :nth-child(n) 选中同级别中的第n个 :nth-last-child(n) 选中同级别中的倒数第n个 :only-child 选中父元素中只有一个子元素的元素 注意点:不区分类型 2:同类型的第几个 :first-of-type 选中同级别中同类型的第一个 :last-of-type 选中同级别中同类型的最后一个 :nth-of-type(n) 选中同级别中同类型的第n个 :nth-last-of-type(n) 选中同级别中同类型的倒数第n个 :only-of-type 选中父元素中子元素的类型只有一种的元素 例如:
<style> /*选中同级别中的第n个元素 如果元素匹配则生效,否则,不生效。*/ p:nth-child(4){ color: red; } /*选择同级别并且同类型的第n个元素 只会查找类型匹配元素中的第n个*/ p:nth-of-type(3){ color: yellow; } /*选中同级别中的倒数第n个元素 如果元素匹配则生效,否则,不生效。*/ p:nth-last-child(2){ color: yellow; } /*选择同级别并且同类型的倒数第n个元素 只会查找类型匹配元素中的第倒数第n个*/ p:nth-last-of-type(2){ color: yellow; } </style>