选择器(代码)

属性选择器

1、🔨 [class]

<style>
        [class]{
            background-color: pink;
        }
    </style>
    /*选中所有具有class属性的元素*/

2、🔨 [class=‘one’]

 <style>
        [class='one']{
            background-color: pink;
        }
    </style>
    /*选中class属性值为one的元素*/

3、🔨 [class~=‘one’]

<style>
        [class~='one']{
            background-color: pink;
        }
    </style>
    /*选中class属性值之一为one的元素*/

4、🔨 [class^=‘one’]

<style>
        [class^='one']{
            background-color: pink;
        }
    </style>
    /*选中class属性值以one开头的元素*/

4、🔨 [class$=‘one’]

<style>
        [class$='one']{
            background-color: pink;
        }
    </style>
    /*选中class属性值以one结尾的元素*/

5、🔨 [class*=‘one’]

<style>
        [class^='one']{
            background-color: pink;
        }
    </style>
    /*选中具有class属性,并且属性值中包含one的元素*/

6、🔨 [id]
选中具有id 属性的元素
7、🔨[id=‘one’]
选中具有id属性,且属性值为one的元素

伪类选择器(表示子元素的/与元素状态相关的)

1、表示子元素的

1、🔨:first-child

 p:first-child{
            background-color: pink;
        }
        /*选中作为p父元素的第一个子元素*/

2、🔨 :last-child

 p:last-child{
            background-color: pink;
        }
        /*选中作为p父元素的最后一个子元素*/

3、🔨 :nth-child(num/odd/even)

 p:last-child{
            background-color: pink;
        }
        /*选中作为p父元素的第nth个元素*/

4、:first-of-type

p:first-of-type{
            background-color: pink;
        }
  /*选中元素p的父元素的第一个p元素*/

5、:last-of-type

p:last-of-type{
            background-color: pink;
        }
  /*选中元素p的父元素的最后一个p元素*/

2、与元素状态相关的

:link:未点击状态
:hover:悬停状态
:active:鼠标按下状态
:visited:点击之后的状态

link->visited->hover->active

伪元素选择器

1、::first-letter

p:first-letter{
            color: pink;
        }
   /*选中p元素的文本中第一个字符*/

2、::first-line

p:first-line{
            color: pink;
        }
   /*选中p元素的文本中第一行字符*/

3、::before

p::before{
            content:url(./center_2.7.png);
        }
 /*在p元素里面的文本内容之前添加一张图片*/
 p::before{
            content: 'nihao ';
        }
  /*在p元素里面的文本内容之前添加文本*/

4、::after

p::after{
            content:url(./center_2.7.png);
        }
 /*在p元素里面的文本内容之后添加一张图片*/
 p::after{
            content: 'nihao ';
        }
  /*在p元素里面的文本内容之后添加文本*/

5、::selection(设置文本被选中之后的状态)

p::selection{
            color: red;
        }
 /*在p元素内的文本被选中以后颜色变成红色*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值