CSS3新增选择器之伪类选择器

CSS3新增选择器

1.结构(位置)伪类选择器

1、E:nth-child(n):匹配元素类型为E且是父元素的第n个子元素
2、E:nth-last-child(n):匹配元素类型为E且是父元素的倒数第n个子元素(与上一项顺序相反)
3、E:first-child:匹配元素类型为E且是父元素的第一个子元素
4、E:last-child:匹配元素类型为E且是父元素的最后一个子元素
5、E:only-child:匹配元素类型为E且是父元素中唯一的子元素
6、E:nth-of-type(n):匹配父元素的第n个类型为E的子元素
7、E:nth-last-of-type(n):匹配父元素的倒数第n个类型为E的子元素(与上一项顺序相反)
8、E:first-of-type:匹配父元素的第一个类型为E的子元素
9、E:last-of-type:匹配父元素的最后一个类型为E的子元素
10、E:only-of-type:匹配父元素中唯一子元素是E的子元素

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <style>
        .list div:nth-child(2){
            background-color:red;
        }
        .list div:nth-last-child(2){
            background-color: blue;
        }
        .list div:first-child{
            background-color:black;
        }
        .list div:last-child{
            background-color:green;
        }
        .box div:only-child{
            background-color:orange;
        }
       .list div:nth-of-type(3){
            background-color:yellow;
       }
       .box div:nth-last-of-type(2){
            background-color:pink;
       }
       .box div:first-of-type{
            background-color:purple;
       }
       .box div:last-of-type{
            background-color:cyan;
       }
       .content div:only-of-type{
            background-color:gray;
       }
    </style>
</head>
<body>
    <div class="list">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
    </div>
    <div class="box">
        <div>6</div>
        <div>7</div>
        <div>8</div>
    </div>
    <div class="content">
        <div>9</div>
    </div>
</body>
</html>

在这里插入图片描述

目标伪类选择器

E:targe:URL后面跟锚点#,指向文档内某个具体的元素。 也就是说,url后面的锚点,指向 某个元素, 那么该元素就会触发 target

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=<d>, initial-scale=1.0">
    <title>Document</title>
</head>
    <style>
       div{
        width: 100px;
        height: 100px;
        background-color: #f00;
       }
    #box1:target{
        background-color: blue;
    }
    #box2:target{
        background-color: green;
    }
    </style>
<body>
    <a href="#box1">box1</a>
    <a href="#box2">box2</a>
    <div id="box1"></div>
    <br>
    <div id="box2"></div>
</body>
</html>>

效果图
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值