CSS nth-child

:nth-child 选择器

:nth-child() 选择器可以指定父元素中一个或多个子元素
兼容性:IE8+

:nth-child(2) 选取第2个标签

	/* :nth-child(2)选取第2个标签 */
	li:nth-child(2){
		background:#ff0000
	}

:nth-child(n+4) 选取大于等于4的标签

	/* :nth-child(n+4)选取大于等于4的标签 */
	li:nth-child(n+4){
		background:#ff0000
	}

:nth-child(-n+4) 选取小于等于4的标签

	/* :nth-child(-n+4)选取小于等于4的标签 */
	li:nth-child(-n+4){
		background:#ff0000
	}

:nth-child(even) 选取偶数标签,even可以写成2n(倍数写法)

	/* :nth-child(even)选取偶数标签(2,4,6,8...) */
	li:nth-child(even){
		background:#ff0000
	}
			||
	/* :nth-child(2n)选取偶数标签,所有2的倍数(2,4,6,8...) */
	li:nth-child(2n){
		background:#ff0000
	}

:nth-child(odd) 选取奇数标签 ,odd可以写成2n-1

	/* :nth-child(odd)选取奇数标签(1,3,5,7...) */
	li:nth-child(odd){
		background:#ff0000
	}
			||
	/* :nth-child(2n-1)选取奇数标签,所有2的倍数减1(2-1=1,4-1=3,6-1=5,8-1=7,...) */
	li:nth-child(2n-1){
		background:#ff0000
	}

:nth-child(3n+1) 倍数分组匹配,3n+1表示“隔二取一”

	/* :nth-child(3n+1)从第1个开始每3个一组(1,4,7,10...) */
	li:nth-child(3n+1){
		background:#ff0000
	}
	/* 拓展 */
	/* :nth-child(3n+5)从第5个开始每3个一组(5,8,11,14...) */
	li:nth-child(3n+1){
		background:#ff0000
	}

:first-child 选取第一个标签

注意:因为不需要参数,结尾没有括号

	/* :first-child 选取第一个标签 */
	li:first-child{
		background:#ff0000
	}

:last-child 选取最后一个标签

注意:因为不需要参数,结尾没有括号

	/* :last-child 选取最后一个标签 */
	li:last-child{
		background:#ff0000
	}

:nth-last-child(3) 选取倒数第3个标签

	/* :nth-last-child(3) 选取倒数第3个标签 */
	li:nth-last-child(3){
		background:#ff0000
	}

只为记录,内容有借鉴,勿喷!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值