CSS3选择器

一:伪类结构选择器
在这里插入图片描述

	<style>
        ul li:first-child {
            background-color: pink;
        }
        
        ul li:last-child {
            background-color: red;
        }
        
        ul li:nth-child(6) {
            background-color: aqua;
        }
    </style>	

二:nth-child参数详解

  1. 本质上就是选中第几个子元素,使用不同的n
  2. n可以是关键字,数字,公式
  3. n如果是第几个元素就是选中第几个
  4. 常见的关键字:even表示偶数;odd表示奇数
  5. 如果n是公式,则从0开始计算;第0个元素或者超出元素的个数则忽略
	<style>
        /* 选中偶数 */
        
        ul li:nth-child(even) {
            background-color: aqua;
        }
        /* 选中奇数 */
        
        ul li:nth-child(odd) {
            background-color: pink;
        }
        /* 选中偶数 */
        
        ul li:nth-child(2n) {
            background-color: aqua;
        }
        /* 选中奇数 */
        
        ul li:nth-child(2n+1) {
            background-color: pink;
        }
        /* 选中前5个 */
        
        ul li:nth-child(-n+5) {
            background-color: red;
        }
        /* 选中从5开始的的元素 */
        
        ul li:nth-child(n+5) {
            background-color: red;
        }
    </style>

三:nth-child和nth-of-type的区别

  1. nth-child:选择父元素里面的第几个子元素,不区分类型
  2. nth-of-type:选择指定类型的元素
<style>
		div span:first-of-type {
            background-color: red;
        }
        
        div span:last-of-type {
            background-color: pink;
        }
        
        div span:nth-of-type(2) {
            background-color: blue;
        }
</style>

四:伪元素选择器
在这里插入图片描述

  1. befor和after必须有content属性
  2. before在内容的前面,after在内容的后面
  3. before和after创建一个元素,但属于行内元素
  4. 因为在dom里面看不见刚才创建的元素,所以我们称之为伪元素
	<style>
        div {
            color: pink;
        }
        
        div::after {
            width: 50px;
            height: 50px;
            text-align: center;
            display: inline-block;
            content: '之阔'
        }
        
        div::before {
            width: 50px;
            height: 50px;
            text-align: center;
            display: inline-block;
            content: '绚丽'
        }
    </style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值