CSS(十五)——CSS伪类和伪元素

目录

CSS 伪类(Pseudo-classes)

语法

anchor伪类

伪类和CSS类

CSS :first-child 伪类

匹配第一个

元素

匹配所有

元素中的第一个 元素

匹配所有作为第一个子元素的

元素中的所有 元素

CSS - :lang 伪类

所有CSS伪类/元素

CSS 伪元素

语法

:first-line 伪元素

:first-letter 伪元素

伪元素和CSS类

多个伪元素

CSS - :before 伪元素

CSS - :after 伪元素

所有CSS伪类/元素


CSS 伪类(Pseudo-classes)

CSS伪类是用来添加一些选择器的特殊效果。

语法

anchor伪类

在支持 CSS 的浏览器中,链接的不同状态都可以以不同的方式显示

示例:

在CSS定义中,a:hover 必须被置于 a:link 和 a:visited 之后,才是有效的。

 在 CSS 定义中,a:active 必须被置于 a:hover 之后,才是有效的。

伪类的名称不区分大小写。

伪类和CSS类

伪类可以与 CSS 类配合使用:

a.red:visited {color:#FF0000;}
 
<a class="red" href="css-syntax.html">CSS 语法</a>

如果在上面的例子的链接已被访问,它会显示为红色。

CSS :first-child 伪类

可以使用 :first-child 伪类来选择父元素的第一个子元素

匹配第一个 <p> 元素

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        p:first-child {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止,风止意难平</p>
        <p> 爱意随风止,风止意难平</p>
        <p> 爱意随风止,风止意难平</p>
    </div>

</body>

</html>

运行结果:

匹配所有<p> 元素中的第一个 <i> 元素

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        p>i:first-child {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止,风止意难平<i>1</i><i>2</i></p>
        <p> 爱意随风止,风止意难平</p>
        <p> 爱意随风止,风止意难平</p>
    </div>

</body>

</html>

运行结果:

匹配所有作为第一个子元素的 <p> 元素中的所有 <i> 元素

在下面的例子中,选择器匹配所有作为元素的第一个子元素的 <p> 元素中的所有 <i> 元素

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        p:first-child i {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止,风止意难平<i>1</i><i>2</i></p>
        <p> 爱意随风止,风止意难平<i>1</i><i>2</i></p>
        <p> 爱意随风止,风止意难平</p>
    </div>

</body>

</html>

运行结果:

CSS - :lang 伪类

在下面的例子中,:lang 类为属性值为 no 的q元素定义引号的类型

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        q:lang(no) {
            quotes: "~" "~";
        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止<q lang="no">风止意难平</q></p>
        <p> 爱意随风止,风止意难平</p>
        <p> 爱意随风止,风止意难平</p>
    </div>

</body>

</html>

运行结果:

所有CSS伪类/元素

选择器示例示例说明
:checkedinput:checked选择所有选中的表单元素
:disabledinput:disabled选择所有禁用的表单元素
:emptyp:empty选择所有没有子元素的p元素
:enabledinput:enabled选择所有启用的表单元素
:first-of-typep:first-of-type选择的每个 p 元素是其父元素的第一个 p 元素
:in-rangeinput:in-range选择元素指定范围内的值
:invalidinput:invalid选择所有无效的元素
:last-childp:last-child选择所有p元素的最后一个子元素
:last-of-typep:last-of-type选择每个p元素是其母元素的最后一个p元素
:not(selector):not(p)选择所有p以外的元素
:nth-child(n)p:nth-child(2)选择所有 p 元素的父元素的第二个子元素
:nth-last-child(n)p:nth-last-child(2)选择所有p元素倒数的第二个子元素
:nth-last-of-type(n)p:nth-last-of-type(2)选择所有p元素倒数的第二个为p的子元素
:nth-of-type(n)p:nth-of-type(2)选择所有p元素第二个为p的子元素
:only-of-typep:only-of-type选择所有仅有一个子元素为p的元素
:only-childp:only-child选择所有仅有一个子元素的p元素
:optionalinput:optional选择没有"required"的元素属性
:out-of-rangeinput:out-of-range选择指定范围以外的值的元素属性
:read-onlyinput:read-only选择只读属性的元素属性
:read-writeinput:read-write选择没有只读属性的元素属性
:requiredinput:required选择有"required"属性指定的元素属性
:rootroot选择文档的根元素
:target#news:target选择当前活动#news元素(点击URL包含锚的名字)
:validinput:valid选择所有有效值的属性
:linka:link选择所有未访问链接
:visiteda:visited选择所有访问过的链接
:activea:active选择正在活动链接
:hovera:hover把鼠标放在链接上的状态
:focusinput:focus选择元素输入后具有焦点
:first-letterp:first-letter选择每个<p> 元素的第一个字母
:first-linep:first-line选择每个<p> 元素的第一行
:first-childp:first-child选择器匹配属于任意元素的第一个子元素的 <p> 元素
:beforep:before在每个<p>元素之前插入内容
:afterp:after在每个<p>元素之后插入内容
:lang(language)p:lang(it)为<p>元素的lang属性选择一个开始值

CSS 伪元素

CSS 伪元素是用来添加一些选择器的特殊效果

语法

:first-line 伪元素

"first-line" 伪元素用于向文本的首行设置特殊样式。

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        p:first-line {
            color: #ff0000;
        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止,风止意难平
            <br>
            爱意随风止,风止意难平
        </p>

    </div>

</body>

</html>

运行结果:

"first-line" 伪元素只能用于块级元素。

下面的属性可应用于 "first-line" 伪元素:

font properties

color properties 

background properties

word-spacing

letter-spacing

text-decoration

vertical-align

text-transform

line-height

clear

:first-letter 伪元素

"first-letter" 伪元素用于向文本的首字母设置特殊样式

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        p:first-letter {
            color: #ff0000;
            font-size: xx-large;
        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止,风止意难平
            <br>
            爱意随风止,风止意难平
        </p>

    </div>

</body>

</html>

运行结果:

 "first-letter" 伪元素只能用于块级元素。

下面的属性可应用于 "first-letter" 伪元素: 

font properties

color properties 

background properties

margin properties

padding properties

border properties

text-decoration

vertical-align (only if "float" is "none")

text-transform

line-height

float

clear

伪元素和CSS类

伪元素可以结合CSS类: 

p.article:first-letter {color:#ff0000;}

<p class="article">文章段落</p>

上面的例子会使所有 class 为 article 的段落的首字母变为红色。

多个伪元素

可以结合多个伪元素来使用

示例:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .center {
            position: relative;
            height: 200px;
            border: 3px solid #73AD21;
        }

        p:first-letter {
            color: #ff0000;
            font-size: xx-large;
        }

        p:first-line {
            color: #0000ff;

        }
    </style>
</head>

<body>
    <div class="center">
        <p> 爱意随风止,风止意难平
            <br>
            爱意随风止,风止意难平
        </p>

    </div>

</body>

</html>

运行结果:

CSS - :before 伪元素

":before" 伪元素可以在元素的内容前面插入新内容。

CSS - :after 伪元素

":after" 伪元素可以在元素的内容之后插入新内容。

所有CSS伪类/元素

选择器示例示例说明
:linka:link选择所有未访问链接
:visiteda:visited选择所有访问过的链接
:activea:active选择正在活动链接
:hovera:hover把鼠标放在链接上的状态
:focusinput:focus选择元素输入后具有焦点
:first-letterp:first-letter选择每个<p> 元素的第一个字母
:first-linep:first-line选择每个<p> 元素的第一行
:first-childp:first-child选择器匹配属于任意元素的第一个子元素的 <p> 元素
:beforep:before在每个<p>元素之前插入内容
:afterp:after在每个<p>元素之后插入内容
:lang(language)p:lang(it)为<p>元素的lang属性选择一个开始值

伪类选择元素基于的是当前元素处于的状态,或者说元素当前所具有的特性,而不是元素的id、class、属性等静态的标志。由于状态是动态变化的,所以一个元素达到一个特定状态时,它可能得到一个伪类的样式;当状态改变时,它又会失去这个样式。由此可以看出,它的功能和class有些类似,但它是基于文档之外的抽象,所以叫伪类。

与伪类针对特殊状态的元素不同的是,伪元素是对元素中的特定内容进行操作,它所操作的层次比伪类更深了一层,也因此它的动态性比伪类要低得多。实际上,设计伪元素的目的就是去选取诸如元素内容第一个字(母)、第一行,选取某些内容前面或后面这种普通的选择器无法完成的工作。它控制的内容实际上和元素是相同的,但是它本身只是基于元素的抽象,并不存在于文档中,所以叫伪元素。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值