CSS层叠样式表—CSS伪类选择器

伪类选择器

学习目标

在这里插入图片描述

伪类选择器

  1. :first-child 选择器:匹配其父元素中的第一个子元素。
  2. :last-child 选择器:用来匹配父元素中最后一个子元素。
  3. :nth-child(n) 选择器:匹配父元素中的第 n 个子元素,元素类型没有限制。奇数和偶数是可以作为关键字使用,用于相匹配的子元素,其索引是odd等同于nth-child(2n-1)或even等同于nth-child(2n),nth-child(an+b) 公式计算 n可以为0。
  4. :before 选择器在被选元素的“内容”前面插入内容,用来和content属性一起使用。
  5. :after选择器在被选择元素的“内容”后面插入内容,用来和content属性一起使用。
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>伪类选择器</title>
    <style>
        div p:first-child{
            color:red;
        }
        div p:last-child{
            color:red;
        }
        div p:nth-child(2){
            color:green
        }
        div p:nth-child(odd){
            color:blue;
        }
        div p:nth-child(even){
            color:yellow;
        }
        div p:nth-child(3n+1){
            color:yellow;
        }
        i:before{
            content:"H";
        }
        i:after{
            content:"d";
        }
    </style>
</head>
<body>
    <div>
        <p>子元素</p>
        <p>子元素</p>
        <p>子元素</p>
        <p>子元素</p>
        <span>子元素</span>
        <p>子元素</p>
        <p>子元素</p>
        <p>子元素</p>
    </div>
    <div>
        <p>子元素</p>
    </div>
    <div>
        文字内容
    </div>
    <div>
        
    </div>
    <div></div>
    <i>ello worl</i>
</body>
</html>

课程总结

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值